-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.py
executable file
·148 lines (130 loc) · 5.15 KB
/
main.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
#!/usr/bin/env python
import sys
import os
import signal
import time
from optparse import OptionParser
from elfpatch import elfpatch
from elf_helper import *
from genshellcode import *
def signal_handler(signal, frame):
print '\nProgram Exit'
sys.exit(0)
def basicDiscovery(FILE):
testBinary = open(FILE, 'rb')
header = testBinary.read(4)
testBinary.close()
if 'MZ' in header:
return 'PE'
elif 'ELF' in header:
return 'ELF'
else:
'Only support ELF formats'
return None
MAGIC_PREFIX_INJECT="hook_"
def main():
author = """\
Author: Feng,Li
Email: [email protected]
"""
print author
signal.signal(signal.SIGINT, signal_handler)
parser = OptionParser(usage='%prog [options]',description="inject tool."
"-t -s -I,please choose one,but -I is the best")
parser.add_option("-f", "--file", dest="FILE", action="store",
type="string",
help="[must] File to patch")
parser.add_option("-t", "--object_file", default=False,
dest="OBJECT_FILE", action="store",
help="Get text section and add it to out file")
parser.add_option("-s", "--shellcode", type="string",
dest="SHELLCODE", action="store",
help="User supplied shellcode, place it in text section,"
"make sure that it matches"
" the architecture that you are targeting.")
parser.add_option("-I", "--inject_nasm_file", type="string", action="store",
help="nasm file to inject")
parser.add_option("-o", "--output-file", default=None, dest="OUTPUT",
action="store", type="string",
help="The output file")
""" ---deprecated----"""
parser.add_option("-i", "--inject_ins", type="string",
dest="INJECT_INS", action="store",
help="[deprecated] inject instruction into func's prolog."
"use --inject_nasm_file instead")
""" ---deprecated----"""
parser.add_option("-F", "--func_name", help="[deprecated] where inject."
"using --inject_nasm_file instead")
parser.add_option("-v", "--verbose", default=False, dest="VERBOSE",
action="store_true",
help="For debug information output.")
(options, args) = parser.parse_args()
if not options.FILE or (not options.SHELLCODE and \
not options.OBJECT_FILE and not options.inject_nasm_file):
parser.print_help()
sys.exit(1)
if options.INJECT_INS or options.func_name:
print "option has deprecated!!!"
parser.print_help()
sys.exit(1)
if not options.OUTPUT:
options.OUTPUT = options.FILE + ".patched"
is_supported = basicDiscovery(options.FILE)
if is_supported != 'ELF':
print >>sys.stderr,"[-] file not supported!\n"
sys.exit(1)
"""
parse inject nasm file to a dict
"""
if options.inject_nasm_file:
'''
1. compile the .nasm to .o
2. disassemble .o to get a dict
3. fix the dict's key to our injection pointer
eg: hook_FsOpen ---> FsOpen
'''
options.SHELLCODE = encode(compile_nasm_file(options.inject_nasm_file,"elf"))
inject_o = options.inject_nasm_file.replace(".nasm", ".o")
inject_funcs = parse_text_section(inject_o)
for k in inject_funcs:
if len(k) > 5 and k[0:5] == MAGIC_PREFIX_INJECT:
inject_funcs[k[5:]] = inject_funcs.pop(k)
print inject_funcs
"""
parse elf and patch our shellcode to elf
"""
elfp = elfpatch(options.FILE,
options.OUTPUT,
options.SHELLCODE,
options.OBJECT_FILE)
(entry,inject_point) = elfp.patch_elf()
offset_base = offset_file_vaddr(options.OUTPUT)
print "[+] entry: {0:#x} inject_point: {1:#x} "\
"base offset: {2:#x}".format(entry,inject_point,offset_base)
"""
insert jump instrument at the beginning of patched elf
1. disassemble elf to get a dict.
2. travel the inject_funcs to compute the file offset.
3. inject a 'push xxxx\nret' to inject func point.
"""
funcs = parse_text_section(options.OUTPUT)
for k in inject_funcs:
if not k in funcs:
#print "[*] {0} not exist!".format(k)
continue
offset = int(funcs[k]['offset'],16)
inject_offset = int(inject_funcs[k]['offset'],16)
file_offset = offset - offset_base
jmp_addr = inject_point + inject_offset
prolog_instrument = "push {0:#x}\nret".format(jmp_addr)
pro_shellcode = compile_instruction(prolog_instrument,"elf")
print "[+] prolog_instrument: {0}".format(repr(prolog_instrument))
print "[+] injecting prolog: {0}".format(pro_shellcode)
write_str_elf(options.OUTPUT,file_offset,to_binary_code(pro_shellcode))
print """[+] func: {0}
offset: {1:#x}
file_offset: {2:#x}
jmp_addr: {3:#x}""".format(k,offset, file_offset, jmp_addr)
print "[+] Done! Output: {0}".format(options.OUTPUT)
if __name__ == "__main__":
main()