Repository navigation
Expand file tree
/
Copy pathotoolUtil.py
More file actions
127 lines (92 loc) · 3.63 KB
/
Copy pathotoolUtil.py
File metadata and controls
127 lines (92 loc) · 3.63 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
#author: june
import os
import re
from abstracttool import Tool
import globals as G
########################################################################
class OtoolUtil(Tool):
""""""
#----------------------------------------------------------------------
def __init__(self, binary_path):
"""Constructor"""
self.otool_path = "/usr/bin/otool"
if not os.path.exists(self.otool_path):
self.otool_path = None
G.log(G.INFO, "otool not found.")
self.binary_path = binary_path
self.parse_load_commands()
self.parse_shared_libraries()
self.parse_header()
self.process_symbol_table()
#----------------------------------------------------------------------
def parse_load_commands(self):
""""""
if self.otool_path is None:
G.log(G.INFO, "otool not found.")
return
self.load_cmds = dict()
cmd_load = "{} -l {}".format(self.otool_path, self.binary_path)
buff = self.exec_shell(cmd_load)
pattern_load_cmd = re.compile(r"Load command (\d+)")
pattern_cmd_crypt = re.compile(r"\s*(cmd|cryptid)\s(.+)")
l = buff.split("\n")
for line in l:
match_lc = pattern_load_cmd.match(line)
if match_lc:
command = dict()
self.load_cmds[match_lc.group(1)] = command
continue
match_cc = pattern_cmd_crypt.match(line)
if match_cc:
command[match_cc.group(1)] = match_cc.group(2)
#----------------------------------------------------------------------
def parse_shared_libraries(self):
""""""
self.shared_library = list()
if self.otool_path is None:
G.log(G.INFO, "otool not found.")
return
cmd_sl = "{} -L {}".format(self.otool_path, self.binary_path)
buff = self.exec_shell(cmd_sl)
l = buff.split("\n")
for line in l[1:]:
if line is not None:
self.shared_library.append(line.strip())
#print ""
#----------------------------------------------------------------------
def parse_header(self):
""""""
if self.otool_path is None:
G.log(G.INFO, "otool not found.")
return
cmd = "{} -h {}".format(self.otool_path, self.binary_path)
buff = self.exec_shell(cmd)
pie_flag = 0x00200000
v = buff.split("\n")[3]
r_blank = v.rfind(" ")
if r_blank == -1:
G.log(G.INFO, "Check PIE flag error.")
self.pie = False
return False
flags = int(v[r_blank:], 16)
if flags & pie_flag == pie_flag:
self.pie = True
else:
self.pie = False
return self.pie
#----------------------------------------------------------------------
def process_symbol_table(self):
""""""
if self.otool_path is None:
G.log(G.INFO, "otool not found.")
return
cmd = "{} -I -v {}".format(self.otool_path, self.binary_path)
buff = self.exec_shell(cmd)
if buff.find("stack_chk_fail") != -1 or buff.find("stack_chk_guard") != -1:
self.stack_canaries = True
else:
self.stack_canaries = False
if buff.find("_objc_release") != -1:
self.arc = True
else:
self.arc = False