-
-
Notifications
You must be signed in to change notification settings - Fork 375
/
Copy pathdiaphora_heuristics.py
1323 lines (1239 loc) · 39.9 KB
/
diaphora_heuristics.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
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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python3
"""
Diaphora, a diffing plugin for IDA
Copyright (c) 2015-2024, Joxean Koret
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
#-------------------------------------------------------------------------------
# Only used for the internal tests
import pprint
from collections import Counter
#-------------------------------------------------------------------------------
# Use only for heuristics generating 1.0 ratios, results without false positives
HEUR_TYPE_NO_FPS = 0
# Use it for most heuristics; it will assign 1.0 ratios to the best chooser,
# values between 0.5 and <1.0 to the specific partial chooser and <0.5 results
# to the unreliable chooser, if specified.
HEUR_TYPE_RATIO = 1
# Similar as before, but partial results are only assigned for matches with a
# min specified ratio.
HEUR_TYPE_RATIO_MAX = 2
# Similar as before, but 'unreliable' results are not unreliable, thus, they go
# to the 'partial' tab instead.
HEUR_TYPE_RATIO_MAX_TRUSTED = 3
#-------------------------------------------------------------------------------
HEUR_FLAG_NONE = 0
HEUR_FLAG_UNRELIABLE = 1
HEUR_FLAG_SLOW = 2
# The heuristic should only be launched when diffing the same architecture
HEUR_FLAG_SAME_CPU = 3
#-------------------------------------------------------------------------------
SELECT_FIELDS = """ f.address ea, f.name name1, df.address ea2, df.name name2,
{heur} description,
f.pseudocode pseudo1, df.pseudocode pseudo2,
f.assembly asm1, df.assembly asm2,
f.pseudocode_primes pseudo_primes1, df.pseudocode_primes pseudo_primes2,
f.nodes nodes1, df.nodes nodes2,
cast(f.md_index as real) md1, cast(df.md_index as real) md2,
f.clean_assembly clean_assembly1, df.clean_assembly clean_assembly2,
f.clean_pseudo clean_pseudo1, df.clean_pseudo clean_pseudo2,
f.mangled_function mangled1, df.mangled_function mangled2,
f.clean_microcode clean_micro1, df.clean_microcode clean_micro2,
f.bytes_hash bytes_hash1, df.bytes_hash bytes_hash2,
f.edges edges1, df.edges edges2,
f.indegree indegree1, df.indegree indegree2,
f.outdegree outdegree1, df.outdegree outdegree2,
f.instructions instructions1, df.instructions instructions2,
f.cyclomatic_complexity cc1, df.cyclomatic_complexity cc2,
f.strongly_connected strongly_connected1,
df.strongly_connected strongly_connected2,
f.loops loops1, df.loops loops2,
f.constants_count constants_count1,
df.constants_count constants_count2,
f.size size1, df.size size2,
f.kgh_hash kgh_hash1, df.kgh_hash kgh_hash2
"""
def get_query_fields(heur, quote=True):
"""
Get the list of fields used in any and all SQL heuristics queries.
"""
val = heur
if quote:
val = repr(val)
ret = SELECT_FIELDS.format(heur=val)
return ret
#-------------------------------------------------------------------------------
HEURISTICS = []
NAME = "Same RVA and hash"
HEURISTICS.append({
"name":NAME,
"category":"Best",
"ratio":HEUR_TYPE_NO_FPS,
"sql":""" select """ + get_query_fields(NAME) + """
from functions f,
diff.functions df
where (df.rva = f.rva
or df.segment_rva = f.segment_rva)
and df.bytes_hash = f.bytes_hash
and df.instructions = f.instructions
and ((f.name = df.name and substr(f.name, 1, 4) != 'sub_')
or (substr(f.name, 1, 4) = 'sub_' or substr(df.name, 1, 4) = 'sub_'))
and f.nodes >= 3
and df.nodes >= 3
%POSTFIX%""",
"flags":[HEUR_FLAG_SAME_CPU]
})
NAME = "Same order and hash"
HEURISTICS.append({
"name":NAME,
"category":"Best",
"ratio":HEUR_TYPE_NO_FPS,
"sql":""" select """ + get_query_fields(NAME) + """
from functions f,
diff.functions df
where df.id = f.id
and df.bytes_hash = f.bytes_hash
and df.instructions = f.instructions
and ((f.name = df.name and substr(f.name, 1, 4) != 'sub_')
or (substr(f.name, 1, 4) = 'sub_' or substr(df.name, 1, 4) = 'sub_'))
and ((f.nodes > 1 and df.nodes > 1
and f.instructions > 5 and df.instructions > 5)
or f.instructions > 10 and df.instructions > 10)
%POSTFIX%""",
"flags":[HEUR_FLAG_SAME_CPU]
})
NAME = "Function Hash"
HEURISTICS.append({
"name":NAME,
"category":"Best",
"ratio":HEUR_TYPE_NO_FPS,
"sql":""" select distinct """ + get_query_fields(NAME) + """
from functions f,
diff.functions df
where f.function_hash = df.function_hash
and ((f.nodes > 1 and df.nodes > 1
and f.instructions > 5 and df.instructions > 5)
or f.instructions > 10 and df.instructions > 10)
%POSTFIX%""",
"flags":[HEUR_FLAG_SAME_CPU]
})
NAME = "Bytes hash"
HEURISTICS.append({
"name":NAME,
"category":"Best",
"ratio":HEUR_TYPE_NO_FPS,
"sql":""" select distinct """ + get_query_fields(NAME) + """
from functions f,
diff.functions df
where f.bytes_hash = df.bytes_hash
and f.instructions > 5 and df.instructions > 5
%POSTFIX%""",
"flags":[HEUR_FLAG_SAME_CPU]
})
NAME = "Same address and mnemonics"
HEURISTICS.append({
"name":NAME,
"category":"Best",
"ratio":HEUR_TYPE_RATIO,
"sql":""" select distinct """ + get_query_fields(NAME) + """
from functions f,
diff.functions df
where df.address = f.address
and df.mnemonics = f.mnemonics
and df.instructions = f.instructions
and df.instructions > 5
and ((f.name = df.name and substr(f.name, 1, 4) != 'sub_')
or (substr(f.name, 1, 4) = 'sub_' or substr(df.name, 1, 4) = 'sub_'))
%POSTFIX%
order by f.source_file = df.source_file""",
"flags":[]
})
NAME = "Same cleaned assembly"
HEURISTICS.append({
"name":NAME,
"category":"Best",
"ratio":HEUR_TYPE_RATIO,
"sql":""" select """ + get_query_fields(NAME) + """
from functions f,
diff.functions df
where f.clean_assembly = df.clean_assembly
and f.nodes >= 3 and df.nodes >= 3
and f.name not like 'nullsub%'
and df.name not like 'nullsub%'
%POSTFIX%
order by f.source_file = df.source_file""",
"flags":[HEUR_FLAG_SAME_CPU]
})
NAME = "Same cleaned microcode"
HEURISTICS.append({
"name":NAME,
"category":"Best",
"ratio":HEUR_TYPE_RATIO,
"sql":""" select """ + get_query_fields(NAME) + """
from functions f,
diff.functions df
where f.clean_microcode = df.clean_microcode
and f.instructions > 3 and df.instructions > 3
and f.name not like 'nullsub%'
and df.name not like 'nullsub%'
%POSTFIX%
order by f.source_file = df.source_file""",
"flags":[HEUR_FLAG_SAME_CPU]
})
NAME = "Same cleaned pseudo-code"
HEURISTICS.append({
"name":NAME,
"category":"Best",
"ratio":HEUR_TYPE_RATIO,
"sql":""" select """ + get_query_fields(NAME) + """
from functions f,
diff.functions df
where f.clean_pseudo = df.clean_pseudo
and f.pseudocode_lines > 5 and df.pseudocode_lines > 5
and f.name not like 'nullsub%'
and df.name not like 'nullsub%'
%POSTFIX%
order by f.source_file = df.source_file""",
"flags":[]
})
NAME = "Same address, nodes, edges and mnemonics"
HEURISTICS.append({
"name":NAME,
"category":"Best",
"ratio":HEUR_TYPE_RATIO,
"sql":"""select """ + get_query_fields(NAME) + """
from functions f,
diff.functions df
where f.rva = df.rva
and f.instructions = df.instructions
and f.nodes = df.nodes
and f.edges = df.edges
and f.mnemonics = df.mnemonics
and f.instructions > 3
and df.instructions > 3
and f.nodes > 1
%POSTFIX%
order by f.source_file = df.source_file""",
"flags":[]
})
NAME = "Same RVA"
HEURISTICS.append({
"name":NAME,
"category":"Best",
"ratio":HEUR_TYPE_RATIO_MAX,
"sql":""" select distinct """ + get_query_fields(NAME) + """
from functions f,
diff.functions df
where df.rva = f.rva
and ((f.name = df.name and substr(f.name, 1, 4) != 'sub_')
or (substr(f.name, 1, 4) = 'sub_' or substr(df.name, 1, 4) = 'sub_'))
and f.nodes >= 3
and df.nodes >= 3
%POSTFIX%
order by f.source_file = df.source_file""",
"min":0.7,
"flags":[HEUR_FLAG_SAME_CPU]
})
#
# Seems not to find anything?
#
NAME = "Equal assembly or pseudo-code"
HEURISTICS.append({
"name":NAME,
"category":"Best",
"ratio":HEUR_TYPE_NO_FPS,
"sql":"""select """ + get_query_fields("Equal pseudo-code") + """
from functions f,
diff.functions df
where f.pseudocode = df.pseudocode
and df.pseudocode is not null
and f.pseudocode_lines >= 5
and f.name not like 'nullsub%'
and df.name not like 'nullsub%'
%POSTFIX%
union
select """ + get_query_fields("Equal assembly") + """
from functions f,
diff.functions df
where f.assembly = df.assembly
and df.assembly is not null
and f.instructions >= 4 and df.instructions >= 4
and f.name not like 'nullsub%'
and df.name not like 'nullsub%'
%POSTFIX% """,
"flags":[]
})
NAME = "Microcode mnemonics small primes product"
HEURISTICS.append({
"name":NAME,
"category":"Best",
"ratio":HEUR_TYPE_RATIO,
"sql":""" select """ + get_query_fields(NAME) + """
from functions f,
diff.functions df
where f.microcode_spp = df.microcode_spp
and f.microcode_spp != 1
and df.microcode_spp != 1
and f.instructions > 5 and df.instructions > 5
and f.nodes > 2 and df.nodes > 2
and f.name not like 'nullsub%'
and df.name not like 'nullsub%'
%POSTFIX%
order by f.source_file = df.source_file""",
"flags":[]
})
# It seems that SQLite is slowly executing this query due to the following:
#
# BLOOM FILTER ON main_cu (id=?)
#
# I have seen bugs related to this fixed on February 2023, so I think I'll have
# to take a look to see if bloom filters can be disabled...
NAME = "Same named compilation unit function match"
HEURISTICS.append({
"name":NAME,
"category":"Partial",
"ratio":HEUR_TYPE_RATIO_MAX_TRUSTED,
"sql":""" select """ + get_query_fields(NAME) + """
from main.compilation_units main_cu,
main.compilation_unit_functions mcuf,
main.functions f,
diff.compilation_units diff_cu,
diff.compilation_unit_functions dcuf,
diff.functions df
where main_cu.name != ''
and diff_cu.name != ''
and main_cu.name = diff_cu.name
and f.id = mcuf.func_id
and df.id = dcuf.func_id
and mcuf.cu_id = main_cu.id
and dcuf.cu_id = diff_cu.id
and df.primes_value = f.primes_value
and df.nodes = f.nodes
and f.nodes >= 5
%POSTFIX%
""",
"min":0.44,
"flags":[]
})
NAME = "Same anonymous compilation unit function match"
HEURISTICS.append({
"name":NAME,
"category":"Partial",
"ratio":HEUR_TYPE_RATIO_MAX,
"sql":""" select """ + get_query_fields(NAME) + """
from main.compilation_units main_cu,
main.compilation_unit_functions mcuf,
main.functions f,
diff.compilation_units diff_cu,
diff.compilation_unit_functions dcuf,
diff.functions df
where main_cu.name != ''
and diff_cu.name != ''
and main_cu.name = diff_cu.name
and f.id = mcuf.func_id
and df.id = dcuf.func_id
and mcuf.cu_id = main_cu.id
and dcuf.cu_id = diff_cu.id
and df.pseudocode_primes = f.pseudocode_primes
and df.nodes = f.nodes
and f.nodes >= 5
%POSTFIX%
order by f.source_file = df.source_file""",
"min":0.449,
"flags":[]
})
# An ORDER BY clause would be good to have here, but SQLite may generate huge
# B-TREEs that might even cause errors after a long time running when dealing
# with huge databases, therefore, I'm removing it.
#
# Also, it seems that a bloom filter is used here too:
#
# BLOOM FILTER ON df (id=?)
#
# And it might be slowing down our query...
NAME = "Same compilation unit"
HEURISTICS.append({
"name":NAME,
"category":"Partial",
"ratio":HEUR_TYPE_RATIO,
"sql":"""select """ + get_query_fields(NAME) + """
from main.compilation_units mcu,
main.compilation_unit_functions mcuf,
main.functions f,
diff.compilation_units dcu,
diff.compilation_unit_functions dcuf,
diff.functions df
where dcu.pseudocode_primes = mcu.pseudocode_primes
and mcuf.cu_id = mcu.id
and dcuf.cu_id = dcu.id
and f.id = mcuf.func_id
and df.id = dcuf.func_id
and f.nodes > 4
and df.nodes > 4
and (substr(f.name, 1, 4) = 'sub_' or substr(df.name, 1, 4) == 'sub_')
%POSTFIX% """,
"flags":[HEUR_FLAG_SLOW]
})
# Adding a DISTINCT and an ORDER BY clause in this query causes SQLite to create
# huge temporary B-TREEs that, depending on the size of the databases, might end
# up triggering an error after a long time running.
NAME = "Same KOKA hash and constants"
HEURISTICS.append({
"name":NAME,
"category":"Partial",
"ratio":HEUR_TYPE_RATIO,
"sql":"""select """ + get_query_fields(NAME) + """
from main.constants mc,
diff.constants dc,
main.functions f,
diff.functions df
where mc.constant = dc.constant
and f.id = mc.func_id
and df.id = dc.func_id
and f.kgh_hash = df.kgh_hash
and f.nodes >= 3
%POSTFIX% """,
"flags":[]
})
# The same explained in the previous query happens here: for huge databases the
# SQLite engine can generate huge B-TREEs for the ORDER BY clause. Removed it.
NAME = "Same KOKA hash and MD-Index"
HEURISTICS.append({
"name":NAME,
"category":"Partial",
"ratio":HEUR_TYPE_RATIO,
"sql":"""
select """ + get_query_fields(NAME) + """
from functions f,
diff.functions df
where f.kgh_hash = df.kgh_hash
and f.md_index = df.md_index
and f.nodes = df.nodes
and f.nodes >= 4
and f.outdegree = df.outdegree
and f.indegree = df.indegree
and (substr(f.name, 1, 4) = 'sub_' or substr(df.name, 1, 4) = 'sub_')
%POSTFIX%
""",
"flags":[]
})
NAME = "Same constants"
HEURISTICS.append({
"name":NAME,
"category":"Partial",
"ratio":HEUR_TYPE_RATIO_MAX,
"sql":"""select """ + get_query_fields(NAME) + """
from functions f,
diff.functions df
where f.constants = df.constants
and f.constants_count = df.constants_count
and f.constants_count > 1
%POSTFIX%
order by f.source_file = df.source_file""",
"min":0.5,
"flags":[]
})
# The ORDER BY clause is removed because it was causing serious slowness problems
# with big and huge databases.
NAME = "Same rare KOKA hash"
HEURISTICS.append({
"name":NAME,
"category":"Partial",
"ratio":HEUR_TYPE_RATIO_MAX,
"sql":"""
with shared_hashes as (
select kgh_hash
from diff.functions
where kgh_hash != 0
group by kgh_hash
having count(*) <= 2
union
select kgh_hash
from main.functions
where kgh_hash != 0
group by kgh_hash
having count(*) <= 2
)
select """ + get_query_fields(NAME) + """
from functions f,
diff.functions df,
shared_hashes
where f.kgh_hash = df.kgh_hash
and df.kgh_hash = shared_hashes.kgh_hash
and f.nodes > 5
and (substr(f.name, 1, 4) = 'sub_'
or substr(df.name, 1, 4) = 'sub_')
%POSTFIX%
""",
"min":0.45,
"flags":[]
})
NAME = "Same rare MD Index"
HEURISTICS.append({
"name":NAME,
"category":"Partial",
"ratio":HEUR_TYPE_RATIO,
"sql":"""
with shared_mds as (
select md_index
from diff.functions
where md_index != 0
group by md_index
having count(*) <= 2
union
select md_index
from main.functions
where md_index != 0
group by md_index
having count(*) <= 2
)
select """ + get_query_fields(NAME) + """
from functions f,
diff.functions df,
shared_mds
where f.md_index = df.md_index
and df.md_index = shared_mds.md_index
and f.nodes > 10
%POSTFIX%
order by f.source_file = df.source_file""",
"flags":[]
})
#
# Seems not find anything???
#
NAME = "Same address and rare constant"
HEURISTICS.append({
"name":NAME,
"category":"Partial",
"ratio":HEUR_TYPE_RATIO_MAX,
"sql":"""select distinct """ + get_query_fields(NAME) + """
from main.constants mc,
diff.constants dc,
main.functions f,
diff.functions df
where mc.constant = dc.constant
and f.id = mc.func_id
and df.id = dc.func_id
and df.address = f.address
%POSTFIX%
order by f.source_file = df.source_file""",
"min":0.5,
"flags":[]
})
# The DISTINCT and ORDER BY clause have been removed due to slowness problems
NAME = "Same rare constant"
HEURISTICS.append({
"name":NAME,
"category":"Partial",
"ratio":HEUR_TYPE_RATIO_MAX,
"sql":"""select """ + get_query_fields(NAME) + """
from main.constants mc,
diff.constants dc,
main.functions f,
diff.functions df
where mc.constant = dc.constant
and f.id = mc.func_id
and df.id = dc.func_id
and f.nodes >= 3 and df.nodes >= 3
and f.constants_count > 0
%POSTFIX% """,
"min":0.2,
"flags":[HEUR_FLAG_SLOW]
})
NAME = "Same MD Index and constants"
HEURISTICS.append({
"name":NAME,
"category":"Partial",
"ratio":HEUR_TYPE_RATIO,
"sql":""" select distinct """ + get_query_fields(NAME) + """
from functions f,
diff.functions df
where f.md_index = df.md_index
and f.md_index > 0
and f.nodes >= 3 and df.nodes >= 3
and ((f.constants = df.constants
and f.constants_count > 0))
%POSTFIX%
order by f.source_file = df.source_file""",
"flags":[]
})
NAME = "Import names hash"
HEURISTICS.append({
"name":NAME,
"category":"Partial",
"ratio":HEUR_TYPE_RATIO,
"sql":"""select distinct """ + get_query_fields(NAME) + """
from functions f,
diff.functions df
where f.names = df.names
and f.names != '[]'
and f.md_index = df.md_index
and f.instructions = df.instructions
and f.nodes > 5 and df.nodes > 5
%POSTFIX%
order by f.source_file = df.source_file""",
"flags":[]
})
NAME = "Mnemonics and names"
HEURISTICS.append({
"name":NAME,
"category":"Partial",
"ratio":HEUR_TYPE_RATIO,
"sql":""" select """ + get_query_fields(NAME) + """
from functions f,
diff.functions df
where f.mnemonics = df.mnemonics
and f.instructions = df.instructions
and f.names = df.names
and f.names != '[]'
and f.instructions > 5 and df.instructions > 5
%POSTFIX%
order by f.source_file = df.source_file""",
"flags":[]
})
NAME = "Pseudo-code fuzzy hash"
HEURISTICS.append({
"name":NAME,
"category":"Partial",
"ratio":HEUR_TYPE_RATIO,
"sql":"""select distinct """ + get_query_fields(NAME) + """
from functions f,
diff.functions df
where df.pseudocode_hash1 = f.pseudocode_hash1
and df.pseudocode_hash2 = f.pseudocode_hash2
and df.pseudocode_hash3 = f.pseudocode_hash3
and df.pseudocode_hash1 is not null
and df.pseudocode_hash2 is not null
and df.pseudocode_hash3 is not null
and f.instructions > 5
and df.instructions > 5
%POSTFIX%
order by f.source_file = df.source_file""",
"flags":[]
})
NAME = "Similar pseudo-code and names"
HEURISTICS.append({
"name":NAME,
"category":"Partial",
"ratio":HEUR_TYPE_RATIO_MAX,
"sql":"""select distinct """ + get_query_fields(NAME) + """
from functions f,
diff.functions df
where f.pseudocode_lines = df.pseudocode_lines
and f.names = df.names
and df.names != '[]'
and df.pseudocode_lines > 5
and df.pseudocode is not null
and f.pseudocode is not null
%POSTFIX%
order by f.source_file = df.source_file""",
"min": 0.579,
"flags":[]
})
NAME = "Mnemonics small-primes-product"
HEURISTICS.append({
"name":NAME,
"category":"Partial",
"ratio":HEUR_TYPE_RATIO_MAX,
"sql":""" select """ + get_query_fields(NAME) + """
from functions f,
diff.functions df
where f.mnemonics_spp = df.mnemonics_spp
and f.instructions = df.instructions
and f.nodes > 1 and df.nodes > 1
and df.instructions > 5
%POSTFIX% """,
"min":0.6,
"flags":[]
})
# The ORDER BY clause is removed because it was causing serious slowness problems
# with big and huge databases.
NAME = "Same nodes, edges, loops and strongly connected components"
HEURISTICS.append({
"name":NAME,
"category":"Partial",
"ratio":HEUR_TYPE_RATIO_MAX,
"sql":"""select """ + get_query_fields(NAME) + """
from functions f,
diff.functions df
where f.nodes = df.nodes
and f.edges = df.edges
and f.strongly_connected = df.strongly_connected
and f.loops = df.loops
and f.nodes > 5 and df.nodes > 5
and f.loops > 0
and (substr(f.name, 1, 4) = 'sub_' or substr(df.name, 1, 4) == 'sub_')
%POSTFIX% """,
"min":0.549,
"flags":[]
})
# The ORDER BY clause is removed because it was causing serious slowness problems
# with big and huge databases.
NAME = "Same low complexity, prototype and names"
HEURISTICS.append({
"name":NAME,
"category":"Partial",
"ratio":HEUR_TYPE_RATIO_MAX,
"sql":"""
select distinct """ + get_query_fields(NAME) + """
from functions f,
diff.functions df
where f.names = df.names
and f.cyclomatic_complexity = df.cyclomatic_complexity
and f.cyclomatic_complexity < 20
and f.prototype2 = df.prototype2
and df.names != '[]'
%POSTFIX% """,
"min":0.5,
"flags":[]
})
NAME = "Same low complexity and names"
HEURISTICS.append({
"name":NAME,
"category":"Partial",
"ratio":HEUR_TYPE_RATIO_MAX,
"sql":"""select """ + get_query_fields(NAME) + """
from functions f,
diff.functions df
where f.names = df.names
and f.cyclomatic_complexity = df.cyclomatic_complexity
and f.cyclomatic_complexity < 15
and df.names != '[]'
and (substr(f.name, 1, 4) = 'sub_' or substr(df.name, 1, 4) == 'sub_')
%POSTFIX% """,
"min":0.5,
"flags":[]
})
NAME = "Switch structures"
HEURISTICS.append({
"name":NAME,
"category":"Partial",
"ratio":HEUR_TYPE_RATIO_MAX,
"sql":"""select """ + get_query_fields(NAME) + """
from functions f,
diff.functions df
where f.switches = df.switches
and df.switches != '[]'
and f.nodes > 5 and df.nodes > 5
%POSTFIX%
order by f.source_file = df.source_file""",
"min": 0.5,
"flags":[]
})
NAME = "Pseudo-code fuzzy (normal)"
HEURISTICS.append({
"name":NAME,
"category":"Partial",
"ratio":HEUR_TYPE_RATIO_MAX,
"sql":"""select distinct """ + get_query_fields(NAME) + """
from functions f,
diff.functions df
where df.pseudocode_hash1 = f.pseudocode_hash1
and f.pseudocode_lines > 5 and df.pseudocode_lines > 5
%POSTFIX%
order by f.source_file = df.source_file""",
"min": 0.5,
"flags":[]
})
NAME = "Pseudo-code fuzzy (mixed)"
HEURISTICS.append({
"name":NAME,
"category":"Partial",
"ratio":HEUR_TYPE_RATIO,
"sql":"""select distinct """ + get_query_fields(NAME) + """
from functions f,
diff.functions df
where df.pseudocode_hash3 = f.pseudocode_hash3
and f.pseudocode_lines > 5 and df.pseudocode_lines > 5
%POSTFIX%
order by f.source_file = df.source_file""",
"flags":[]
})
NAME = "Pseudo-code fuzzy (reverse)"
HEURISTICS.append({
"name":NAME,
"category":"Partial",
"ratio":HEUR_TYPE_RATIO,
"sql":"""select distinct """ + get_query_fields(NAME) + """
from functions f,
diff.functions df
where df.pseudocode_hash2 = f.pseudocode_hash2
and f.pseudocode_lines > 5 and df.pseudocode_lines > 5
%POSTFIX%
order by f.source_file = df.source_file""",
"flags":[]
})
NAME = "Pseudo-code fuzzy AST hash"
HEURISTICS.append({
"name":NAME,
"category":"Partial",
"ratio":HEUR_TYPE_RATIO_MAX,
"sql":"""select distinct """ + get_query_fields(NAME) + """
from functions f,
diff.functions df
where df.pseudocode_primes = f.pseudocode_primes
and f.pseudocode_lines >= 3
and length(f.pseudocode_primes) >= 35
%POSTFIX%
order by f.source_file = df.source_file""",
"min": 0.35,
"flags":[]
})
NAME = "Partial pseudo-code fuzzy hash (normal)"
HEURISTICS.append({
"name":NAME,
"category":"Partial",
"ratio":HEUR_TYPE_RATIO_MAX,
"sql":""" select distinct """ + get_query_fields(NAME) + """
from functions f,
diff.functions df
where substr(df.pseudocode_hash1, 1, 16) = substr(f.pseudocode_hash1, 1, 16)
and f.nodes > 5 and df.nodes > 5
%POSTFIX%
order by f.source_file = df.source_file""",
"min":0.5,
"flags":[HEUR_FLAG_SLOW, HEUR_FLAG_UNRELIABLE]
})
NAME = "Partial pseudo-code fuzzy hash (reverse)"
HEURISTICS.append({
"name":NAME,
"category":"Partial",
"ratio":HEUR_TYPE_RATIO_MAX,
"sql":""" select distinct """ + get_query_fields(NAME) + """
from functions f,
diff.functions df
where substr(df.pseudocode_hash2, 1, 16) = substr(f.pseudocode_hash2, 1, 16)
and f.nodes > 5 and df.nodes > 5
%POSTFIX%
order by f.source_file = df.source_file""",
"min":0.5,
"flags":[HEUR_FLAG_SLOW, HEUR_FLAG_UNRELIABLE]
})
NAME = "Partial pseudo-code fuzzy hash (mixed)"
HEURISTICS.append({
"name":NAME,
"category":"Partial",
"ratio":HEUR_TYPE_RATIO_MAX,
"sql":""" select distinct """ + get_query_fields(NAME) + """
from functions f,
diff.functions df
where substr(df.pseudocode_hash3, 1, 16) = substr(f.pseudocode_hash3, 1, 16)
and f.nodes > 5 and df.nodes > 5
%POSTFIX%
order by f.source_file = df.source_file""",
"min":0.5,
"flags":[HEUR_FLAG_SLOW, HEUR_FLAG_UNRELIABLE]
})
NAME = "Same rare assembly instruction"
HEURISTICS.append({
"name":NAME,
"category":"Partial",
"ratio":HEUR_TYPE_RATIO_MAX,
"sql":"""
with main_asm as (
select f.id, f.name, inst.disasm
from main.instructions inst,
main.functions f
where f.id = inst.func_id
and f.name not like 'nullsub%'
and inst.disasm is not null
and inst.disasm != ''
group by inst.disasm
having count(0) = 1
),
diff_asm as (
select f.id, f.name, inst.disasm
from diff.instructions inst,
diff.functions f
where f.id = inst.func_id
and f.name not like 'nullsub%'
and inst.disasm is not null
and inst.disasm != ''
group by inst.disasm
having count(0) = 1
),
query1 as (
select distinct main_asm.id main_func_id, diff_asm.id diff_func_id
from main_asm,
diff_asm
where main_asm.disasm = diff_asm.disasm
)
select """ + get_query_fields(NAME) + """
from main.functions f,
diff.functions df,
query1
where f.id = query1.main_func_id
and df.id = query1.diff_func_id
and f.name != df.name
and ((min(f.nodes, df.nodes) * 100) / max(f.nodes, df.nodes)) < 50
%POSTFIX%
""",
"min":0.5,
"flags":[HEUR_FLAG_SAME_CPU]
})
NAME = "Same rare basic block mnemonics list"
HEURISTICS.append({
"name":NAME,
"category":"Partial",
"ratio":HEUR_TYPE_RATIO_MAX,
"sql":"""
with main_bblocks as (
select inst.func_id, bb.basic_block_id bb_id, GROUP_CONCAT(inst.mnemonic) as mnemonics_list, count(0) inst_total
from main.bb_instructions bb,
main.instructions inst
where bb.instruction_id = inst.id
group by bb_id
),
diff_bblocks as (
select inst.func_id, bb.basic_block_id bb_id, GROUP_CONCAT(inst.mnemonic) as mnemonics_list, count(0) inst_total
from diff.bb_instructions bb,
diff.instructions inst
where bb.instruction_id = inst.id
group by bb_id
),
unique_main_bblocks as (
select func_id, mnemonics_list, count(0) total
from main_bblocks
group by mnemonics_list
having count(0) = 1
order by total asc
)
select """ + get_query_fields(NAME) + """
from unique_main_bblocks main_query,
diff_bblocks diff_query,
main.functions f,
diff.functions df
where main_query.mnemonics_list = diff_query.mnemonics_list
and f.id = main_query.func_id
and df.id = diff_query.func_id
and f.nodes > 3
and df.nodes > 3
and diff_query.inst_total >= 6
and ((min(f.nodes, df.nodes) * 100) / max(f.nodes, df.nodes)) < 50
%POSTFIX%
""",
"min":0.5,
"flags":[]
})
NAME = "Loop count"
HEURISTICS.append({
"name":NAME,
"category":"Partial",
"ratio":HEUR_TYPE_RATIO_MAX,
"sql":"""select """ + get_query_fields(NAME) + """
from functions f,
diff.functions df
where f.loops = df.loops
and df.loops > 1
and f.nodes >= 3 and df.nodes >= 3
%POSTFIX%
order by f.source_file = df.source_file""",
"min":0.49,
"flags":[HEUR_FLAG_SLOW]
})
NAME = "Same graph"
HEURISTICS.append({
"name":NAME,