Skip to content

Commit 971dd0c

Browse files
committed
Convert vectors from class-based to object-based
So init methods could be useful. Needed this in the basicblocks vector
1 parent c45f9b4 commit 971dd0c

11 files changed

+48
-42
lines changed

idaplugin/rematch/collectors/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
def collect(collectors, offset, instance_id=None):
1010
for collector in collectors:
1111
try:
12-
r = collector.collect(offset, instance_id)
12+
collector_obj = collector(offset, instance_id)
13+
r = collector_obj.collect()
1314
if r:
1415
yield r
1516
except UnicodeDecodeError:

idaplugin/rematch/collectors/vectors/assembly_hash.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ class AssemblyHashVector(vector.Vector):
1010
type = 'assembly_hash'
1111
type_version = 0
1212

13-
@classmethod
14-
def data(cls, offset):
15-
if cls.inst_count(offset) < 3:
13+
def data(self):
14+
if self.inst_count() < 3:
1615
return None
1716

1817
md5 = hashlib.md5()
19-
for ea in idautils.FuncItems(offset):
18+
for ea in idautils.FuncItems(self.offset):
2019
asm_line = idc.GetDisasmEx(ea, idc.GENDSM_MULTI_LINE)
2120
if ';' in asm_line:
2221
asm_line = asm_line[:asm_line.find(';')]

idaplugin/rematch/collectors/vectors/basicblock.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ def __init__(self, *args, **kwargs):
1818

1919
self.seen_nodes = set()
2020

21-
def data(self, offset):
22-
del offset
21+
def data(self):
2322

2423
# Assuming node #0 is the root node
2524
serialized_bbs = self.add_node(self.nodes[0])
@@ -48,7 +47,7 @@ def token(node):
4847
# alternatives: offset from start of function
4948
return node.endEA - node.startEA
5049

51-
sort_ket = token
50+
sort_key = token
5251

5352
def node_contained(self, node):
5453
# make sure only nodes inside the function are accounted for

idaplugin/rematch/collectors/vectors/identity_hash.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,16 @@ def _cycle(h, b):
2121
h &= 0xffffffffffffffff
2222
return h
2323

24-
@classmethod
25-
def data(cls, offset):
26-
if cls.inst_count(offset) < 3:
24+
def data(self):
25+
if self.inst_count() < 3:
2726
return None
2827

29-
h = cls.keleven
30-
for ea in idautils.FuncItems(offset):
31-
h = cls._cycle(h, idc.Byte(ea))
28+
h = self.keleven
29+
for ea in idautils.FuncItems(self.offset):
30+
h = self._cycle(h, idc.Byte(ea))
3231
# skip additional bytes of any instruction that contains an offset in it
3332
if idautils.CodeRefsFrom(ea, False) or idautils.DataRefsFrom(ea):
3433
continue
3534
for i in range(ea + 1, ea + idc.ItemSize(ea)):
36-
h = cls._cycle(h, idc.Byte(i))
35+
h = self._cycle(h, idc.Byte(i))
3736
return h

idaplugin/rematch/collectors/vectors/instruction_hash.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,14 @@ def _cycle(h, b):
2121
h &= 0xffffffffffffffff
2222
return h
2323

24-
@classmethod
25-
def data(cls, offset):
26-
if cls.inst_count(offset) < 3:
24+
def data(self):
25+
if self.inst_count() < 3:
2726
return None
2827

29-
h = cls.keleven
30-
for ea in idautils.FuncItems(offset):
31-
h = cls._cycle(h, idc.Byte(ea))
28+
h = self.keleven
29+
for ea in idautils.FuncItems(self.offset):
30+
h = self._cycle(h, idc.Byte(ea))
3231
# go over all additional bytes of any instruction
3332
for i in range(ea + 1, ea + idc.ItemSize(ea)):
34-
h = cls._cycle(h, idc.Byte(i))
33+
h = self._cycle(h, idc.Byte(i))
3534
return h

idaplugin/rematch/collectors/vectors/mnemonic_hash.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ class MnemonicHashVector(vector.Vector):
1010
type = 'mnemonic_hash'
1111
type_version = 0
1212

13-
@classmethod
14-
def data(cls, offset):
15-
if cls.inst_count(offset) < 3:
13+
def data(self):
14+
if self.inst_count() < 3:
1615
return None
1716

1817
md5 = hashlib.md5()
19-
for ea in idautils.FuncItems(offset):
18+
for ea in idautils.FuncItems(self.offset):
2019
mnem_line = idc.GetMnem(ea)
2120
mnem_line = mnem_line.strip()
2221
mnem_line = mnem_line.lower()

idaplugin/rematch/collectors/vectors/mnemonic_hist.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ class MnemonicHistVector(vector.Vector):
1010
type = 'mnemonic_hist'
1111
type_version = 0
1212

13-
@classmethod
14-
def data(cls, offset):
13+
def data(self):
1514
instruction_hist = defaultdict(int)
1615

17-
for ea in idautils.FuncItems(offset):
16+
for ea in idautils.FuncItems(self.offset):
1817
mnem_line = idc.GetMnem(ea)
1918
mnem_line = mnem_line.lower()
2019
instruction_hist[mnem_line] += 1

idaplugin/rematch/collectors/vectors/name_hash.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ class NameHashVector(vector.Vector):
1010
type = 'name_hash'
1111
type_version = 0
1212

13-
@classmethod
14-
def data(cls, offset):
15-
name = idc.Name(offset)
13+
def data(self):
14+
name = idc.Name(self.offset)
1615
if ida_name.is_uname(name):
1716
return None
1817

idaplugin/rematch/collectors/vectors/vector.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44

55

66
class Vector(object):
7-
@classmethod
8-
def collect(cls, offset, instance_id=None):
9-
data = cls.data(offset)
7+
def __init__(self, offset, instance_id=None):
8+
self.offset = offset
9+
self.instance_id = instance_id
10+
11+
def collect(self):
12+
data = self.data()
1013
if not data:
1114
return None
1215

1316
data = json.dumps(data)
14-
return {"instance": instance_id, "type": cls.type,
15-
"type_version": cls.type_version, "data": data}
17+
return {"instance": self.instance_id, "type": self.type,
18+
"type_version": self.type_version, "data": data}
1619

17-
@staticmethod
18-
def inst_count(offset):
19-
return len(list(idautils.FuncItems(offset)))
20+
def inst_count(self):
21+
return len(list(idautils.FuncItems(self.offset)))

server/collab/matchers/__init__.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@
88
from .name_hash import NameHashMatcher
99
from .mnemonic_euclidean import MnemonicEuclideanMatcher
1010
from .dictionary_matcher import DictionaryMatcher
11+
from .basicblock import BasicBlockMatcher
1112

1213

1314
matchers_list = [InstructionHashMatcher, IdentityHashMatcher, NameHashMatcher,
1415
AssemblyHashMatcher, MnemonicHashMatcher,
15-
MnemonicEuclideanMatcher]
16+
MnemonicEuclideanMatcher, BasicBlockMatcher]
1617

1718

1819
__all__ = ['Matcher', 'HashMatcher', 'EuclideanDictionaryMatcher',
1920
'InstructionHashMatcher', 'IdentityHashMatcher',
2021
'AssemblyHashMatcher', 'MnemonicHashMatcher', 'NameHashMatcher',
21-
'MnemonicEuclideanMatcher', 'DictionaryMatcher', 'matchers_list']
22+
'MnemonicEuclideanMatcher', 'DictionaryMatcher',
23+
'BasicBlockMatcher', 'matchers_list']

server/collab/matchers/basicblock.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from . import matcher
2+
3+
4+
class BasicBlockMatcher(matcher.Matcher):
5+
vector_type = 'basicblock'
6+
match_type = 'basicblock'
7+
matcher_name = "Basic Block"
8+
matcher_description = ("TODO")

0 commit comments

Comments
 (0)