My favorites | Sign in
Project Home Downloads Wiki Issues Source
Repository:
Checkout   Browse   Changes   Clones  
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
# This file is part of Androguard.
#
# Copyright (C) 2010, Anthony Desnos <desnos at t0t0.org>
# All rights reserved.
#
# Androguard is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Androguard is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Androguard. If not, see <http://www.gnu.org/licenses/>.

import hashlib

from error import error, warning, debug, set_debug, get_debug
from similarity import *

import bytecode

class CheckSumMeth :
def __init__(self, m1, sim) :
code = m1.get_code()
bc = code.get_bc()

buff = ""

for i in bc.get() :
buff += "%s" % i.get_name()
if i.type_ins_tag == 0 :
for op in i.get_operands() :
if "#" in op[0] :
buff += "%s" % op

self.buff = buff
self.entropy = sim.entropy( self.buff )

def get_entropy(self) :
return self.entropy

def get_buff(self) :
return self.buff

def filter_checksum_meth_1( m1, sim ) :
return CheckSumMeth( m1, sim )

def filter_sim_meth_1( m1, m2, sim, name_attribute ) :
a1 = getattr( m1, "checksum_" + name_attribute )
a2 = getattr( m2, "checksum_" + name_attribute )

e1 = a1.get_entropy()
e2 = a2.get_entropy()

ncd = sim.ncd( a1.get_buff(), a2.get_buff() )

return (max(e1, e2) - min(e1, e2)) + ncd

def filter_sim_meth_2( m1, m2, sim, name_attribute ) :
ncd1 = sim.ncd( m1.vmx.get_method_signature( m1.m, 1 ), m2.vmx.get_method_signature( m2.m, 1 ) )

a1 = getattr( m1, "checksum_" + name_attribute )
a2 = getattr( m2, "checksum_" + name_attribute )

ncd2 = sim.ncd( a1.get_buff(), a2.get_buff() )

return (ncd1 + ncd2) / 2.0

def filter_sim_bb_1( bb1, bb2, sim ) :
ncd = sim.ncd( bb1.get_buff(), bb2.get_buff() )

return ncd

class CheckSumBB :
def __init__(self, basic_block) :
self.basic_block = basic_block
self.buff = ""
for i in self.basic_block.ins :
self.buff += i.get_name()

#self.hash = hashlib.sha256( self.buff + "%d%d" % (len(basic_block.childs), len(basic_block.fathers)) ).hexdigest()
self.hash = hashlib.sha256( self.buff ).hexdigest()

def get_buff(self) :
return self.buff

def get_hash(self) :
return self.hash

def filter_checksum_bb_1( basic_block ) :
return CheckSumBB( basic_block )

def LCS(X, Y):
m = len(X)
n = len(Y)
# An (m+1) times (n+1) matrix
C = [[0] * (n+1) for i in range(m+1)]
for i in range(1, m+1):
for j in range(1, n+1):
if X[i-1] == Y[j-1]:
C[i][j] = C[i-1][j-1] + 1
else:
C[i][j] = max(C[i][j-1], C[i-1][j])
return C

def getDiff(C, X, Y, i, j, a, r):
if i > 0 and j > 0 and X[i-1] == Y[j-1]:
getDiff(C, X, Y, i-1, j-1, a, r)
debug(" " + "%02X" % ord(X[i-1]))
else:
if j > 0 and (i == 0 or C[i][j-1] >= C[i-1][j]):
getDiff(C, X, Y, i, j-1, a, r)
a.append( (j-1, Y[j-1]) )
debug(" + " + "%02X" % ord(Y[j-1]))
elif i > 0 and (j == 0 or C[i][j-1] < C[i-1][j]):
getDiff(C, X, Y, i-1, j, a, r)
r.append( (i-1, X[i-1]) )
debug(" - " + "%02X" % ord(X[i-1]))

def toString( bb, hS, rS ) :
S = ""

for i in bb.ins :
ident = i.get_name()
for op in i.get_operands() :
if i.type_ins_tag == 0 :
if "#" in op[0] :
ident += "%s" % op

# print i.get_name(), i.get_operands()
if ident not in hS :
hS[ ident ] = len(hS)
rS[ chr( hS[ ident ] ) ] = ident

S += chr( hS[ ident ] )

return S

DIFF_INS_TAG = {
"ORIG" : 0,
"ADD" : 1,
"REMOVE" : 2
}

class DiffBB :
def __init__(self, bb1, bb2, info) :
self.bb1 = bb1
self.bb2 = bb2
self.info = info

self.start = self.bb1.start
self.end = self.bb1.end
self.name = self.bb1.name

self.di = None
self.ins = []

def diff_ins(self, di) :
self.di = di

off_add = {}
off_rm = {}
for i in self.di.add_ins :
off_add[ i[0] ] = i

for i in self.di.remove_ins :
off_rm[ i[0] ] = i

nb = 0
for i in self.bb1.ins :
ok = False
if nb in off_add :
debug("%d ADD %s %s" % (nb, off_add[ nb ][2].get_name(), off_add[ nb ][2].get_operands()))
self.ins.append( off_add[ nb ][2] )
setattr( off_add[ nb ][2], "diff_tag", DIFF_INS_TAG["ADD"] )
del off_add[ nb ]

if nb in off_rm :
debug("%d RM %s %s" % (nb, off_rm[ nb ][2].get_name(), off_rm[ nb ][2].get_operands()))
self.ins.append( off_rm[ nb ][2] )
setattr( off_rm[ nb ][2], "diff_tag", DIFF_INS_TAG["REMOVE"] )
del off_rm[ nb ]
ok = True

if ok == False :
self.ins.append( i )
debug("%d %s %s" % (nb, i.get_name(), i.get_operands()))
setattr( i, "diff_tag", DIFF_INS_TAG["ORIG"] )

nb += 1

#print nb, off_add, off_rm

nbmax = nb
if off_add != {} :
nbmax = sorted(off_add)[-1]
if off_rm != {} :
nbmax = max(nbmax, sorted(off_rm)[-1])

while nb <= nbmax :
if nb in off_add :
debug("%d ADD %s %s" % (nb, off_add[ nb ][2].get_name(), off_add[ nb ][2].get_operands()))
self.ins.append( off_add[ nb ][2] )
setattr( off_add[ nb ][2], "diff_tag", DIFF_INS_TAG["ADD"] )
del off_add[ nb ]

if nb in off_rm :
debug("%d RM %s %s" % (nb, off_rm[ nb ][2].get_name(), off_rm[ nb ][2].get_operands()))
self.ins.append( off_rm[ nb ][2] )
setattr( off_rm[ nb ][2], "diff_tag", DIFF_INS_TAG["REMOVE"] )
del off_rm[ nb ]

nb += 1

#print off_add, off_rm

def set_childs(self, abb) :
setattr( self.bb1.ins[-1], "childs", self.bb1.childs )

for i in self.ins :
if i == self.bb2.ins[-1] :

childs = []
for c in self.bb2.childs :
if c[2].name in abb :
debug("SET %s %s" % (c[2], abb[ c[2].name ]))
childs.append( (c[0], c[1], abb[ c[2].name ]) )
else :
debug("SET ORIG %s" % str(c))
childs.append( c )

setattr( i, "childs", childs )

def show(self) :
print "\tADD INSTRUCTIONS :"
for i in self.di.add_ins :
print "\t\t", i[0], i[1], i[2].get_name(), i[2].get_operands()

print "\tREMOVE INSTRUCTIONS :"
for i in self.di.remove_ins :
print "\t\t", i[0], i[1], i[2].get_name(), i[2].get_operands()

class NewBB :
def __init__(self, bb) :
self.bb = bb

self.start = self.bb.start
self.end = self.bb.end
self.name = self.bb.name
self.ins = self.bb.ins

def set_childs(self, abb) :
childs = []
for c in self.bb.childs :
if c[2].name in abb :
debug("SET %s %s " % (c[2], abb[ c[2].name ]))
childs.append( (c[0], c[1], abb[ c[2].name ]) )
else :
debug("SET ORIG %s" % str(c))
childs.append( c )

setattr( self, "childs", childs )

class DiffINS :
def __init__(self, add_ins, remove_ins) :
self.add_ins = add_ins
self.remove_ins = remove_ins

def filter_diff_ins_1( dbb, sim, name_attribute ) :
final_add = []
final_rm = []

hS = {}
rS = {}

X = toString( dbb.bb1, hS, rS )
Y = toString( dbb.bb2, hS, rS )


debug("%s %d" % (repr(X), len(X)))
debug("%s %d" % (repr(Y), len(Y)))

m = len(X)
n = len(Y)

C = LCS( X, Y )
a = []
r = []

getDiff(C, X, Y, m, n, a, r)
debug(a)
debug(r)

debug("DEBUG ADD")
for i in a :
debug(" \t %s %s %s" % (i[0], dbb.bb2.ins[ i[0] ].get_name(), dbb.bb2.ins[ i[0] ].get_operands()))
final_add.append( (i[0], 0, dbb.bb2.ins[ i[0] ]) )

debug("DEBUG REMOVE")
for i in r :
debug(" \t %s %s %s" % (i[0], dbb.bb1.ins[ i[0] ].get_name(), dbb.bb1.ins[ i[0] ].get_operands()))
final_rm.append( (i[0], 0, dbb.bb1.ins[ i[0] ]) )

dbb.diff_ins( DiffINS( final_add, final_rm ) )

FILTER_NAME = "FILTER_NAME"
FILTER_CHECKSUM_METH = "FILTER_CHECKSUM_METH"
FILTER_SIM_METH = "FILTER_SIM_METH"
FILTER_CHECKSUM_BB = "FILTER_CHECKSUM_BB"
FILTER_SIM_BB = "FILTER_SIM_BB"
FILTER_DIFF_INS = "FILTER_DIFF_INS"
FILTER_CHECKSUM_VM = "FILTER_CHECKSUM_VM"
FILTER_SIM_VM = "FILTER_SIM_VM"

FILTERS = {
"FILTER_1" : { FILTER_CHECKSUM_METH : filter_checksum_meth_1,
FILTER_SIM_METH : filter_sim_meth_2,
FILTER_CHECKSUM_BB : filter_checksum_bb_1,
FILTER_SIM_BB : filter_sim_bb_1,
FILTER_DIFF_INS : filter_diff_ins_1,
},
}

DIFF_BB_TAG = {
"ORIG" : 0,
"DIFF" : 1,
"NEW" : 2
}

class Method :
def __init__(self, vm, vmx, m, sim) :
self.m = m
self.vm = vm
self.vmx = vmx
self.mx = vmx.get_method( m )

self.sim = sim

#######
# Attribute :
# Method <-> sorted Methods
#
# Method <-> Methods[0] :
#
#
#
#
def add_attribute(self, name, func_meth, func_checksum_bb) :
bb = {}
bbhash = {}

fm = func_meth( self.m, self.sim )

for i in self.mx.basic_blocks.get() :
bb[ i.name ] = func_checksum_bb( i )

try :
bbhash[ bb[ i.name ].get_hash() ].append( bb[ i.name ] )
except KeyError :
bbhash[ bb[ i.name ].get_hash() ] = []
bbhash[ bb[ i.name ].get_hash() ].append( bb[ i.name ] )

setattr(self, "checksum_" + name, fm)

setattr(self, "bb_" + name, bb)
setattr(self, "bb_sha256_" + name, bbhash)
setattr(self, "sha256_" + name, hashlib.sha256( fm.get_buff() ).hexdigest())

def similarity(self, new_method, func_sim, name_attribute) :
x = None
try :
x = getattr( self, "hash_" + name_attribute )
except AttributeError :
setattr( self, "hash_" + name_attribute, {} )
x = getattr( self, "hash_" + name_attribute )

x[ new_method ] = func_sim( self, new_method, self.sim, name_attribute )

def sort(self, name_attribute) :
x = getattr( self, "hash_" + name_attribute )

z = sorted(x.iteritems(), key=lambda (k,v): (v,k))

if get_debug() :
for i in z :
debug("\t %s %s %s %f" %(i[0].m.get_class_name(), i[0].m.get_name(), i[0].m.get_descriptor(), i[1]))

setattr( self, "sort_" + name_attribute, z[:1] )

def checksort(self, name_attribute, method) :
z = getattr( self, "sort_" + name_attribute )
for i in z :
if method == i[0] :
return True
return False

def diff(self, name_attribute, func_sim_bb, func_diff_ins):
self.sim.set_compress_type( XZ_COMPRESS )

z = getattr( self, "sort_" + name_attribute )
bb1 = getattr( self, "bb_" + name_attribute )

### Dict for diff basic blocks
### vm1 basic block : vm2 basic blocks -> value (0.0 to 1.0)
diff_bb = {}

### List to get directly all diff basic blocks
direct_diff_bb = []

### Dict for new basic blocks
new_bb = {}

### Reverse Dict with matches diff basic blocks
associated_bb = {}

for b1 in bb1 :
diff_bb[ bb1[ b1 ] ] = {}

debug("%s 0x%x" % (b1, bb1[ b1 ].basic_block.end))
for i in z :
bb2 = getattr( i[0], "bb_" + name_attribute )
b_z = diff_bb[ bb1[ b1 ] ]

bb2hash = getattr( i[0], "bb_sha256_" + name_attribute )

# If b1 is in bb2 :
# we can have one or more identical basic blocks to b1, we must add them
if bb1[ b1 ].get_hash() in bb2hash :
for equal_bb in bb2hash[ bb1[ b1 ].get_hash() ] :
b_z[ equal_bb.basic_block.name ] = 0.0

# If b1 is not in bb2 :
# we must check similarities between all bb2
else :
for b2 in bb2 :
b_z[ b2 ] = func_sim_bb( bb1[ b1 ], bb2[ b2 ], self.sim )

sorted_bb = sorted(b_z.iteritems(), key=lambda (k,v): (v,k))

debug("\t\t%s" % sorted_bb[:2])

for new_diff in sorted_bb :
associated_bb[ new_diff[0] ] = bb1[ b1 ].basic_block

if new_diff[1] == 0.0 :
direct_diff_bb.append( new_diff[0] )

if sorted_bb[0][1] != 0.0 :
diff_bb[ bb1[ b1 ] ] = (bb2[ sorted_bb[0][0] ], sorted_bb[0][1])
direct_diff_bb.append( sorted_bb[0][0] )
else :
del diff_bb[ bb1[ b1 ] ]

for i in z :
bb2 = getattr( i[0], "bb_" + name_attribute )
for b2 in bb2 :
if b2 not in direct_diff_bb :
new_bb[ b2 ] = bb2[ b2 ]

dbb = {}
nbb = {}
# Add all different basic blocks
for d in diff_bb :
dbb[ d.basic_block.name ] = DiffBB( d.basic_block, diff_bb[ d ][0].basic_block, diff_bb[ d ] )

# Add all new basic blocks
for n in new_bb :
nbb[ new_bb[ n ].basic_block ] = NewBB( new_bb[ n ].basic_block )
del associated_bb[ n ]

setattr(self, "dbb_" + name_attribute, dbb)
setattr(self, "nbb_" + name_attribute, nbb)

# Found diff instructions
for d in dbb :
func_diff_ins( dbb[d], self.sim, name_attribute )

# Set new childs for diff basic blocks
# The instructions will be tag with a new flag "childs"
for d in dbb :
dbb[ d ].set_childs( associated_bb )

# Set new childs for new basic blocks
for d in nbb :
nbb[ d ].set_childs( associated_bb )

# Create and tag all (orig/diff/new) basic blocks
self.create_bbs( name_attribute )

def create_bbs(self, name_attribute) :
dbb = getattr(self, "dbb_" + name_attribute)
nbb = getattr(self, "nbb_" + name_attribute)

# For same block :
# tag = 0
# For diff block :
# tag = 1
# For new block :
# tag = 2
l = []
for bb in self.mx.basic_blocks.get() :
if bb.name not in dbb :
# add the original basic block
setattr( bb, "bb_tag", DIFF_BB_TAG["ORIG"] )
l.append( bb )
else :
# add the diff basic block
setattr( dbb[ bb.name ], "bb_tag", DIFF_BB_TAG["DIFF"] )
l.append( dbb[ bb.name ] )

for i in nbb :
# add the new basic block
setattr( nbb[ i ], "bb_tag", DIFF_BB_TAG["NEW"] )
l.append( nbb[ i ] )

# Sorted basic blocks by addr (orig, new, diff)
l = sorted(l, key = lambda x : x.start)
setattr( self, "bbs_" + name_attribute, l )

def getsha256(self, name_attribute) :
return getattr(self, "sha256_" + name_attribute)

def show(self, name_attribute, details=False) :
print self.m.get_class_name(), self.m.get_name(), self.m.get_descriptor(),
print "with",

z = getattr( self, "sort_" + name_attribute )
for i in z :
print i[0].m.get_class_name(), i[0].m.get_name(), i[0].m.get_descriptor(), i[1]

dbb = getattr(self, "dbb_" + name_attribute)
nbb = getattr(self, "nbb_" + name_attribute)

print "\tDIFF BASIC BLOCKS :"
for d in dbb :
print "\t\t", dbb[d].bb1.name, " --->", dbb[d].bb2.name, ":", dbb[d].info[1]
if details :
dbb[d].show()

print "NEW BASIC BLOCKS :"
for b in nbb :
print "\t\t", nbb[b].name

if details :
# show diff !
bytecode.PrettyShow2( getattr(self, "bbs_" +name_attribute) )

def show2(self, details=False) :
print self.m.get_class_name(), self.m.get_name(), self.m.get_descriptor()
if details :
bytecode.PrettyShow1( self.mx.basic_blocks.get() )

BASE = "base"
METHODS = "methods"
HASHSUM = "hashsum"
DIFFMETHODS = "diffmethods"
NEWMETHODS = "newmethods"
class Diff :
def __init__(self, vm1, vm2, F=FILTERS) :
self.vms = [ vm1, vm2 ]
self.sim = SIMILARITY( "classification/libsimilarity/libsimilarity.so" )
self.sim.set_compress_type( XZ_COMPRESS )

#set_debug()

self.F = F

self.filters = {}

for i in self.F :
self.filters[ i ] = {}
self.filters[ i ][ BASE ] = { FILTER_NAME : i }
self.filters[ i ][ BASE ].update( self.F[ i ] )
self.filters[ i ][ METHODS ] = {}
self.filters[ i ][ HASHSUM ] = {}
self.filters[ i ][ DIFFMETHODS ] = []
self.filters[ i ][ NEWMETHODS ] = []

for i in self.vms :
for m in i[0].get_methods() :
m = Method( i[0], i[1], m, self.sim )

for fil in self.filters :
if i[0] not in self.filters[fil][METHODS] :
self.filters[fil][METHODS][ i[0] ] = []
self.filters[fil][HASHSUM][ i[0] ] = []

self.filters[fil][METHODS][ i[0] ].append( m )
m.add_attribute( self.filters[fil][BASE][FILTER_NAME], self.filters[fil][BASE][FILTER_CHECKSUM_METH], self.filters[fil][BASE][FILTER_CHECKSUM_BB] )
self.filters[fil][HASHSUM][i[0]].append( m.getsha256( self.filters[fil][BASE][FILTER_NAME] ) )

# Check if some methods in the first file has been modified
for fil in self.filters :
for j in self.filters[fil][METHODS][vm1[0]] :
for i1 in self.filters[fil][METHODS] :
if i1 != vm1[0] :
# B1 not at 0.0 in BB2
if j.getsha256( self.filters[fil][BASE][FILTER_NAME] ) not in self.filters[fil][HASHSUM][i1] :
for k in self.filters[fil][METHODS][i1] :
# B2 not at 0.0 in BB1
if k.getsha256( self.filters[fil][BASE][FILTER_NAME] ) not in self.filters[fil][HASHSUM][vm1[0]] :
j.similarity( k, self.filters[fil][BASE][FILTER_SIM_METH], self.filters[fil][BASE][FILTER_NAME] )
if j not in self.filters[fil][DIFFMETHODS] :
self.filters[fil][DIFFMETHODS].append(j)

# print "DEBUG DIFF METHODS"
for fil in self.filters :
for j in self.filters[fil][DIFFMETHODS] :
# print "DEBUG", j, j.m.get_class_name(), j.m.get_name(), j.m.get_descriptor()
j.sort( self.filters[fil][BASE][FILTER_NAME] )
j.diff( self.filters[fil][BASE][FILTER_NAME], self.filters[fil][BASE][FILTER_SIM_BB], self.filters[fil][BASE][FILTER_DIFF_INS] )
# j.show( self.filters[fil][BASE][FILTER_NAME] )

# Check if some methods in the second file are totally new !
for fil in self.filters :
for j in self.filters[fil][METHODS][vm2[0]] :

# new methods can't be in diff methods
if j not in self.filters[fil][DIFFMETHODS] :
# new methods hashs can't be in first file
if j.getsha256( self.filters[fil][BASE][FILTER_NAME] ) not in self.filters[fil][HASHSUM][vm1[0]] :
ok = True
# new methods can't be compared to another one
for diff_method in self.filters[fil][DIFFMETHODS] :
#print diff_method, "--->", j
if diff_method.checksort( self.filters[fil][BASE][FILTER_NAME], j ) :
ok = False
break

if ok :
self.filters[fil][NEWMETHODS].append( j )

# print "DEBUG NEW METHODS"
# for fil in self.filters :
# print "\tDEBUG", self.filters[fil][NEWMETHODS]
# for method in self.filters[fil][NEWMETHODS] :
# print "DEBUG", method.m.get_class_name(), method.m.get_name(), method.m.get_descriptor()

def get_diff_methods(self) :
return self.get_elem( DIFFMETHODS )

def get_new_methods(self) :
return self.get_elem( NEWMETHODS )

def get_elem(self, attr) :
d = {}
for fil in self.filters :
d[ fil ] = [ x for x in self.filters[fil][attr] ]
return d

#######################################################################################################

def filter_sim_3( m1, m2, sim, name_attribute ) :
ncd1 = sim.ncd( m1.vmx.get_method_signature( m1.m, 1 ), m2.vmx.get_method_signature( m2.m, 1 ) )
return ncd1

class CheckSumVM :
def __init__(self) :
pass

def filter_checksum_vm_1( vm, name_attribute ):
return

def filter_sim_vm_1( vm, name_attribute ):
return

FILTERS_SIM = {
"FILTER_SIM1" : {
FILTER_CHECKSUM_VM : filter_checksum_vm_1,
FILTER_SIM_VM : filter_sim_vm_1,
FILTER_CHECKSUM_METH : filter_checksum_meth_1,
FILTER_SIM_METH : filter_sim_3,
FILTER_CHECKSUM_BB : filter_checksum_bb_1
},
}

### SIM :
# DATA
# string
# constant (int, float ...)
# clinit
# CODE
# Instructions : module Diff
# exceptions
# Format
class Sim :
def __init__(self, vm1, vm2, F=FILTERS_SIM) :
self.vms = [ vm1, vm2 ]
self.sim = SIMILARITY( "classification/libsimilarity/libsimilarity.so" )
self.sim.set_compress_type( XZ_COMPRESS )

set_debug()

svm1 = ''.join( vm1[0].get_strings() )
svm2 = ''.join( vm2[0].get_strings() )

print self.sim.ncd( svm1, svm2 )

self.F = F

self.filters = {}

for i in self.F :
self.filters[ i ] = {}
self.filters[ i ][ BASE ] = { FILTER_NAME : i }
self.filters[ i ][ BASE ].update( self.F[ i ] )
self.filters[ i ][ METHODS ] = {}
self.filters[ i ][ HASHSUM ] = {}
self.filters[ i ][ DIFFMETHODS ] = []
self.filters[ i ][ NEWMETHODS ] = []

for i in self.vms :
for m in i[0].get_methods() :
m = Method( i[0], i[1], m, self.sim )

for fil in self.filters :
if i[0] not in self.filters[fil][METHODS] :
self.filters[fil][METHODS][ i[0] ] = []
self.filters[fil][HASHSUM][ i[0] ] = []

self.filters[fil][METHODS][ i[0] ].append( m )
m.add_attribute( self.filters[fil][BASE][FILTER_NAME], self.filters[fil][BASE][FILTER_CHECKSUM_METH], self.filters[fil][BASE][FILTER_CHECKSUM_BB] )
self.filters[fil][HASHSUM][i[0]].append( m.getsha256( self.filters[fil][BASE][FILTER_NAME] ) )

# Check if some methods in the first file has been modified
for fil in self.filters :
for j in self.filters[fil][METHODS][vm1[0]] :
for i1 in self.filters[fil][METHODS] :
if i1 != vm1[0] :
# B1 not at 0.0 in BB2
if j.getsha256( self.filters[fil][BASE][FILTER_NAME] ) not in self.filters[fil][HASHSUM][i1] :
for k in self.filters[fil][METHODS][i1] :
# B2 not at 0.0 in BB1
if k.getsha256( self.filters[fil][BASE][FILTER_NAME] ) not in self.filters[fil][HASHSUM][vm1[0]] :
j.similarity( k, self.filters[fil][BASE][FILTER_SIM_METH], self.filters[fil][BASE][FILTER_NAME] )
if j not in self.filters[fil][DIFFMETHODS] :
self.filters[fil][DIFFMETHODS].append(j)

# print "DEBUG DIFF METHODS"
for fil in self.filters :
for j in self.filters[fil][DIFFMETHODS] :
debug("%s %s %s" % (j.m.get_class_name(), j.m.get_name(), j.m.get_descriptor()))
j.sort( self.filters[fil][BASE][FILTER_NAME] )

Change log

a7887d5d8446 by zost on May 3, 2011   Diff
reindent
Go to: 
Project members, sign in to write a code review

Older revisions

820d6f4879d8 by desnos on May 3, 2011   Diff
update
e7836bbe8ba1 by desnos on May 3, 2011   Diff
begin androsim
95f462175429 by desnos on Apr 30, 2011   Diff
up method2*
All revisions of this file

File info

Size: 25984 bytes, 747 lines
Powered by Google Project Hosting