-
Notifications
You must be signed in to change notification settings - Fork 1
/
postgresql.py
323 lines (262 loc) · 9.32 KB
/
postgresql.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
#!/usr/bin/python
from task import tasks, subtasks, Task
from sql import SQL, tables
from id import get_binary_id, get_binary_name, get_package_id, get_package_name
from utils import get_config
import binary
import os
import sys
import re
import psycopg2
class PostgreSQL(SQL):
def __init__(self):
SQL.__init__(self)
self.hostname = get_config('postgresql_host', 'localhost')
self.port = get_config('postgresql_port', '5432')
self.username = get_config('postgresql_user', 'postgres')
self.password = get_config('postgresql_pass', 'postgres')
self.dbname = get_config('postgresql_db', 'syscall_popularity')
self.db = None
self.tables = []
def __del__(self):
self.disconnect()
def connect(self):
self.db = psycopg2.connect(database=self.dbname, host=self.hostname, port=self.port, user=self.username, password=self.password)
def disconnect(self):
if self.db:
self.db.close()
self.db = None
def commit(self):
self.db.commit()
def postgresql_execute(self, query):
cur = self.db.cursor()
try:
cur.execute(query)
except psycopg2.Error as err:
cur.close()
self.db.rollback()
raise err
cur.close()
def postgresql_query(self, query):
cur = self.db.cursor()
try:
cur.execute(query)
results = cur.fetchall()
except psycopg2.Error as err:
cur.close()
self.db.rollback()
raise err
cur.close()
return results
def connect_table(self, table):
if table not in self.tables:
query = 'SELECT relname FROM pg_class WHERE relname=\'' + table.name + '\''
create_query = table.create_table()
retry = 10
while not self.postgresql_query(query):
try:
self.postgresql_execute(create_query)
for q in table.create_indexes():
self.postgresql_execute(q)
self.db.commit()
except:
retry = retry - 1
if retry == 0:
raise
continue
break
self.tables.append(table)
def append_record(self, table, values):
self.postgresql_execute(table.insert_record(values))
def search_record(self, table, condition=None, fields=None):
return self.postgresql_query(table.select_record(condition, fields))
def delete_record(self, table, condition=None):
self.postgresql_execute(table.delete_record(condition))
def update_record(self, table, values, condition=None):
self.postgresql_execute(table.update_record(values, condition))
def hash_text(self, text):
return self.postgresql_query('SELECT hashtext(\'' + text + '\')')[0][0]
def AnalyzeLibrary(jmgr, os_target, sql, args):
pkg_name = args[0]
bin = args[1]
if args[2]:
pkg_id = args[2]
else:
pkg_id = get_package_id(sql, pkg_name)
if args[3]:
bin_id = args[3]
else:
bin_id = get_binary_id(sql, bin)
sql.postgresql_execute('SELECT analyze_library(%d, %d)' % (pkg_id, bin_id))
sql.commit()
subtasks['PostgresqlAnalyzeLibrary'] = Task(
name = "Analyze Library by PostgreSQL",
func = AnalyzeLibrary,
arg_defs = ["Package Name", "Binary Name"],
job_name = lambda args: "Analyze Library: " + args[1] + " in " + args[0])
def AnalyzeAllLibraries(jmgr, os_target, sql, args):
sql.connect_table(tables['binary_list'])
results = sql.search_record(tables['binary_list'], 'callgraph=false AND type=\'lib\'', ['pkg_id', 'bin_id'])
for r in results:
values = r[0][1:-1].split(',')
pkg_id = int(values[0])
bin_id = int(values[1])
pkg_name = get_package_name(sql, pkg_id)
bin = get_binary_name(sql, bin_id)
if pkg_name and bin:
subtasks['PostgresqlAnalyzeLibrary'].create_job(jmgr, [pkg_name, bin, pkg_id, bin_id]);
tasks['PostgresqlAnalyzeAllLibraries'] = Task(
name="Analyze All Libaries by PostgreSQL",
func=AnalyzeAllLibraries,
order = 30)
def AnalyzeLinking(jmgr, os_target, sql, args):
pkg_name = args[0]
if len(args) > 1:
pkg_id = args[1]
else:
pkg_id = get_package_id(sql, pkg_name)
sql.postgresql_execute('SELECT analyze_linking(%d, lnk_id, true, false) FROM binary_link WHERE pkg_id=%d AND linking=false' % (pkg_id, pkg_id))
sql.postgresql_execute('SELECT analyze_linking(%d, bin_id, false, false) FROM binary_list WHERE pkg_id=%d AND linking=false and type != \'scr\'' % (pkg_id, pkg_id))
sql.commit()
subtasks['PostgresqlAnalyzeLinking'] = Task(
name = "Analyze Linking by PostgreSQL",
func = AnalyzeLinking,
arg_defs = ["package_name"],
job_name = lambda args: "Analyze Linking: " + args[0])
def AnalyzeAllLinking(jmgr, os_target, sql, args):
sql.connect_table(tables['binary_list'])
sql.connect_table(tables['binary_link'])
results = sql.postgresql_query(
'SELECT DISTINCT pkg_id FROM binary_link ' +
'WHERE linking=false ' +
'UNION SELECT DISTINCT pkg_id FROM binary_list ' +
'WHERE linking=false AND type != \'scr\'')
for r in results:
pkg_id = int(r[0])
pkg_name = get_package_name(sql, pkg_id)
if pkg_name:
subtasks['PostgresqlAnalyzeLinking'].create_job(jmgr, [pkg_name, pkg_id]);
tasks['PostgresqlAnalyzeAllLinking'] = Task(
name = "Analyze All Linking by PostgreSQL",
func = AnalyzeAllLinking,
order = 31)
def AnalyzeExecutable(jmgr, os_target, sql, args):
pkg_name = args[0]
bin = args[1]
if len(args) > 2:
pkg_id = args[2]
else:
pkg_id = get_package_id(sql, pkg_name)
if len(args) > 3:
bin_id = args[3]
else:
bin_id = get_binary_id(sql, bin)
sql.postgresql_execute('SELECT analyze_executable(%d, %d)' % (pkg_id, bin_id))
sql.commit()
subtasks['PostgresqlAnalyzeExecutable'] = Task(
name = "Analyze Executable by PostgreSQL",
func = AnalyzeExecutable,
arg_defs = ["Package Name", "Binary Name"],
job_name = lambda args: "Analyze Executable: " + args[1] + " in " + args[0])
def AnalyzeAllExecutables(jmgr, os_target, sql, args):
sql.connect_table(tables['binary_list'])
results = sql.search_record(tables['binary_list'],
'callgraph=false AND type=\'exe\'', ['pkg_id', 'bin_id'])
for r in results:
values = r[0][1:-1].split(',')
pkg_id = int(values[0])
bin_id = int(values[1])
pkg_name = get_package_name(sql, pkg_id)
bin = get_binary_name(sql, bin_id)
if pkg_name and bin:
subtasks['PostgresqlAnalyzeExecutable'] \
.create_job(jmgr, [pkg_name, bin, pkg_id, bin_id]);
tasks['PostgresqlAnalyzeAllExecutables'] = Task(
name = "Analyze All Executables by PostgreSQL",
func = AnalyzeAllExecutables,
order = 32)
def AnalyzePackage(jmgr, os_target, sql, args):
pkg_name = args[0]
if len(args) > 1:
pkg_id = args[1]
else:
pkg_id = get_package_id(sql, pkg_name)
sql.postgresql_execute('SELECT analyze_package(%d)' % (pkg_id))
sql.commit()
subtasks['PostgresqlAnalyzePackage'] = Task(
name = "Analyze Package by PostgreSQL",
func = AnalyzePackage,
arg_defs = ["Package Name"],
job_name = lambda args: "Analyze Package: " + args[0])
def AnalyzeAllPackages(jmgr, os_target, sql, args):
sql.connect_table(tables['package_id'])
results = sql.search_record(tables['package_id'], 'footprint=false', ['id'])
for r in results:
pkg_id = r[0]
pkg_name = get_package_name(sql, pkg_id)
if pkg_name:
subtasks['PostgresqlAnalyzePackage'].create_job(jmgr, [pkg_name, pkg_id]);
tasks['PostgresqlAnalyzeAllPackages'] = Task(
name = "Analyze All Packages by PostgreSQL",
func = AnalyzeAllPackages,
order = 33)
def AnalyzeExecutableSource(jmgr, os_target, sql, args):
pkg_name = args[0]
bin = args[1]
if len(args) > 2:
pkg_id = args[2]
else:
pkg_id = get_package_id(sql, pkg_name)
if len(args) > 3:
bin_id = args[3]
else:
bin_id = get_binary_id(sql, bin)
sql.postgresql_execute('SELECT analyze_executable_source(%d, %d)' % (pkg_id, bin_id))
sql.commit()
subtasks['PostgresqlAnalyzeExecutableSource'] = Task(
name = "Source of Opcodes in Executables",
func = AnalyzeExecutableSource,
arg_defs = ["Package Name", "Binary Name"],
job_name = lambda args: "Analyze Executable Source: " + args[1] + " in " + args[0])
def AnalyzeAllExecutablesSources(jmgr, os_target, sql, args):
sql.connect_table(tables['binary_list'])
results = sql.search_record(tables['binary_list'],
'callgraph=false AND type=\'exe\'', ['pkg_id', 'bin_id'])
for r in results:
values = r[0][1:-1].split(',')
pkg_id = int(values[0])
bin_id = int(values[1])
pkg_name = get_package_name(sql, pkg_id)
bin_name = get_binary_name(sql, bin_id)
if pkg_name and bin_name:
subtasks['PostgresqlAnalyzeExecutableSource'] \
.create_job(jmgr, [pkg_name, bin_name, pkg_id, bin_id]);
tasks['PostgresqlAnalyzeAllExecutablesSources'] = Task(
name = "Analyze Sources of Opcodes in all Executables by PostgreSQL",
func = AnalyzeAllExecutablesSources,
order = 35)
def AnalyzePackageSource(jmgr, os_target, sql, args):
pkg_name = args[0]
if len(args) > 1:
pkg_id = args[1]
else:
pkg_id = get_package_id(sql, pkg_name)
sql.postgresql_execute('SELECT analyze_package_source(%d)' % (pkg_id))
sql.commit()
subtasks['PostgresqlAnalyzePackageSource'] = Task(
name = "Analyze Opcode Sources in a Package",
func = AnalyzePackageSource,
arg_defs = ["Package Name"],
job_name = lambda args: "Analyze Package Source: " + args[0])
def AnalyzeAllPackagesSources(jmgr, os_target, sql, args):
sql.connect_table(tables['package_id'])
results = sql.search_record(tables['package_id'], 'footprint=false', ['id'])
for r in results:
pkg_id = r[0]
pkg_name = get_package_name(sql, pkg_id)
if pkg_name:
subtasks['PostgresqlAnalyzePackageSource'].create_job(jmgr, [pkg_name, pkg_id]);
tasks['PostgresqlAnalyzeAllPackagesSources'] = Task(
name = "Analyze Sources in All Packages by PostgreSQL",
func = AnalyzeAllPackagesSources,
order = 36)