-
Notifications
You must be signed in to change notification settings - Fork 43
/
powerob.py
executable file
·351 lines (266 loc) · 11.6 KB
/
powerob.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
#!/usr/bin/python3
import os.path
from os import path
import re
import sys
import argparse
import random
import string
import json
import warnings
import sqlite3
# Suppressed because of weird future warning with regex
warnings.simplefilter(action='ignore', category=FutureWarning)
dashline = "-" * 75
db_name = "./powerob.db"
def banner():
print(r"""
___ ___ _
/ _ \_____ _____ _ __ /___| |__
/ /_)/ _ \ \ /\ / / _ | '__// /| '_ \
/ ___| (_) \ V V | __| | / \_//| |_) |
\/ \___/ \_/\_/ \___|_| \___/ |_.__/
author: Cory Wolff <fenrir>
company: Layer 8 <layer8security.com>
""")
print(dashline + "\n")
def python_check():
if sys.version_info.major != 3:
print(color("[-] Please run with Python 3. Python 2 is ded."))
sys.exit()
def db_check():
if not path.exists('db.json'):
return False
else:
print(color("[*] Db file found"))
return True
def color(text):
if text.startswith('[+]'):
return "\033[%d;3%dm%s\033[0m" % (0, 2, text)
if text.startswith('[-]'):
return "\033[%d;3%dm%s\033[0m" % (0, 1, text)
if text.startswith('[*]'):
return "\033[%d;3%dm%s\033[0m" % (0, 3, text)
# Adds the new functions/obfuscated functions to the db file.
def save_functions(filename, functions):
try:
conn = sqlite3.connect(db_name)
cursor = conn.cursor()
sql = '''CREATE TABLE IF NOT EXISTS Files
(FID INTEGER PRIMARY KEY AUTOINCREMENT,
FILENAME VARCHAR(42))'''
cursor.execute(sql)
sql = '''CREATE TABLE IF NOT EXISTS Functions
(FID INTEGER PRIMARY KEY AUTOINCREMENT,
FILE_NAME VARCHAR(42),
OG_FUNCTION VARCHAR(42),
OB_FUNCTION VARCHAR(42))'''
cursor.execute(sql)
cursor.execute("SELECT * FROM Files WHERE FILENAME = :og_file", {"og_file": filename})
rows = cursor.fetchall()
if len(rows) > 0:
# The file has been obfuscated before. Removed old functions from db and add new ones
cursor.execute("DELETE FROM Functions WHERE FILE_NAME = :file", {"file": filename})
for f in functions.items():
cursor.execute("INSERT INTO Functions(FILE_NAME, OG_FUNCTION, OB_FUNCTION) VALUES (?,?,?)", (filename, f[0],f[1]))
else:
# File is not in db so add filename to Files table and add functions to Functions table
cursor.execute("INSERT INTO Files(FILENAME) VALUES(?)", (filename, ))
for f in functions.items():
cursor.execute("INSERT INTO Functions(FILE_NAME, OG_FUNCTION, OB_FUNCTION) VALUES (?,?,?)", (filename, f[0],f[1]))
conn.commit()
return True
except:
print(color("[-] Unexpected error: {}".format(sys.exc_info())))
return False
def find_functions(input):
print(color("[+] Loading the Powershell script"))
function_pattern = r'([Ff]unction\s([A-Z]{1}[a-z]{2,10})-([A-Z]{1}[a-z]+[A-Z]{1}([^\s||^(]+)))'
#function_pattern = r'([Ff]unction\s(.*)\s\{)'
if not input.endswith('.ps1'):
print(color("[-] Error: The input file must end with .ps1"))
sys.exit()
try:
f = open(input,"r")
print(color("[+] Locating functions"))
contents = f.read()
funcs = re.findall(function_pattern, contents)
if funcs:
#Create list for function names
function_names = []
for i in funcs:
function_names.append(i[0].split(" ")[1])
return function_names
else:
print(color("[-] No Functions Found. Exiting..."))
return "0"
except IOError as e:
errno, strerror = e.args
print(color("[-] I/O error({0}): {1}".format(errno,strerror)))
except: #handle other exceptions such as attribute errors
print(color("[-] Unexpected error:" + str(sys.exc_info()[0])))
pass
def create_obfuscated_functions(functions):
print(color("[+] Obfuscating functions"))
# Create list for old function names with corresponding obfuscated function
substitutions = {}
for f in functions:
# Generate a random string to use in place of each function name
rando_name = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(8))
substitutions[f] = str(rando_name)
return substitutions
def write_functions_to_file(inputfile, outputfile, functions):
try:
# Open the file
with open(inputfile) as file:
file_as_str = file.read()
regex = re.compile("|".join(map(re.escape, functions.keys( ))))
newtext = regex.sub(lambda match: functions[match.group(0)], file_as_str)
# Write file
with open(outputfile, "w+") as f:
f.write(newtext)
f.close()
return True
except IOError as e:
errno, strerror = e.args
print(color("[-] I/O error ({}): {}".format(errno, strerror)))
sys.exit()
except: #handle other exceptions such as attribute errors
print(color("[-] Unexpected error yo:" + str(sys.exc_info())))
sys.exit()
pass
def remove_comments(outputfile):
print(color("[+] Removing Comments"))
comments_regex1 = r'<#((.|\n)*?)#>'
comments_regex2 = r'#.*'
try:
with open(outputfile) as file:
file_as_str = file.read()
nocomments = re.sub(comments_regex1, '', file_as_str)
nocomments = re.sub(comments_regex2, '', nocomments)
nocomments = nocomments.lstrip(' \n')
with open(outputfile, "w+") as f:
f.write(nocomments)
f.close()
return True
except IOError as e:
errno, strerror = e.args
print(color("[-] I/O error ({}): {}".format(errno, strerror)))
sys.exit()
except: #handle other exceptions such as attribute errors
print(color("[-] Unexpected error yo:" + str(sys.exc_info())))
sys.exit()
pass
#
#
# Start of class
#
#
class PowerOb(object):
def __init__(self):
parser = argparse.ArgumentParser(usage='''powerob.py <command> [<args>]
Possible Commands:
<obfuscate> // Obfuscate new powershell script
<list> // List obfuscated files and their commands
<getcommand> // Get a particular command from an obfuscated file
<cleardb> // Clears the database of previously obfuscated files and functions
Try powerob.py <command> -h for help with a particular command.
''')
parser.add_argument('command', help='Command to run')
args = parser.parse_args(sys.argv[1:2])
if not hasattr(self, args.command):
print (color('[-] Unrecognized command'))
parser.print_help()
exit(1)
# use dispatch pattern to invoke method with same name
getattr(self, args.command)()
def obfuscate(self):
parser = argparse.ArgumentParser(description='Obfuscate a new Powershell script.')
parser.add_argument('inputfile', type=str)
parser.add_argument('outputfile', type=str)
db_check()
args = parser.parse_args(sys.argv[2:])
# Find the functions. Returns list of functions in the inputfile
functions = find_functions(args.inputfile)
if functions == "0":
exit()
# Generate random strings. This is where substitutions{} is populated. Returns list. eg. [original_function, obfuscated_function]
obfuscated_functions = create_obfuscated_functions(functions)
obfuscated_file = write_functions_to_file(args.inputfile, args.outputfile, obfuscated_functions)
remove_comments(args.outputfile)
if obfuscated_file:
print("\n" + dashline)
print('{:<30s}{:>30s}'.format("Original Function","Obfuscated Function"))
print(dashline)
for functions in obfuscated_functions.items():
print('{:<30}{:>25}'.format(functions[0],functions[1]))
save_db = save_functions(args.outputfile, obfuscated_functions)
if save_db:
print("\n")
print(color("[+] Saving to db file"))
print(color("[+] Done. Output file located at " + args.outputfile))
def list(self):
parser = argparse.ArgumentParser(description='List all obfuscated files and their commands.')
if not os.path.exists(db_name):
print(color("[-] No functions or files have been obfuscated."))
sys.exit()
else:
try:
conn = sqlite3.connect(db_name)
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
cursor.execute("SELECT * FROM Files")
file_rows = cursor.fetchall()
for filename in file_rows:
print(color('[*] Obfuscated File: ' + filename['FILENAME']))
print('{:<30s}{:>30s}'.format("Original Function","Obfuscated Function"))
print(dashline)
cursor.execute("SELECT * FROM Functions WHERE FILE_NAME = :filename", {"filename": filename['FILENAME']})
function_rows = cursor.fetchall()
for f in function_rows:
print('{:<30}{:>25}'.format(f['OG_FUNCTION'],f['OB_FUNCTION']))
print(dashline+"\n")
print(color("[+] Done listing files. Over and out."))
except OSError as e:
errno, strerror = e.args
print(color("[-] I/O error ({}): {}".format(errno, strerror)))
except: #handle other exceptions such as attribute errors
print(color("[-] Unexpected error opening db file: " + str(sys.exc_info())))
pass
def getcommand(self):
parser = argparse.ArgumentParser(description='Get a particular command from an obfuscated file.')
#parser.add_argument('file', type=str, help='The name of the obfuscated file.')
parser.add_argument('scriptcommand', type=str, help='The name of the command from the original script that you would like the obfuscated equivalent.')
args = parser.parse_args(sys.argv[2:])
conn = sqlite3.connect(db_name)
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
cursor.execute("SELECT FILE_NAME, OG_FUNCTION, OB_FUNCTION FROM Functions WHERE OG_FUNCTION LIKE :function",{"function":args.scriptcommand})
rows = cursor.fetchall()
if len(rows) == 0:
print(color("[-] No commands found. Sorry bub."))
else:
print(color("[*] " + str(len(rows)) + " Command(s) Found"))
print('{:<25s}{:<25s}{:>25s}'.format("Obfuscated File", "Original Function","Obfuscated Function"))
print(dashline)
for f in rows:
print('{:25s}{:<25s}{:>25s}'.format(f['FILE_NAME'],f['OG_FUNCTION'],f['OB_FUNCTION']))
# Helper command to clear the database
def cleardb(self):
try:
if os.path.exists("powerob.db"):
os.remove("powerob.db")
print(color("[+] Db cleared. See yaaaaaa"))
except OSError as e:
errno, strerror = e.args
print(color("[-] Error clearing db: " + strerror))
# Debug function
def showdb(self):
conn = sqlite3.connect(db_name)
cursor = conn.cursor()
cursor.execute("SELECT * FROM Functions")
print(cursor.fetchall())
if __name__ == '__main__':
banner()
python_check()
PowerOb()