-
Notifications
You must be signed in to change notification settings - Fork 92
/
Start-Listener.ps1
181 lines (121 loc) · 4.29 KB
/
Start-Listener.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<#
.SYNOPSIS
This cmdlet is for starting a listener that a reverse shell connection can attach too.
.DESCRIPTION
This cmdlet opens a listner port to connect to from a target machine.
.PARAMETER Port
This parameter is for defining the listening port to connect too. The cmdlet binds connections to the port that you specify. The default value for this parameter is 1337.
.EXAMPLE
Start-Listener
# This examples connects to a listener on port 1337.
.EXAMPLE
Start-Listener -Port 1234
# This examples connects to a listener on port 1234.
.NOTES
Author: Robert H. Osborne
Alias: tobor
Contact: [email protected]
.INPUTS
None
.OUTPUTS
None
.LINK
https://osbornepro.com
https://writeups.osbornepro.com
https://btpssecpack.osbornepro.com
https://github.com/tobor88
https://gitlab.com/tobor88
https://www.powershellgallery.com/profiles/tobor
https://www.linkedin.com/in/roberthosborne/
https://www.credly.com/users/roberthosborne/badges
https://www.hackthebox.eu/profile/52286
#>
Function Start-Listener {
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Low')]
param(
[Parameter(
Mandatory=$False,
Position=0,
ValueFromPipeline=$False,
HelpMessage='Enter a port to listen on. Valid ports are between 1 and 65535. Example: 1234')] # End Parameter
[ValidateRange(1,65535)]
[Int32]$Port = 1337
) # End param
$PortString = $Port.ToString()
Write-Verbose "Checking for availability of $PortString"
$TCPProperties = [System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties()
$Connections = $TCPProperties.GetActiveTcpListeners()
If ($Connections.Port -Contains "$Port")
{
Throw "[!] Port $Port is alreday in use by another process(es). Select another port to use or stop the occupying processes."
} # End If
Write-Verbose "Defining listener object"
$Socket = New-Object -TypeName System.Net.Sockets.TcpListener('0.0.0.0', $Port)
If ($Null -eq $Socket)
{
Exit
} # End If
Write-Verbose "Starting listener on port $PortString and creating job to allow closing the connection"
If ($PSCmdlet.ShouldProcess($Socket.Start()))
{
Try
{
Write-Output ("[*] Listening on [0.0.0.0] (port $PortString)")
While ($True)
{
Write-Verbose "Waiting for connection..."
If ($Socket.Pending())
{
$Client = $Socket.AcceptTcpClient()
Break;
} # End If
Start-Sleep -Seconds 2
} # End While
} # End Try
Finally
{
If (!($Client.Connected))
{
Write-Verbose "Terminating connection"
$Socket.Stop()
$Client.Close()
$Stream.Dispose()
Write-Verbose "Connection closed"
} # End If
} # End Finally
Write-Output "[*] Connection Established"
Write-Verbose "Creating byte stream"
$Stream = $Client.GetStream()
$Writer = New-Object -TypeName System.IO.StreamWriter($Stream)
$Buffer = New-Object -TypeName System.Byte[] 2048
$Encoding = New-Object -TypeName System.Text.AsciiEncoding
Write-Verbose "Begin command execution loop"
Do
{
$Command = Read-Host
$Writer.WriteLine($Command)
$Writer.Flush();
If ($Command -eq "exit")
{
Write-Verbose "Exiting"
Break
} # End If
$Read = $Null
While ($Stream.DataAvailable -or $Null -eq $Read)
{
$Read = $Stream.Read($Buffer, 0, 2048)
$Out = $Encoding.GetString($Buffer, 0, $Read)
Write-Output $Out
} # End While
} While ($Client.Connected -eq $True) # End Do While Loop
Write-Verbose "Terminating connection"
$Socket.Stop()
$Client.Close()
$Stream.Dispose()
Write-Verbose "Connection closed"
} # End If
Else
{
Write-Output "[*] Start-Listener would have started a listener on port $PortString"
} # End Else
} # End Function Start-Listener