forked from Neo23x0/yarGen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyargendb.py
150 lines (129 loc) · 4.5 KB
/
yargendb.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
#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-
# -*- coding: utf-8 -*-
#
# yarGen DB
#
# Florian Roth
import traceback
import sqlite3 as lite
class YargenDB:
def __init__(self, string_db, opcode_db):
# Initialize databases
try:
self.string_con = lite.connect(string_db)
self.opcode_con = lite.connect(opcode_db)
except lite.Error, e:
if self.string_con:
self.string_con.rollback()
if self.opcode_con:
self.opcode_con.rollback()
traceback.print_exc()
def clear_dbs(self):
# String DB
self.clear_db(self.string_con)
self.clear_db(self.opcode_con)
def clear_db(self, db):
try:
with db:
cur = db.cursor()
cur.execute("DROP TABLE IF EXISTS Stats")
cur.execute("CREATE TABLE Stats(Value TEXT, Count INT)")
db.commit()
except lite.Error, e:
if db:
db.rollback()
traceback.print_exc()
def update_string_db(self, objects):
count = self.get_table_count(self.string_con)
print "[+] Updating string database with count %s ..." % count
self.update_db(objects, self.string_con)
count = self.get_table_count(self.string_con)
print "[+] Updated string database to count %s" % count
def update_opcode_db(self, objects):
count = self.get_table_count(self.opcode_con)
print "[+] Updating opcode database with count %s ..." % count
self.update_db(objects, self.opcode_con)
count = self.get_table_count(self.opcode_con)
print "[+] Updated opcode database to count %s" % count
def update_db(self, objects, db):
try:
with db:
cur = db.cursor()
# UPDATE
cur.executemany("INSERT INTO Stats VALUES (?,?);", objects.iteritems())
db.commit()
except lite.Error, e:
if db:
db.rollback()
traceback.print_exc()
def is_good_string(self, string):
return self.is_value_good(string, self.string_con)
def is_good_opcode(self, opcode):
return self.is_value_good(opcode, self.opcode_con)
def get_string_count(self, string):
return self.get_value_count(string, self.string_con)
def get_opcode_count(self, opcode):
return self.get_value_count(opcode, self.opcode_con)
def get_table_count(self, db):
try:
with db:
cur = db.cursor()
cur.execute("SELECT count(*) FROM Stats")
row = cur.fetchone()
if row is None:
return 0
else:
return row[0]
except lite.Error, e:
if db:
db.rollback()
traceback.print_exc()
def is_value_good(self, value, db):
try:
with db:
cur = db.cursor()
cur.execute("SELECT count(*) FROM Stats WHERE Value = ?", (value,))
if cur.rowcount > 0:
return 1
else:
return 0
except lite.Error, e:
if db:
db.rollback()
traceback.print_exc()
def get_value_count(self, value, db):
try:
with db:
cur = db.cursor()
cur.execute("SELECT sum(Count) FROM Stats WHERE Value = ?;", (value,))
row = cur.fetchone()
if row[0] is None:
return 0
else:
return row[0]
except lite.Error, e:
if db:
db.rollback()
traceback.print_exc()
def get_good_string_matches(self, strings):
return self.get_good_matches(strings, self.string_con)
def get_good_opcode_matches(self, opcodes):
return self.get_good_matches(strings, self.opcode_con)
def get_good_matches(self, values, db):
good_values = {}
try:
with db:
cur = db.cursor()
cur.execute("SELECT * FROM Stats;")
while True:
row = cur.fetchone()
if row == None:
break
if row[0] in values:
good_values[row[0]] = row[1]
return good_values
except lite.Error, e:
if db:
db.rollback()
traceback.print_exc()