-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcore.py
More file actions
143 lines (123 loc) · 5.1 KB
/
core.py
File metadata and controls
143 lines (123 loc) · 5.1 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from capstone import *
from keystone import *
import sys
import os
import stat
import binascii
import datetime
class Arch:
def __init__(self):
self.cs = ""
self.ks = ""
def arch(self, arch_type):
if arch_type == "x86-32":
self.cs = Cs(CS_ARCH_X86, CS_MODE_32)
self.ks = Ks(KS_ARCH_X86, KS_MODE_32)
elif arch_type == "x86-64":
self.cs = Cs(CS_ARCH_X86, CS_MODE_64)
self.ks = Ks(KS_ARCH_X86, KS_MODE_64)
elif arch_type == "arm":
self.cs = Cs(CS_ARCH_ARM, CS_MODE_ARM)
self.ks = Ks(KS_ARCH_ARM, KS_MODE_ARM)
elif arch_type == "arm64":
self.cs = Cs(CS_ARCH_ARM64, CS_MODE_ARM)
self.ks = Ks(KS_ARCH_ARM64, KS_MODE_ARM)
else:
return 1
return 0
class FilesOp:
def __init__(self, binary):
self.binary = binary
self.fd = ""
self.data = ""
def read_file(self):
try:
self.fd = open(self.binary, 'rb')
self.data = self.fd.read()
self.size = self.get_file_size(self.binary)
except Exception as e:
print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
sys.exit()
def disas_from_offset(self, offset):
try:
with open(self.binary, 'rb') as fd:
self.fd.seek(offset, 0)
data_from_offset = self.fd.read()
self.fd.close()
return data_from_offset
except Exception as e:
print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
sys.exit()
def patch_file(self, filename, offset, patch):
try:
with open(filename, 'wb') as fd:
fd.write(self.data)
fd.seek(offset)
fd.write(patch)
fd.close()
self.fd.close()
new_size = self.get_file_size(filename)
if new_size != self.size:
print("[*] The size of the new file {} is different than the one of original file {}".format(new_size, self.size))
statinfo = os.stat(filename)
os.chmod(filename, statinfo.st_mode | stat.S_IEXEC)
except Exception as e:
print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
sys.exit()
print("[*] File {} with patch is created".format(filename))
def get_file_size(self, binary):
try:
statinfo = os.stat(binary)
return statinfo.st_size
except Exception as e:
print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
sys.exit()
def check_file(self):
try:
if not os.path.exists(self.binary):
print("file not found {}".format(self.binary))
sys.exit()
except Exception as e:
print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
sys.exit()
def generate_random(self):
try:
unique_filename = "out" + str(datetime.datetime.now().date()) + '_' + str(datetime.datetime.now().time()).replace(':', '.')
out_file = os.path.join(os.path.abspath(os.path.dirname(__file__)), unique_filename)
return out_file
except Exception as e:
print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
sys.exit()
class Core(Arch):
def __init__(self):
Arch.__init__(self)
def disassemble(self, code, disassembly_len, offset, mode=False):
try:
if mode == False:
code = self.get_bytes(code)
for i in self.cs.disasm(code, offset, disassembly_len):
print("{0:}: {1:16} {2:5} {3:16}".format(hex(i.address), ''.join(format(x, '02x') for x in i.bytes), i.mnemonic, i.op_str))
except (CsError, ValueError, Exception) as e:
print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
def assemble(self, code, mode=False):
try:
encoding, count = self.ks.asm(code)
if count > 0:
if mode:
print("[*] Instructions: {} (len: {})\n[*] Encoding: {} (len: {})".format(code.split(";"), count, ' '.join(hex(x) for x in encoding), len(encoding)))
else:
print("[*] %s = %s (number of statements: %u)" %(code, encoding, count))
return encoding
except (KsError, ValueError, Exception) as e:
print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
def get_bytes(self, code):
try:
code_bytes = b''
code = code.replace(" ", "").replace("0x", "")
code = code.split(",")
for i in code:
code_bytes += binascii.unhexlify(i)
return code_bytes
except Exception as e:
print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
sys.exit()