hackoofr

Scan_TCP_Connections.bat

Mar 11th, 2023 (edited)
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 2.89 KB | None | 0 0
  1. <# : Batch Script Section
  2. @rem # The previous line does nothing in Batch, but begins a multiline comment block in PowerShell. This allows a single script to be executed by both interpreters.
  3. @echo off
  4. Title Scan TCP connections On Windows by Hackoo 2023 & Mode 80,10
  5. setlocal
  6. cd "%~dp0"
  7. Color 1B & echo( & Echo(
  8. Echo(           Please Wait ... Scanning TCP connections On Windows ...
  9. Powershell -executionpolicy bypass -Command "Invoke-Expression $([System.IO.File]::ReadAllText('%~f0'))"
  10. pause
  11. EndLocal
  12. goto:eof
  13. #>
  14. # Powershell Script Section begin here...
  15. # Here we execute our powershell commands...
  16. Clear-Host
  17. Write-Host Please Wait a While... -Fore Cyan
  18.  
  19. # Get the list separator value from the registry
  20. $ListSeparator = (Get-ItemProperty "HKCU:\Control Panel\International" -Name sList).sList
  21. # Get the total number of TCP connections to be processed
  22. $TotalConnections = (Get-NetTCPConnection).Count
  23.  
  24. # Initialize the progress bar
  25. $Progress = 0
  26. Write-Progress -Activity "Processing TCP connections" -Status "Starting..." -PercentComplete 0
  27.  
  28. $TCP_Var = Get-NetTCPConnection | ForEach-Object {
  29.         # Increment the progress bar
  30.         $Progress++
  31.         Write-Progress -Activity "Processing TCP connections" -Status "Progress: $($Progress)/$($TotalConnections)" -PercentComplete (($Progress / $TotalConnections) * 100)
  32.    
  33.         $process = Get-Process -Id $_.OwningProcess -ErrorAction SilentlyContinue
  34.         $_ | Add-Member -MemberType NoteProperty -Name "RemoteHostName" -Value (Resolve-DnsName $_.RemoteAddress -ErrorAction SilentlyContinue).NameHost -PassThru |
  35.         Add-Member -MemberType NoteProperty -Name "ProcessName" -Value $process.ProcessName -PassThru |
  36.         Add-Member -MemberType NoteProperty -Name "ProcessPath" -Value $process.Path -PassThru
  37.     } |
  38.     Select-Object OwningProcess, ProcessName, ProcessPath, State, LocalAddress, LocalPort, RemoteHostName, RemoteAddress, RemotePort
  39.  
  40. # Hide the progress bar
  41. Write-Progress -Activity "Processing TCP connections" -Completed
  42.  
  43. Clear-Host
  44. $TCP_Var | Out-GridView -Title "NET TCP CONNECTIONS" -PassThru
  45.  
  46. # Save the results to a log file on the desktop of the user in JSON format
  47. $LogFilePath = "$([Environment]::GetFolderPath('Desktop'))\TCP_Connections_Log.json"
  48. $TCP_Var | ConvertTo-Json | Out-File -FilePath $LogFilePath
  49. Write-Host "Results saved to $LogFilePath" -Fore Green
  50.  
  51. # Save the results to a log file on the desktop of the user in XML format
  52. $LogFilePath = "$([Environment]::GetFolderPath('Desktop'))\TCP_Connections_Log.xml"
  53. $TCP_Var | Export-Clixml -Path $LogFilePath
  54. Write-Host "Results saved to $LogFilePath" -Fore Green
  55.  
  56. # Save the results to a log file on the desktop of the user
  57. $LogFilePath = "$([Environment]::GetFolderPath('Desktop'))\TCP_Connections_Log.csv"
  58. $TCP_Var | Export-Csv -Path $LogFilePath -Delimiter $ListSeparator -NoTypeInformation
  59. Write-Host "Results saved to $LogFilePath" -Fore Green
Add Comment
Please, Sign In to add comment