Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. <#
  2. Finds all IPs in given subnet that listen to RDP port
  3. And try to connect to all with given credentials
  4. #>
  5. $c_type_subnet = "192.168.0"
  6. $rdp_default_port = "3389"
  7. $username = "username"
  8. $password = "password"
  9.  
  10. $nmap_output_file_name = "temp_nmap.txt"
  11. $ips_file_name = "temp_ips.txt"
  12.  
  13. if( Test-Path $nmap_output_file_name ) { Remove-Item $nmap_output_file_name }
  14. if( Test-Path $ips_file_name ) { Remove-Item $ips_file_name }
  15.  
  16. #Run nmap and save its output
  17. $nmap_expression = "nmap $c_type_subnet.* -p T:$rdp_default_port > $nmap_output_file_name"
  18. Invoke-Expression $nmap_expression
  19.  
  20. #Save all ips into a different file from the nmap output
  21. $regex = '\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b'
  22. select-string -Path $nmap_output_file_name -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value } > $ips_file_name
  23.  
  24. $lines = Get-Content $ips_file_name | Where {$_ -notmatch '^\s+$'}
  25.  
  26. foreach( $line in $lines )
  27. {
  28. $cur_ip_address = $line
  29. $expression = "cmdkey /generic:TERMSRV/`"$cur_ip_address`" /user:`"$username`" /pass:`"$password`""
  30. Invoke-Expression $expression
  31. $expression = "mstsc /v:`"$cur_ip_address`""
  32. Invoke-Expression $expression
  33. }
  34.  
  35. Remove-Item $nmap_output_file_name
  36. Remove-Item $ips_file_name
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement