-
Notifications
You must be signed in to change notification settings - Fork 60
/
batch_interpreter.py
388 lines (361 loc) · 16.4 KB
/
batch_interpreter.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Based on source code from: https://github.com/DissectMalware/batch_deobfuscator
# Original License: MIT License
# Copyright (c) 2018 Malwrologist
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# NOTE: The code was refactored and the following modifications were made:
# - allow stripping used variable definitions
# - allow stripping echo commands
# - allow stripping comments
# - allow processing of strings rather than files
from __future__ import annotations
import re
import os
import io
from dataclasses import dataclass, field
from enum import Enum, IntFlag, auto
from typing import Dict, Generator, List, NamedTuple, Optional
from collections import defaultdict
class _T(str, Enum):
OPEN = '(' # noqa
CLOSE = ')' # noqa
ESCAPE = '^' # noqa
CONNECT = '&' # noqa
PIPE = '|' # noqa
QUOTE = '"' # noqa
V1 = '%' # noqa
V2 = '!' # noqa
SPACE = ' ' # noqa
class _S(int, Enum):
INIT = auto()
STRING = auto()
ESCAPE = auto()
VARIABLE_TYPE1 = auto()
VARIABLE_TYPE2 = auto()
class STRIP(IntFlag):
DEFINITION = 0b001 # noqa
ECHO = 0b010 # noqa
COMMENT = 0b100 # noqa
ALL = 0b111 # noqa
NONE = 0b000 # noqa
@dataclass
class ScriptVariable:
definitions: Dict[int, str] = field(default_factory=dict)
evaluations: List[int] = field(default_factory=list)
@property
def value(self):
latest = max(self.definitions)
return self.definitions[latest]
class DeobfuscatedLine(NamedTuple):
depth: int
value: str
class BatchDeobfuscator:
variables: Dict[str, ScriptVariable]
pending_subcommand: Optional[str]
def __init__(self):
if os.name == 'nt':
variables = {name.lower(): value for name, value in os.environ.items()}
else:
variables = {
'allusersprofile' : 'C:\\ProgramData',
'appdata' : 'C:\\Users\\puncher\\AppData\\Roaming',
'commonprogramfiles' : 'C:\\Program Files\\Common Files',
'commonprogramfiles(x86)"' : 'C:\\Program Files (x86)\\Common Files',
'commonprogramw6432' : 'C:\\Program Files\\Common Files',
'computername' : 'MISCREANTTEARS',
'comspec' : 'C:\\WINDOWS\\system32\\cmd.exe',
'driverdata' : 'C:\\Windows\\System32\\Drivers\\DriverData',
'fps_browser_app_profile_string' : 'Internet Explorer',
'fps_browser_user_profile_string' : 'Default',
'homedrive' : 'C:',
'homepath' : '\\Users\\puncher',
'java_home' : 'C:\\Program Files\\Amazon Corretto\\jdk11.0.7_10',
'localappdata' : 'C:\\Users\\puncher\\AppData\\Local',
'logonserver' : '\\\\MISCREANTTEARS',
'number_of_processors' : '4',
'onedrive' : 'C:\\Users\\puncher\\OneDrive',
'os' : 'Windows_NT',
'path' : 'C:\\Program Files\\Amazon Corretto\\jdk11.0.7_10\\bin;C:\\WINDOWS\\system32;'
'C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerS'
'hell\\v1.0\\;C:\\Program Files\\dotnet\\;C:\\Program Files\\Microsoft SQL Se'
'rver\\130\\Tools\\Binn\\;C:\\Users\\puncher\\AppData\\Local\\Microsoft\\Wind'
'owsApps;%USERPROFILE%\\AppData\\Local\\Microsoft\\WindowsApps;',
'pathext' : '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC',
'processor_architecture' : 'AMD64',
'processor_identifier' : 'Intel Core Ti-83 Family 6 Model 158 Stepping 10, GenuineIntel',
'processor_level' : '6',
'processor_revision' : '9e0a',
'programdata' : 'C:\\ProgramData',
'programfiles' : 'C:\\Program Files',
'programfiles(x86)"' : 'C:\\Program Files (x86)',
'programw6432' : 'C:\\Program Files',
'psmodulepath' : 'C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\Modules\\',
'public' : 'C:\\Users\\Public',
'sessionname' : 'Console',
'systemdrive' : 'C:',
'systemroot' : 'C:\\WINDOWS',
'temp' : 'C:\\Users\\puncher\\AppData\\Local\\Temp',
'tmp' : 'C:\\Users\\puncher\\AppData\\Local\\Temp',
'userdomain' : 'MISCREANTTEARS',
'userdomain_roamingprofile' : 'MISCREANTTEARS',
'username' : 'puncher',
'userprofile' : 'C:\\Users\\puncher',
'windir' : 'C:\\WINDOWS',
'__compat_layer' : 'DetectorsMessageBoxErrors',
}
self.pending_subcommand = None
self.variables = defaultdict(ScriptVariable)
for name, value in variables.items():
self.variables[name].definitions[-1] = value
def read_logical_lines(self, text: str):
logical_line = io.StringIO()
for line in text.splitlines(False):
if not line.endswith(_T.ESCAPE):
logical_line.write(line)
yield logical_line.getvalue()
logical_line.seek(0)
logical_line.truncate(0)
else:
logical_line.write(line)
logical_line.write('\n')
def commands(self, logical_line: str) -> Generator[str, None, None]:
state = _S.INIT
start = 0
for offset, token in enumerate(logical_line):
if state is _S.INIT:
if token == _T.QUOTE:
state = _S.STRING
elif token == _T.ESCAPE:
state = _S.ESCAPE
elif token == _T.CONNECT or token == _T.PIPE:
yield logical_line[start:offset].strip()
start = offset + 1
elif state is _S.STRING:
if token == _T.QUOTE:
state = state.INIT
elif state is _S.ESCAPE:
state = _S.INIT
last_command = logical_line[start:].strip()
if last_command:
yield last_command
def evaluate_variable(self, lno, variable):
pattern = (
r'''(?P<delim>%|!)\s*(?P<name>[\w#$'()*+,-.?@\[\]`{}~ ]+)'''
r'''(:~\s*(?P<index>[+-]?\d+)\s*,\s*(?P<length>[+-]?\d+)\s*)?(?P=delim)''')
match = re.fullmatch(pattern, variable)
if match is None:
return variable
var = self.variables.get(match.group('name').lower())
if var is None:
return variable
value = var.value
var.evaluations.append(lno)
if match.group('index'):
index = int(match.group('index'))
length = int(match.group('length'))
if length >= 0:
value = value[index : length + index]
else:
value = value[index : length]
return value
return value
def interpret(self, lno: int, command: str) -> None:
command = command.strip()
index = 0
last = len(command) - 1
while index < last and (command[index] == _T.SPACE or command[index] == _T.OPEN):
if command[index] == _T.OPEN:
while last > index and (command[last] == _T.SPACE or command[last] == _T.CLOSE):
if command[last] == _T.CLOSE:
last -= 1
break
last -= 1
index += 1
command = command[index : last + 1]
if command.lower().startswith('cmd'):
pattern = r"\s*(call)?cmd(.exe)?\s*((\/A|\/U|\/Q|\/D)\s+|((\/E|\/F|\/V):(ON|OFF))\s*)*(\/c|\/r)\s*(?P<cmd>.*)"
match = re.search(pattern, command, re.IGNORECASE)
if match and match.group('cmd'):
cmd = match.group('cmd').strip(_T.QUOTE)
self.pending_subcommand = cmd
else:
pattern = (
r"(\s*(call)?\s*set\s+\"?(?P<var>[\w#$'()*+,-.?@\[\]`{}~ ]+)=\s*(?P<val>[^\"\n]*)\"?)|"
r"(\s*(call)?\s*set\s+/p\s+\"?(?P<input>[\w#$'()*+,-.?@\[\]`{}~ ]+)=[^\"\n]*\"?)"
)
match = re.search(pattern, command, re.IGNORECASE)
if match is None:
return
var = match.group('input')
if var is not None:
var = var.lower()
val = '__input__'
else:
var = match.group('var').lower()
val = match.group('val')
self.variables[var].definitions[lno] = val
def normalize(self, lno: int, command: str):
result = ''
state = _S.INIT
stack = []
for token in command:
if state == _S.INIT:
if token == _T.QUOTE:
state = _S.STRING
result += token
elif token in ',;\t':
# commas (",") are replaced by spaces, unless they are part of a string in doublequotes
# semicolons (";") are replaced by spaces, unless they are part of a string in doublequotes
# tabs are replaced by a single space
# http://www.robvanderwoude.com/parameters.php
result += _T.SPACE
elif token == _T.ESCAPE:
state = _S.ESCAPE
stack.append(_S.INIT)
elif token == _T.V1:
variable_start = len(result)
result += _T.V1
stack.append(_S.INIT)
state = _S.VARIABLE_TYPE1
elif token == _T.V2:
variable_start = len(result)
result += _T.V1
stack.append(_S.INIT)
state = _S.VARIABLE_TYPE2
else:
result += token
elif state == _S.STRING:
if token == _T.QUOTE:
state = _S.INIT
result += token
elif token == _T.V1:
variable_start = len(result)
result += _T.V1
stack.append(_S.STRING)
state = _S.VARIABLE_TYPE1
elif token == _T.V2:
variable_start = len(result)
result += _T.V1
stack.append(_S.STRING)
state = _S.VARIABLE_TYPE2
elif token == _T.ESCAPE:
state = _S.ESCAPE
stack.append(_S.STRING)
else:
result += token
elif state == _S.VARIABLE_TYPE1:
if token.isdigit() and result[-1] == _T.V1:
result += token
state = stack.pop()
elif token == _T.V1 and result[-1] != _T.V1:
result += _T.V1
value = self.evaluate_variable(lno, result[variable_start:].lower())
result = result[:variable_start]
result += value
state = stack.pop()
elif token == _T.V1:
variable_start = len(result)
result += token
elif token == _T.QUOTE:
if stack[-1] == _S.STRING:
result += token
stack.pop()
state = _S.INIT
else:
result += token
elif token == _T.ESCAPE:
state = _S.ESCAPE
stack.append(_S.VARIABLE_TYPE1)
else:
result += token
elif state == _S.VARIABLE_TYPE2:
if token == _T.V2 and result[-1] != _T.V1:
result += _T.V1
value = self.evaluate_variable(lno, result[variable_start:].lower())
result = result[:variable_start]
result += value
state = stack.pop()
elif token == _T.V2:
variable_start = len(result)
result += token
elif token == _T.QUOTE:
if stack[-1] == _S.STRING:
result += token
stack.pop()
state = _S.INIT
else:
result += token
elif token == _T.ESCAPE:
state = _S.ESCAPE
stack.append(_S.VARIABLE_TYPE1)
else:
result += token
elif state == _S.ESCAPE:
result += token
state = stack.pop()
return result.strip()
def _interpret(self, text: str, lno: int = 0, depth: int = 0) -> Generator[DeobfuscatedLine, None, None]:
for line in self.read_logical_lines(text):
for command in self.commands(line):
normalized = self.normalize(lno, command)
self.interpret(lno, normalized)
yield DeobfuscatedLine(depth, normalized)
lno += 1
if self.pending_subcommand is not None:
subcommand = BatchDeobfuscator()
for name, var in self.variables.items():
subcommand.variables[name].definitions[-1] = var.value
for result in subcommand._interpret(self.pending_subcommand, lno, depth + 1):
yield result
lno += 1
self.pending_subcommand = None
def deobfuscate(
self,
text: str,
mode: STRIP = STRIP.NONE
) -> str:
lines = list(self._interpret(text))
used = set()
if mode & STRIP.DEFINITION:
for variable in self.variables.values():
if variable.evaluations:
used.update(variable.definitions.keys())
def tab(depth):
return ' ' * 3 * depth
def output():
depth = 0
for lno, line in enumerate(lines):
if lno in used:
continue
if not line.value:
continue
cmd = line.value.split()[0].lower()
cmd, _, _ = cmd.partition('/')
if mode & STRIP.ECHO and cmd == 'echo':
continue
if mode & STRIP.COMMENT and (line.value.startswith('::') or cmd == 'rem'):
continue
if line.depth > depth:
yield F'{tab(depth)}:: SUBCOMMAND'
depth = line.depth
yield F'{tab(depth)}{line.value}'
return '\n'.join(output())