-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathdb.py
executable file
·235 lines (214 loc) · 8.13 KB
/
db.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
#!/usr/bin/env python
# coding: utf-8
# site : beebeeto.com
# team : n0tr00t security
import os
import sys
import sqlite3 as sq3
import collections as clt
try:
import simplejson as json
except ImportError:
import json
from poc import Poc
class Database(object):
tables = {
'poc': [
'id',
'name',
'rank',
'level',
'author',
'create_date',
'protocol',
'port',
'layer4_protocol',
'app_name',
'vul_type',
'tag',
'desc',
'batchable',
'path',
],
}
def __init__(self, dbFilePath='./hive.db', pocDir='./pocs'):
self.dbConn = sq3.connect(dbFilePath)
self.pocDir = pocDir
self.cursor = self.dbConn.cursor()
def updtDbFromPocs(self, pocDir='../pocs/'):
'''
Update local sqlite database according to the
poc_info in the pocs' source code online database.
'''
updatedNum = 0
insertedNum = 0
errNum = 0
errPocs = []
for pocFileName in os.listdir(pocDir):
if pocFileName.startswith('poc') and pocFileName.endswith('py'):
try:
pocInfo = Poc(os.path.join(pocDir, pocFileName),
batchable=False).module.MyPoc.poc_info
status = self.__updtFromPocInfo(pocInfo)
if status == 'inserted':
insertedNum += 1
elif status == 'updated':
updatedNum += 1
except Exception, err:
errNum += 1
errPocs.append('%s:%s...' % (pocFileName, str(err)[:50]))
self.dbConn.commit()
return (insertedNum, updatedNum, errNum, errPocs)
def __updtFromPocInfo(self, pocInfo):
self.cursor.execute('SELECT * FROM poc WHERE id=?',
(pocInfo['poc']['id'],))
pocPath = os.path.join(self.pocDir,
'%s.py' % pocInfo['poc']['id'].replace('-', '_'))
if not self.cursor.fetchone():
args = [
pocInfo['poc']['id'],
pocInfo['poc']['name'].decode('utf-8', 'ignore'),
None,
None,
pocInfo['poc']['author'].decode('utf-8', 'ignore'),
pocInfo['poc']['create_date'],
pocInfo['protocol']['name'],
'%s' % ','.join([str(i) for i in pocInfo['protocol']['port']]),
'%s' % ','.join(pocInfo['protocol']['layer4_protocol']),
pocInfo['vul']['app_name'].decode('utf-8', 'ignore'),
pocInfo['vul']['type'],
'%s' % ','.join([i.decode('utf-8', 'ignore') for i in pocInfo['vul']['tag']]),
pocInfo['vul']['desc'].decode('utf-8', 'ignore'),
None,
pocPath if os.path.exists(pocPath) else None,
]
sql = 'INSERT INTO poc VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)'
self.cursor.execute(sql, args)
return 'inserted'
else:
args = [
pocInfo['poc']['name'].decode('utf-8', 'ignore'),
pocInfo['poc']['author'].decode('utf-8', 'ignore'),
pocInfo['poc']['create_date'],
pocInfo['protocol']['name'],
'%s' % ','.join([str(i) for i in pocInfo['protocol']['port']]),
'%s' % ','.join(pocInfo['protocol']['layer4_protocol']),
pocInfo['vul']['app_name'].decode('utf-8', 'ignore'),
pocInfo['vul']['type'],
'%s' % ','.join([i.decode('utf-8', 'ignore') for i in pocInfo['vul']['tag']]),
pocInfo['vul']['desc'].decode('utf-8', 'ignore'),
pocPath if os.path.exists(pocPath) else None,
pocInfo['poc']['id'],
]
sql = 'UPDATE poc SET name=?, author=?, create_date=?, '\
'protocol=?, port=?, layer4_protocol=?, app_name=?, '\
'vul_type=?, tag=?, desc=?, path=? WHERE id=?'
self.cursor.execute(sql, args)
return 'updated'
def updtDbFromJson(self, jsonFile):
'''
Update local sqlite database according to the
dumped json file export from beebeeto.com.
'''
updatedNum = 0
insertedNum = 0
errNum = 0
errPocs = []
f = open(jsonFile, 'rbU')
for row in f:
try:
status = self.__updtFromJsonRow(
dictRow=json.loads(row.strip())
)
if status == 'inserted':
insertedNum += 1
elif status == 'updated':
updatedNum += 1
except Exception, err:
errNum += 1
errPocs.append('%s:%s...' % (row.strip(), str(err)[:50]))
self.dbConn.commit()
f.close()
return (insertedNum, updatedNum, errNum, errPocs)
def __updtFromJsonRow(self, dictRow):
pocId = dictRow.get('id')
if not pocId:
return
pocPath = os.path.join(self.pocDir,
'%s.py' % pocId.replace('-', '_'))
pocFile = open(pocPath, 'wb')
pocFile.write(dictRow.get('source_code').encode('utf-8', 'ignore'))
pocFile.close()
dbMapping = [
('id', pocId, ),
('name', dictRow.get('name'), ),
('rank', dictRow.get('rank'), ) ,
('level', dictRow.get('level'), ),
('author', dictRow.get('author'),),
('create_date', dictRow.get('create_date'), ),
('protocol', None, ),
('port', None, ),
('layer4_protocol', None, ),
('app_name', dictRow.get('app_name'), ),
('vul_type', dictRow.get('vul_type'), ),
('tag', dictRow.get('tag'), ),
('desc', dictRow.get('desc'), ),
('batchable', dictRow.get('batchable'), ),
('path', pocPath if os.path.exists(pocPath) else None, ),
]
self.cursor.execute('SELECT * FROM poc WHERE id=?',(pocId,))
if not self.cursor.fetchone():
sql = ' '.join([
'INSERT INTO poc VALUES',
'(%s)' % ','.join('?' * len(dbMapping)),
])
self.cursor.execute(sql, map(lambda i: i[1], dbMapping))
return 'inserted'
else:
sql1 = 'UPDATE poc SET'
sql2 = ', '.join(['%s=?' % i[0] for i in dbMapping[1:] if i[1] is not None])
sql3 = 'WHERE id=?'
sql = ' '.join([sql1, sql2, sql3])
args = [i[1] for i in dbMapping[1:] if i[1] is not None]
args.append(pocId)
self.cursor.execute(sql, args)
return 'updated'
def searchStr(self, item):
columns = [
'name',
'app_name',
'tag',
'desc',
]
sql = 'SELECT * FROM poc WHERE ' + ' or '.join(
['LOWER(%s) like "%%%s%%"' % (col, item.lower()) for col in columns]
)
self.cursor.execute(sql)
return self.cursor.fetchall()
def searchPoc(self, pocId):
sql = 'SELECT * FROM poc WHERE id=?'
self.cursor.execute(sql, (pocId,))
return self.cursor.fetchone()
def getBatchable(self):
sql = 'SELECT * FROM poc WHERE batchable=1'
self.cursor.execute(sql)
return self.cursor.fetchall()
def countAll(self):
sql = 'SELECT count(*) FROM poc'
self.cursor.execute(sql)
return self.cursor.fetchone()
if __name__ == '__main__':
# testing code
sys.path.append('../')
from SETTINGS import FRAMEWORK_DIR
sys.path.append(FRAMEWORK_DIR)
from pprint import pprint as pr
db = Database(dbFilePath='../hive.db',
pocDir='../pocs/')
#print db.updtDbFromBB2Db(bb2DbFile='../pocdb.json')
#print db.updtDbFromPocs(pocDir='../pocs')
#pr(db.searchStr(item='discuz'))
#pr(db.countAll())
#pr(db.searchPoc(pocId='poc-2014-0019'))
#pr(db.getBatchable())
pr(db.updtDbFromJson(jsonFile='../pocdb.json'))