jueves, 8 de agosto de 2019

Como guardar registros detallados de las sesiones de PowerShell

Auditar la el historial de actividad de PowerShell es algo que puede venir útil en caso de incidentes de seguridad o con fines de documentación. En este post, se muestra como habilitar una transcripción detallada de la actividad de PowerShell mediante directivas de grupo.

En una nueva directiva de grupo, ir a "Configuración del equipo > Directivas > Plantillas Administrativas > Componentes de Windows > Windows Powershell" y seleccionar "Activar la transcripción de Powershell". Aquí debe habilitarse la política y seleccionar un directorio donde se guardaran las transcripciones.



Despues de un gpupdate /force para forzar la aplicación de la nueva política, podemos probar si la nueva configuración está en efecto corriendo un comando de PowerShell:


Y finalmente revisar que los comandos ingresados esten siendo registrados en el directorio especificado:


Eso es todo, en otro post se mostrará como utilizar Start-Transcript para registrar los comandos de una sesión individual.

martes, 16 de julio de 2019

Hyper-V: Conectar una maquina virtual a otro VMSwitch con PowerShell

Para conectar una máquina virtual a un VMSwitch distinto al que tiene asignada o asignarle uno por primera vez en caso de que no este conectada, se puede seguir el siguiente ejemplo. En primer lugar, verificar a que switch virtual está conectada la VM (si es que tiene alguno asignado):

PS C:\Users\Administrador> get-vm -Name vm1 | Get-VMNetworkAdapter

Name             IsManagementOs VMName SwitchName MacAddress   Status IPAddresses
----             -------------- ------ ---------- ----------   ------ -----------
Adaptador de red False          vm1               00155D581500 {Ok}   {}

En este caso se ve que el adaptador de esta VM no está conectado a ningún switch virtual. Como este servidor aún no tiene ningún VMSwitch, empiezo verificando las interfaces físicas disponibles en el equipo:

PS C:\Users\Administrador> Get-NetAdapter

Name                      InterfaceDescription                    ifIndex Status       MacAddress             LinkSpeed
----                      --------------------                    ------- ------       ----------             ---------
Ethernet0                 Intel(R) 82574L Gigabit Network Conn...       4 Up           00-0C-29-F7-C4-22         1 Gbps
vEthernet (int_switch)    Hyper-V Virtual Ethernet Adapter             12 Up           00-15-5D-58-15-01        10 Gbps


En este servidor la interfaz física Ethernet0 está libre, con el siguiente comando creo un VMSwitch asociado a la misma:

PS C:\Users\Administrador> New-VMSwitch -Name vmswitch_prod -NetAdapterName "Ethernet0" -AllowManagementOS $true

Name          SwitchType NetAdapterInterfaceDescription
----          ---------- ------------------------------
vmswitch_prod External   Intel(R) 82574L Gigabit Network Connection


Finalmente, conectamos el adaptador de la máquina virtual al VMSwitch nuevo con el cmdlet Connect-VMNetworkAdapter y verificamos:


PS C:\Users\Administrador> get-vm -Name vm1 | Get-VMNetworkAdapter | Connect-VMNetworkAdapter -SwitchName vmswitch_prod
PS C:\Users\Administrador> get-vm -Name vm1 | Get-VMNetworkAdapter

Name             IsManagementOs VMName SwitchName    MacAddress   Status IPAddresses
----             -------------- ------ ----------    ----------   ------ -----------
Adaptador de red False          vm1    vmswitch_prod 00155D581500 {Ok}   {}

miércoles, 10 de julio de 2019

PowerShell: Cambiar el tipo de perfil de red en Windows

En ocasiones, al tratar de configurar WinRM para conexiones remotas con PowerShell, recibimos un error debido a que PowerShell considera inseguro realizar conexiones sobre una red pública. Para resolver esto es necesario modificar el perfil de red que Windows asigna a los adaptadores de red, a continuación se muestra como hacer este cambio.

1-) Identificar el adaptador de red cuyo perfil necesitamos modificar:

PS C:\Windows\system32> Get-NetConnectionProfile

Name             : Unidentified network
InterfaceAlias   : vEthernet (Default Switch)
InterfaceIndex   : 10
NetworkCategory  : Public
IPv4Connectivity : NoTraffic
IPv6Connectivity : NoTraffic

Name             : Network 18
InterfaceAlias   : vEthernet (Ether-wifi)
InterfaceIndex   : 15
NetworkCategory  : Private
IPv4Connectivity : Internet
IPv6Connectivity : NoTraffic
2-) Utilizando el InterfaceIndex que obtuvimos en el paso anterior, modificamos al tipo de perfil deseado (Las opciones son Private, Public y Domain):

PS C:\Windows\system32> Set-NetConnectionProfile -InterfaceIndex 10 -NetworkCategory Private
PS C:\Windows\system32> Get-NetConnectionProfile



Name             : Unidentified network
InterfaceAlias   : vEthernet (Default Switch)
InterfaceIndex   : 10
NetworkCategory  : Private
IPv4Connectivity : NoTraffic
IPv6Connectivity : NoTraffic


Name             : Network 18
InterfaceAlias   : vEthernet (Ether-wifi)
InterfaceIndex   : 15
NetworkCategory  : Private
IPv4Connectivity : Internet
IPv6Connectivity : NoTraffic

Como alternativa, podría usarse el parametro SkipNetworkCheck al habilitar el PSRemoting para que se ignore el tipo de perfil establecido en el adaptador de red:

PS C:\Windows\system32> Enable-PSRemoting -Force -SkipNetworkCheck

sábado, 6 de julio de 2019

PowerShell: Determinar el historial de membresias de grupo en Active Directory

Hay ocasiones en donde es necesario conocer en que momento un usuario fué agregado a un grupo, en estos casos, esta información puede extraerse de Active Directory gracias a ciertos atributos existentes en los metadatos de replicación.

PS C:\Users\Administrador>
PS C:\Users\Administrador> $username = "juan"
PS C:\Users\Administrador> $userobj  = Get-ADUser $username
PS C:\Users\Administrador>
PS C:\Users\Administrador> Get-ADUser $userobj.DistinguishedName -Properties memberOf |
>>  Select-Object -ExpandProperty memberOf |
>>  ForEach-Object {
>>     Get-ADReplicationAttributeMetadata $_ -Server localhost -ShowAllLinkedValues |
>>       Where-Object {$_.AttributeName -eq 'member' -and
>>       $_.AttributeValue -eq $userobj.DistinguishedName} |
>>       Select-Object FirstOriginatingCreateTime, Object, AttributeValue
>>     } | Sort-Object FirstOriginatingCreateTime -Descending

FirstOriginatingCreateTime Object                                               AttributeValue
-------------------------- ------                                               --------------
6/7/2019 17:48:59          CN=Administradores clave,CN=Users,DC=seclab,DC=local CN=juan,CN=Users,DC=seclab,DC=local


PS C:\Users\Administrador> 

Como se ve en la salida del script anterior, con esto puede determinarse en que fecha un usuario fué agregado a uno o mas grupos. Podría ser que necesitamos esa información con fines forenses, o bien para determinar que membresias de grupo podrían ser reducidas en caso de que algún usuario experimente problemas de "token bloat". El script es una colaboración de Ashley McGlone, ex Premier Field Engineer de Microsoft, aquí puede verse el articulo original.

Por último vale mencionar que ademas de funcionar con cuentas de usuario, puede utilizarse con cuentas de equipos, sustituyendo Get-ADUser por Get-ADComputer

lunes, 8 de abril de 2019

Explorando la red con PowerShell: Escaneo de puertos TCP y barridos ping

A continuación comparto una recopilación de algunos comandos útiles a la hora de explorar la red utilizando PowerShell.

Barridos ping

Para descubrir todos los hosts activos de una red de forma rápida podemos realizar un barrido ping o ICMP. En este ejemplo se usa un operador de rango para hacer ping a todas las IP's de una red /24 y del resultado filtramos los que contienen la linea TTL, que son los que nos interesan (esto excluye las IP's que respondieron con tiempo de espera agotado)


PS C:\> 1..254 | % {echo "192.168.88.$_"; ping -n 1 -w 100 192.168.88.$_} | Select-String ttl

Respuesta desde 192.168.88.1: bytes=32 tiempo=2ms TTL=64
Respuesta desde 192.168.88.214: bytes=32 tiempo=2ms TTL=64
Respuesta desde 192.168.88.229: bytes=32 tiempo<1m TTL=128

PS C:\>

Escaneo de puertos TCP

Para probar si un puerto TCP está abierto, podemos usar el cmdlet Test-NetConnection

PS C:\> foreach ($ip in 1..254) {Test-NetConnection -Port 80 -InformationLevel "Detailed" 192.168.88.$ip}



ComputerName            : 192.168.88.1
RemoteAddress           : 192.168.88.1
RemotePort              : 80
NameResolutionResults   : 192.168.88.1
MatchingIPsecRules      :
NetworkIsolationContext : Private Network
InterfaceAlias          : vEthernet (Ether-wifi)
SourceAddress           : 192.168.88.229
NetRoute (NextHop)      : 0.0.0.0
TcpTestSucceeded        : True


ADVERTENCIA: TCP connect to (192.168.88.2 : 80) failed
ADVERTENCIA: Ping to 192.168.88.2 failed with status: TimedOut

ComputerName            : 192.168.88.2
RemoteAddress           : 192.168.88.2
RemotePort              : 80
NameResolutionResults   : 192.168.88.2
MatchingIPsecRules      :
NetworkIsolationContext : Private Network
InterfaceAlias          : vEthernet (Ether-wifi)
SourceAddress           : 192.168.88.229
NetRoute (NextHop)      : 0.0.0.0
PingSucceeded           : False
PingReplyDetails (RTT)  : 0 ms
TcpTestSucceeded        : False


El comando anterior arroja resultados muy verbose para mi gusto, la siguiente alternativa facilita probar un rango de puertos y da como resultado una salida mas limpia:

PS C:\> 1..1024 | % { echo ((new-object Net.Sockets.TcpClient).Connect("192.168.61.1",$_)) "$_ is open" } 2>$null
22 is open
80 is open
443 is open
PS C:\>

Eso es todo. En una próxima entrada analizaremos opciones para probar puertos UDP con PowerShell.

domingo, 7 de abril de 2019

Preparación para el examen AZ-500: Sesiones de Ignite

Recientemente se lanzó el examen beta AZ-500: Microsoft Azure Security Engineer, siendo el único necesario para obtener la certificación Microsoft Certified: Azure Security Engineer Associate. De momento no hay una guía de preparación oficial, por lo cual lo que voy a apoyarme exclusivamente en la documentación de Microsoft y en Sesiones de Ignite y Microsoft Mechanics.

Si quieres tomar este exámen beta, puedes conseguir un voucher con un 80% de descuento en este post de Microsoft Learning Blog.

A continuación, una lista de las sesiones de Ignite que estoy usando como preparación para este examen.

Azure Essentials: Defense in depth security



Azure Security Center | Azure Friday



Azure security & management - BRK2021



Azure Security fundamentals: Protecting infrastructure apps and data in the cloud - BRK2395



Built-in not bolted on - securing your Azure resources in practice - THR3064



Governing Azure subscriptions with auditing management groups and policies - BRK3268



Protect server workloads across datacenter and cloud with Azure Security Center and - BRK3235



Protect the keys to your kingdom with Privileged Identity Management - BRK3248



Securing your data with Azure SQL DB : Build 2018



Understanding how Microsoft Information Protection capabilities work together to - BRK3002



Accelerate deployment and adoption of Microsoft Information Protection solutions - BRK3009



Securing your hybrid cloud environments with Azure ATP and AAD Identity Protection - BRK3237



Securing web applications using Web Application Firewall



Securing Azure SQL Database Managed Instance: Overview and best practices - BRK3163



Secure customer identity and access management using Azure Active Directory B2C - BRK3240



Monitoring your networks in Azure - BRK3298



Monitor your infrastructure and analyze operational logs at scale with Azure Monitor - BRK3354



MNA 02/08/2019 - Azure DDoS



Common sense in the world of Azure governance - THR2102




Manage keys secrets and certificates for secure apps and data with Azure Key Vault - BRK3059



Lock down access to Azure using identity - BRK3383



Learn how to protect your data in Azure Storage with new features and capabilities - BRK3340



In the security trenches of Azure SQL Database and Azure SQL Data Warehouse - BRK3149



Identity and secure resource access in App Service and Azure Functions - Matthew Henderson



Granting partners and suppliers access to resources using Azure Active Directory B2B - BRK3249



From the trenches: Hardening your Azure Active Directory tenant - THR2214



Expose APIs with peace of mind when using Azure API Management - BRK2200



Enable Azure Active Directory Conditional Access to secure user access while - BRK3241



Deep dive into Implementing governance at scale through Azure Policy - BRK3085



CYA (covering your assets) with security and threat detection in Azure - BRK2421



Azure Update Inventory and Automation for Linux and Windows VM management - BRK3063



Azure Information Protection and Exchange Online - better together - THR3076



Azure Firewall and Best Practices in building an enterprise-grade DMZ in Azure - BRK4029



Azure Active Directory security insights with Conditional Access Identity Protection - BRK3401



Azure Active Directory best practices from around the world - BRK3408



Attack discovery and investigation with Azure Advanced Threat Protection - THR3037



AKS (Azure Kubernetes Service) Security & Identity updates | Best of Microsoft Ignite 2018



How to delegate administration in Azure AD - BRK3239



Early look at Microsoft Threat Protection





domingo, 24 de marzo de 2019

Transferir archivos con PowerShell usando WinRM

Mover archivos con PowerShell es sencillo siempre que tengamos acceso vía SMB al servidor remoto. Por ejemplo, para copiar la carpeta "C:\Tools" y todo su contenido (-Recurse) al servidor FILESERVER por SMB, basta con el siguiente comando:

Copy-Item -Path "C:\Tools" -Destination "\\FILESERVER\C$" -Recurse

El comando asume que las credenciales utilizándose son válidas en el equipo remoto. En caso de que el servidor no tenga habilitado el SMB, puede lograrse lo mismo mediante WinRM. Los requisitos son los siguientes:


  • PowerShell 5.0 en el equipo local y en el remoto
  • WinRM debe estar habilitado (Viene configurado por defecto desde Windows Server 2012 en adelante, en caso de que esté deshabilitado puede habilitarse con Enable-PSRemoting -Force)
  • Los puertos 5985 (HTTP) y 5986 (HTTPS) deben estar habilitados.
  • Ambos equipos deben estar en dominio. Si la estación de trabajo desde donde se está administrando el servidor está en un grupo de trabajo, seguir las siguientes instrucciones.

Primero, crear una sesión remota y guardarla dentro de una variable:

$FileSession = New-PSSession –ComputerName FILESERVER

El comando es muy similar al anterior, el único cambio es que en este caso utilizamos el parametro ToSession y le pasamos la variable de la sesión creada previamente:

Copy-Item –Path "C:\Tools" –Destination 'C:\' –ToSession $FileSession -Recurse

A pesar de ser similar al comando anterior, esta vez la transferencia se realizó vía WinRM. Una vez terminada la transferencia, es buena practica remover la sesión que creamos:

$FileSession | Remove-PSSession