Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. <#
  2.  
  3. Create FW rules for TCP and UDP Listening Ports
  4.  
  5. netstat -an -p tcp |Select-String "Listening"
  6. netstat -an -p udp |Select-String "Listening"
  7.  
  8. for each entry in netstat create firewall rule
  9. name = -p tcp|udp port port #
  10. description = automatic allow rule generated by powershell on get-date
  11.  
  12. Perhaps as part of this also create a dsc configuration document
  13. #>
  14.  
  15. $netstat = netstat -a -n -o -p TCP
  16. $netstat += netstat -a -n -o -p UDP
  17. [regex]$regexTCP = '(?<Protocol>\S+)\s+((?<LAddress>(2[0-4]\d|25[0-5]|[01]?\d\d?)\.(2[0-4]\d|25[0-5]|[01]?\d\d?)\.(2[0-4]\d|25[0-5]|[01]?\d\d?)\.(2[0-4]\d|25[0-5]|[01]?\d\d?))|(?<LAddress>\[?[0-9a-fA-f]{0,4}(\:([0-9a-fA-f]{0,4})){1,7}\%?\d?\]))\:(?<Lport>\d+)\s+((?<Raddress>(2[0-4]\d|25[0-5]|[01]?\d\d?)\.(2[0-4]\d|25[0-5]|[01]?\d\d?)\.(2[0-4]\d|25[0-5]|[01]?\d\d?)\.(2[0-4]\d|25[0-5]|[01]?\d\d?))|(?<RAddress>\[?[0-9a-fA-f]{0,4}(\:([0-9a-fA-f]{0,4})){1,7}\%?\d?\]))\:(?<RPort>\d+)\s+(?<State>\w+)\s+(?<PID>\d+$)'
  18. [regex]$regexUDP = '(?<Protocol>\S+)\s+((?<LAddress>(2[0-4]\d|25[0-5]|[01]?\d\d?)\.(2[0-4]\d|25[0-5]|[01]?\d\d?)\.(2[0-4]\d|25[0-5]|[01]?\d\d?)\.(2[0-4]\d|25[0-5]|[01]?\d\d?))|(?<LAddress>\[?[0-9a-fA-f]{0,4}(\:([0-9a-fA-f]{0,4})){1,7}\%?\d?\]))\:(?<Lport>\d+)\s+(?<RAddress>\*)\:(?<RPort>\*)\s+(?<PID>\d+)'
  19. $Listening = @()
  20. foreach ($Line in $Netstat)
  21. {
  22. switch -regex ($Line.Trim())
  23. {
  24. $RegexTCP
  25. {
  26. $MyProtocol = $Matches.Protocol
  27. $MyLocalAddress = $Matches.LAddress
  28. $MyLocalPort = $Matches.LPort
  29. $MyRemoteAddress = $Matches.Raddress
  30. $MyRemotePort = $Matches.RPort
  31. $MyState = $Matches.State
  32. $MyPID = $Matches.PID
  33. $MyProcessName = (Get-Process -Id $Matches.PID -ErrorAction SilentlyContinue).ProcessName
  34. $MyProcessPath = (Get-Process -Id $Matches.PID -ErrorAction SilentlyContinue).Path
  35. $MyUser = (Get-WmiObject -Class Win32_Process -Filter ("ProcessId = "+$Matches.PID)).GetOwner().User
  36. }
  37. $RegexUDP
  38. {
  39. $MyProtocol = $Matches.Protocol
  40. $MyLocalAddress = $Matches.LAddress
  41. $MyLocalPort = $Matches.LPort
  42. $MyRemoteAddress = $Matches.Raddress
  43. $MyRemotePort = $Matches.RPort
  44. $MyState = $Matches.State
  45. $MyPID = $Matches.PID
  46. $MyProcessName = (Get-Process -Id $Matches.PID -ErrorAction SilentlyContinue).ProcessName
  47. $MyProcessPath = (Get-Process -Id $Matches.PID -ErrorAction SilentlyContinue).Path
  48. $MyUser = (Get-WmiObject -Class Win32_Process -Filter ("ProcessId = "+$Matches.PID)).GetOwner().User
  49. }
  50. }
  51. $LineItem = New-Object -TypeName PSobject -Property @{
  52. Protocol = $MyProtocol
  53. LocalAddress = $MyLocalAddress
  54. LocalPort = $MyLocalPort
  55. RemoteAddress = $MyRemoteAddress
  56. RemotePort = $MyRemotePort
  57. State = $MyState
  58. PID = $MyPID
  59. ProcessName = $MyProcessName
  60. ProcessPath = $MyProcessPath
  61. User = $MyUser
  62. }
  63. if ($LineItem.LocalAddress = "0.0.0.0")
  64. {
  65. if (($LineItem.State) -and ($LineItem.State.ToUpper() -eq "LISTENING"))
  66. {
  67. if ($LineItem.User)
  68. {
  69. $User = $LineItem.User.ToLower()
  70. }
  71. else
  72. {
  73. $User = "system"
  74. }
  75. if (($User -ne "system") -and ($User -ne "updatususer") -and ($User -notlike "network*") -and ($User -notlike "local s*"))
  76. {
  77. if ($LineItem.ProcessName.ToLower() -ne "system")
  78. {
  79. $Listening += $LineItem
  80. }
  81. }
  82. }
  83. }
  84. }
  85. #
  86. # $Listening contains a list of services/applications listening on a given port + protocol
  87. #
  88. foreach ($Listener in $Listening)
  89. {
  90. $Protocol = $Listener.Protocol.ToUpper()
  91. $Port = $Listener.LocalPort
  92. New-NetFirewallRule `
  93. -DisplayName "Allow $($Protocol) traffic over port $($Port)" `
  94. -Name "AUTOGEN_$($Protocol)_$($Port)" `
  95. -Action Allow `
  96. -Description $Listener `
  97. -Direction Inbound `
  98. -Enabled True `
  99. -LocalPort $Listener.LocalPort `
  100. -Protocol $Listener.Protocol
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement