Repository navigation
Expand file tree
/
Copy pathshcode2exe.py
More file actions
125 lines (105 loc) · 4.43 KB
/
Copy pathshcode2exe.py
File metadata and controls
125 lines (105 loc) · 4.43 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
#!/usr/bin/env python3
import os
import shutil
import subprocess
from argparse import ArgumentParser
def CheckRequirementsMet(arg_vars):
requirements = ['ld','nasm']
for prog in requirements:
if shutil.which(prog) is None:
if prog == 'ld':
print("{} is not installed or found. Ensure it is installed (e.g. 'sudo apt install binutils') and in your PATH and try again.".format(prog))
elif prog == 'nasm':
print("{} is not installed or found. Ensure it is installed (e.g 'sudo apt install nasm') and in your PATH and try again.".format(prog))
else:
print("Unmatched or unidentified requirements")
raise SystemExit(1)
CompileShellCode(arg_vars)
def ConvertToBin(file_input, filename):
with open(file_input, 'r', encoding='unicode_escape') as input_file:
s = input_file.read().replace('\n', '')
with open(filename + '.bin', 'wb') as gen_file:
gen_file.write(b'' + bytes(s, encoding='raw_unicode_escape'))
file_input = filename + '.bin'
input_file.close()
gen_file.close()
return file_input
def CompileShellCode(arguments):
if arguments['output']:
filename = os.path.basename(arguments['output']).split('.')[0]
else:
filename = 'output'
file_input = arguments['input']
if file_input and not os.path.exists(file_input):
print('ERROR: File {} does not exist!'.format(file_input))
raise SystemExit(1)
if arguments['string']:
file_input = ConvertToBin(file_input, filename + '-gen')
if arguments['verbose']:
print("Converting input file to {}-gen.bin".format(filename))
asm_file_contents = '\tglobal WinMainCRTStartup\n' \
'\tsection .text\n' \
'WinMainCRTStartup:\n' \
'\tincbin "' + file_input + '"\n' \
'\tmov eax, 0x0\n' \
'\tret\n'
if arguments['verbose']:
print("Writing assembly instruction to {}.asm".format(filename))
with open(filename + '.asm', 'w+') as f:
f.write(asm_file_contents)
nasm_bin = 'nasm -f win' + arguments['architecture'] + ' -o ' + filename + '.obj ' + filename + '.asm'
if arguments['verbose']:
print("Executing: {}".format(nasm_bin))
subprocess.check_output(nasm_bin, shell=True)
ld_bin = 'ld'
if arguments['architecture'] == '32':
ld_bin = ld_bin + ' -m i386pe -o '
elif arguments['architecture'] == '64':
ld_bin = ld_bin + ' -m i386pep -o '
if arguments['output']:
ld_bin += arguments['output']
else:
ld_bin += filename + '.exe'
ld_bin += ' -e WinMainCRTStartup -subsystem windows'
ld_bin += ' ' + filename + '.obj'
if arguments['verbose']:
print("Executing: {}".format(ld_bin))
subprocess.check_output(ld_bin, shell=True)
if arguments['verbose']:
print("Compiled shellcode saved as {}".format(filename))
if not arguments['keep']:
if arguments['verbose']:
print("Attempting to remove {0}.obj, {0}.asm, and {0}-gen.bin (if present)".format(filename))
os.remove(filename + '.obj')
os.remove(filename + '.asm')
if os.path.exists(filename + '-gen.bin'):
os.remove(filename + '-gen.bin')
def main():
parser = ArgumentParser(description='Compile shellcode into an exe file from Windows or Linux.')
parser.add_argument('-o',
'--output',
help='Set output exe file.')
parser.add_argument('-s',
'--string',
action='store_true',
help='Set if input file contains shellcode in string format.')
parser.add_argument('-a',
'--architecture',
choices=['32', '64'],
default = '32',
help='The windows architecture to use')
parser.add_argument('-k',
'--keep',
action='store_true',
help='Keep files used in compilation')
parser.add_argument('-V',
'--verbose',
action='store_true',
help='Print actions to stdout')
parser.add_argument('input',
help='The input file containing the shellcode.')
args = parser.parse_args()
arg_vars = vars(args)
CheckRequirementsMet(arg_vars)
if __name__ == '__main__':
main()