• EnglishSpanishGermanFrenchPolishChinese (Traditional)


EnglishSpanishGermanFrenchPolishChinese (Traditional)

Operating systems, scripting, PowerShell and security

Operating systems, software development, scripting, PowerShell tips, network and security

Menú principal
  • Categorías
  • Cursos
  • Libro de PowerShell (nivel medio)
  • Libro de PowerShell (nivel avanzado)
  • Lo mejor
  • Lo último
  • Proyectos
  • Contactar
Ir al contenido

Ejercicios de seguridad: realizar una comunicación TCP segura (utilizando Cryptographic Message Syntax)

Contenidos

  • Servidor
  • Cliente

Servidor

PowerShell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
##Server
 
$TcpListener = New-Object System.Net.Sockets.TcpListener (New-Object System.Net.IPEndPoint([IPAddress]::Loopback,2050))
$TcpListener.Start()
 
$stream = (New-Object System.IO.StreamReader ($TcpListener.AcceptTcpClient().GetStream())).ReadLine()
 
#Para descifrar el mensaje es necesario tener el certificado
# https://www.jesusninoc.com/11/20/imports-certificates-and-private-keys-from-a-personal-information-exchange-pfx-file-to-the-destination-store/
"Texto cifrado: $stream"
$stream > cifradoparadescifrar.txt
$mensaje = Unprotect-CmsMessage -Path cifradoparadescifrar.txt
"Texto descifrado: $mensaje"
 
$TcpListener.Stop()

Cliente

PowerShell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
##Client
 
$TcpClient = New-Object System.Net.Sockets.TcpClient([IPAddress]"127.0.0.1", "2050")
 
$stream = New-Object System.IO.StreamWriter $TcpClient.GetStream()
 
# Enviar mensaje cifrado
 
# Crear un certificado para firmar el mensaje
# https://www.jesusninoc.com/11/18/exports-a-certificate-to-a-personal-information-exchange-pfx-file/
$cert = New-SelfSignedCertificate -DnsName jesusninoc3445122 -CertStoreLocation "Cert:\CurrentUser\My" -KeyUsage KeyEncipherment,DataEncipherment, KeyAgreement -Type DocumentEncryptionCert  -KeyExportPolicy ExportableEncrypted
 
# Encrypts content by using the Cryptographic Message Syntax format
"hola" | Protect-CmsMessage -To cn=jesusninoc3445122 -OutFile secret.txt
$val=[string](Get-Content secret.txt)
$stream.Write($val)
 
$stream.Dispose()
Publicado el día 11 de febrero de 2021

CATEGORÍAS

PowerShell, Red, Seguridad

ETIQUETAS

.NET, Certificate, Cifrado, cipher, client, CMS, data, Descifrar, DNS, dnsn, Ejercicios de seguridad, Encrypted, Exchange, Get-Content, HTTP, HTTPS, iOS, IP, IPAddress, Jesús Niño, Jesús Niño Camazón, New-Object, New-SelfSignedCertificate, Path, Persona, Protect-CmsMessage, Server, socket, Sockets, Store, System.Net.IPEndPoint, System.Net.Sockets.TCPClient, TCP, Unprotect-CmsMessage

MÁS

  • Ejercicios de seguridad: práctica sobre virus en PowerShell
  • Ejercicios de seguridad: simular el funcionamiento de una VPN desde PowerShell
  • Utilizar los cmdlets de cifrado New-SelfSignedCertificate, Protect-CmsMessage, Get-CmsMessage y Unprotect-CmsMessage (crear certificado, cifrar, ver contenido cifrado y descifrar)
  • Utilización de técnicas de programación segura (Programación de servicios y procesos)
  • Ejercicios de PowerShell: enviar un mensaje entres varios equipos
  • Enviar un mensajes cifrado con Cryptographic Message Syntax (CMS) entre un cliente y un servidor mediante UDP desde PowerShell
Jesusninoc utiliza cookies: saber más aquí.