-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathanalyze.py
executable file
·1695 lines (1411 loc) · 51.6 KB
/
analyze.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/env python
"""
Copyright (C) 2017-2018 IAIK TU Graz and Fraunhofer AISEC
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
##
# @package analysis.analyze
# @file analyze.py
# @brief Main analysis script.
# @license This project is released under the GNU GPLv3+ License.
# @author See AUTHORS file.
# @version 0.3
import sys
import os
import fcntl
import click
import struct
import copy
import numpy
import types
import fnmatch
import natsort
from collections import Counter, defaultdict
from itertools import chain
import kuipertest
import rdctest
import datastub
from datastub.SymbolInfo import SymbolInfo
from datastub.utils import debug, debuglevel, set_debuglevel, sorted_keys, progress
from datastub.printer import XmlLeakPrinter, BinLeakPrinter
from datastub.export import storepickle, loadpickle, export_leaks
from datastub.leaks import (
FUNC_ENTRY_BIN,
FUNC_EXIT_BIN,
bs,
CallHistory,
CallStack,
CFLeak,
CFLeakEntry,
Context,
DataLeak,
DataLeakEntry,
Entry,
EvidenceEntry,
EvidenceSource,
Key,
Lookahead,
MergePoint,
NSLeak,
NSPType,
SPLeak,
TraceQueue,
Type,
MaskType,
Leak,
)
import multiprocessing
nospleak = None
"""
*************************************************************************
"""
printer = None
queues = None
leaks = CallHistory()
"""
*************************************************************************
"""
def report_dataleak(callstack, e1, e2):
debug(1, "Data leak@ %08x: %08x vs %08x", (e1.ip, e1.data, e2.data))
if debuglevel(3):
callstack.doprint_reverse()
leak = DataLeak(e1.ip)
leak.append(DataLeakEntry(e1.data))
leak.append(DataLeakEntry(e2.data))
leaks.report_leak(callstack, leak)
def report_cfleak(callstack, bp, mp, e1, len1, e2, len2):
debug(
1,
"Control flow leak@BB %08x, merging@%08x(%s): %08x(%s)(+%d) vs %08x(%s)(+%d)",
(
bp,
mp.ip,
Type(mp.type).name,
e1.ip,
Type(e1.type),
len1,
e2.ip,
Type(e2.type),
len2,
),
)
if debuglevel(3):
callstack.doprint_reverse()
leak = CFLeak(bp)
leak.append(CFLeakEntry(e1, len1, mp.ip))
leak.append(CFLeakEntry(e2, len2, mp.ip))
leaks.report_leak(callstack, leak)
"""
*************************************************************************
"""
# Both queues have advanced by a chunk which is not consumed yet but
# equal. We search for calls and rets in one chunk and apply it to both
# queue's call stacks.
def consume_call_ret(queues):
assert queues[0].chunk is not None
assert len(queues[0].chunk) % bs == 0
cblocks = int(len(queues[0].chunk) / bs)
for i in range(0, cblocks):
idx = 17 * i
typ = queues[0].chunk[idx : idx + 1]
if typ == FUNC_ENTRY_BIN or typ == FUNC_EXIT_BIN:
e = Entry(struct.unpack("<BQQ", queues[0].chunk[idx : idx + 17]))
queues[0].callstack.update_context(e)
queues[1].callstack.update_context(e)
queues[0].chunk = None
queues[1].chunk = None
def fast_forward(queues, bp, bdepth):
# Try at most 5 times to equalize queue size
# First try might not work since conditional branches are stored
# as one element in the file but reported as two elements in the queue.
for _ in range(1, 5):
s0 = queues[0].size()
s1 = queues[1].size()
if s0 > s1:
queues[1].refill(s0 - s1)
elif s1 > s0:
queues[0].refill(s1 - s0)
else:
break
if queues[0].size() != queues[1].size():
debug(
2,
"[fast-forward] queue alignment error %d vs %d",
(queues[0].size(), queues[1].size()),
)
return [queues[0].get(), queues[1].get(), bp, bdepth]
while queues[0].size() > 0:
e1 = queues[0].get()
e2 = queues[1].get()
if e1 is None:
assert e2 is None
break
if e1 == e2:
if Type.isbranch(e1):
bp = e1
bdepth = queues[0].callstack.depth()
continue
else:
return [e1, e2, bp, bdepth]
assert queues[1].size() == 0
# Queues are empty, start fast forward
debug(2, "[fast-forward] starting fast search")
while True:
if not queues[0].load_chunk():
break
if not queues[1].load_chunk():
break
if queues[0].chunk != queues[1].chunk:
break
newbp = queues[0].peak_last_branch_from_chunk()
# It could happen by incredibly bad luck that last chunk does not contain a branch
# In this case reuse previous bp.
if newbp is not None:
bp = newbp
consume_call_ret(queues)
debug(2, "[fast-forward] stopping fast search")
if queues[0].chunk is not None:
queues[0].refill_chunk()
if queues[1].chunk is not None:
queues[1].refill_chunk()
bdepth = queues[0].callstack.depth()
e1 = queues[0].get()
e2 = queues[1].get()
return [e1, e2, bp, bdepth]
"""
*************************************************************************
"""
def iterate_queue(files, fast=True):
global unpacked
global trace
global traces
global queues
bp = None
bdepth = -1
queues = [TraceQueue(files[i], i) for i in range(0, len(files))]
queues[0].id = 0
queues[1].id = 1
while True:
if fast:
[e1, e2, bp, bdepth] = fast_forward(queues, bp, bdepth)
else:
e1 = queues[0].get()
e2 = queues[1].get()
if e1 is None or e2 is None:
if e1 is not None or e2 is not None:
debug(0, "Unbalanced end of trace")
break
if e1 == e2:
# no diff
assert queues[0].callstack == queues[1].callstack
if Type.isbranch(e1):
bp = e1
bdepth = queues[0].callstack.depth()
continue
assert bp is None or Type.isbranch(bp)
if e1.ip != e2.ip:
# This should never happen. We miss some conditional branches in the code
debug(0, "Missed some branch (outer miss) @ %08x vs %08x", (e1.ip, e2.ip))
assert False
if e1.type == e2.type and e1.ip == e2.ip:
if Type.isbranch(e1):
bp = e1
bcallstack = copy.copy(queues[0].callstack)
bdepth = bcallstack.depth()
# We have a control flow leak
debug(1, "CF Leak @ %08x, depth %d", (bp.ip, bdepth))
if debuglevel(3):
queues[0].callstack.doprint_reverse()
lhA = Lookahead(queues[0])
lhB = Lookahead(queues[1])
# Get 2 branches
e1b = queues[0].lookahead(0)
e2b = queues[1].lookahead(0)
foundA = True
foundB = True
while True:
if foundA:
foundA = lhA.advance_next_bp_candidate(bdepth)
if foundB:
foundB = lhB.advance_next_bp_candidate(bdepth)
mergepoint = Lookahead.intersect(lhA, lhB)
if mergepoint is not None:
break
if not foundA and not foundB:
debug(0, "No mergepoint found!")
report_cfleak(
queues[0].callstack,
bp.ip,
MergePoint(Type.FUNC_EXIT, 0, 0),
e1b,
-1,
e2b,
-1,
)
return
assert isinstance(mergepoint, MergePoint)
debug(2, "found mp: %08x, depth %d", (mergepoint.ip, mergepoint.depth))
for mp in lhA.myset:
debug(3, f"lhA {mp.ip:x}")
for mp in lhB.myset:
debug(3, f"lhB {mp.ip:x}")
# Advance to mergepoint
debug(2, "advancing to mp:")
if debuglevel(3):
queues[0].callstack.doprint_reverse()
len1 = queues[0].advance(mergepoint)
len2 = queues[1].advance(mergepoint)
debug(
2,
"advanced to mp: %08x,%08x",
(queues[0].lookahead(0).ip, queues[1].lookahead(0).ip),
)
if debuglevel(3):
queues[0].callstack.doprint_reverse()
assert queues[0].lookahead(0).ip == queues[1].lookahead(0).ip
# assert(queues[0].callstack == queues[1].callstack)
if not queues[0].callstack == queues[1].callstack:
queues[0].callstack.doprint_reverse()
print("====")
queues[1].callstack.doprint_reverse()
assert False
assert Type.isbranch(bp)
report_cfleak(bcallstack, bp.ip, mergepoint, e1b, len1, e2b, len2)
elif Type(e1.type) in (Type.READ, Type.WRITE, Type.HREAD, Type.HWRITE):
# We have a dataleak
assert e1.data != 0
assert e2.data != 0
assert queues[0].callstack == queues[1].callstack
if Type(e1.type) in (Type.HREAD, Type.HWRITE):
e1.data &= 0x00000000FFFFFFFF
e2.data &= 0x00000000FFFFFFFF
if e1.data != e2.data:
report_dataleak(queues[0].callstack, e1, e2)
else:
debug(0, "Unknown type")
assert False
elif Type(e1.type) in (Type.READ, Type.WRITE, Type.HREAD, Type.HWRITE):
if Type(e2.type) in (Type.READ, Type.WRITE, Type.HREAD, Type.HWRITE):
# Mixture of heap and non-heap read/write. Maybe, heap tracking is imprecise
# We require that both elements are either (h)read or (h)write
debug(0, "Imprecise heap tracking @ %08x", (e1.ip))
# assert((e1.type | MaskType.HEAP.value) == (e2.type | MaskType.HEAP.value))
if (e1.type | MaskType.HEAP.value) > 0:
e1.data &= 0x00000000FFFFFFFF
if (e2.type | MaskType.HEAP.value) > 0:
e2.data &= 0x00000000FFFFFFFF
report_dataleak(queues[0].callstack, e1, e2)
else:
# This should never happen. We miss some conditional branches in the code
debug(0, "Missed some branch (inner miss)")
assert False
else:
# This should never happen. We miss some conditional branches in the code
debug(0, "Missed some branch (outer miss) @ %08x vs %08x", (e1.ip, e2.ip))
assert False
"""
*************************************************************************
"""
def loadkeys(directory):
keyfiles = fnmatch.filter(os.listdir(directory), "*.key")
keyfiles = natsort.natsorted(keyfiles)
keys = list()
for k in keyfiles:
with open(os.path.join(directory, k)) as f:
key = f.readline().encode("utf-8")
keys.append(key)
return keys
"""
*************************************************************************
"""
def load_leaks(files, keys, source):
def read_and_advance(chunk, idx, format_type, length):
if format_type == "B":
byte_length = length
elif format_type == "Q":
byte_length = length * 8
else:
assert False
format_character = f"<{length}{format_type}"
data = struct.unpack(format_character, chunk[idx : idx + byte_length])
return (data, idx + byte_length)
if keys is None:
assert False
origin = "fixed" if len(set(keys)) == 1 else "random"
key_index = 0
if origin == "fixed":
# This parsing requires a naming scheme like: `key1.key`
key_index = int(set(keys).pop().replace("key", "").replace(".", ""))
for (trace_file, key_file) in zip(files, keys):
with open(trace_file, "rb") as tf, open(key_file, "rb") as kf:
trace = tf.read()
key = kf.read()
idx = 0
cs = CallStack()
while idx < len(trace):
(data, idx) = read_and_advance(trace, idx, "B", 1)
typ = data[0]
if typ not in [
Type.FUNC_ENTRY.value,
Type.FUNC_EXIT.value,
Type.CFLEAK.value,
Type.DLEAK.value,
]:
debug(0, f"Unknown type: {typ}")
assert False
if typ == Type.FUNC_EXIT.value:
debug(2, "FUNC_EXIT")
cs.doreturn_context()
continue
(data, idx) = read_and_advance(trace, idx, "Q", 2)
if typ == Type.FUNC_ENTRY.value:
(caller, callee) = data
debug(2, "FUNC_ENTRY %x->%x", (caller, callee))
cs.docall_context(Context(caller, callee))
continue
(ip, no) = data
leak = CFLeak(ip) if typ == Type.CFLEAK.value else DataLeak(ip)
debug(2, f"{leak.name} {hex(ip)} ({no})")
(evidence, idx) = read_and_advance(trace, idx, "Q", no)
debug(2, str(evidence))
ee = EvidenceEntry(evidence, Key(key_index, key), source, origin)
leak.add_evidence(ee)
if debuglevel(3):
cs.doprint_reverse()
leaks.report_leak(cs, leak, False)
"""
*************************************************************************
"""
def extract_leakdiff_to_array(A, LeaksOnly=False):
array = []
for leak in A.dataleaks:
if LeaksOnly:
if leak.status.is_generic_leak():
array.append(leak)
else:
array.append(leak)
for leak in A.cfleaks:
if LeaksOnly:
if leak.status.is_generic_leak():
array.append(leak)
else:
array.append(leak)
for k in A.children:
child = A.children[k]
array += extract_leakdiff_to_array(child, LeaksOnly)
return array
"""
*************************************************************************
"""
def _glt_gather_information(leak):
num = defaultdict(int)
num_uniq = defaultdict(int)
dic = defaultdict(int)
for e in leak.evidence:
if e.source != EvidenceSource.Generic.value:
continue
entries = e.entries
if len(entries) == 0:
continue
# Type1a
cn = len(entries)
num[cn] += 1
# Type1b
cnu = len(set(entries))
num_uniq[cnu] += 1
# Type2
counts = Counter(entries)
for c in counts.keys():
dic[c] += counts[c]
return (num, num_uniq, dic)
def _glt_sanity_check_abort(lengths, limit=1):
for length in lengths:
if length < limit:
return True
return False
def _glt_sort_and_map(mapping_table, data):
tmp_data = dict()
# Sort data from big to little wrt to the counts
data = sorted(data.items(), key=lambda kv: kv[1], reverse=True)
# Map addr to new value
for (address, count) in data:
if address not in mapping_table:
mapping_table[address] = len(mapping_table.keys())
tmp_data[mapping_table[address]] = count
return tmp_data
def _glt_solve_entry_mismatches(fnum, rnum):
fset = set(fnum.keys())
rset = set(rnum.keys())
if fset != rset:
for s in list(fset - rset):
rnum[s] = 0
for s in list(rset - fset):
fnum[s] = 0
return (fnum, rnum)
def _glt_compile_histograms(num):
hist = numpy.array([num[j] for j in sorted(num.keys())], dtype=numpy.float32)
hist_len = numpy.int32(numpy.sum(hist))
return (hist, hist_len)
def _glt_do_kuipertest(fhist, rhist, fhist_len, rhist_len, confidence=0.9999):
(D, L) = kuipertest.kp_histogram(fhist, rhist, fhist_len, rhist_len, confidence)
assert not (numpy.isnan(D) or numpy.isnan(L))
R = D > L
return (D, L, R, confidence)
def generic_leakage_test1a(msgleak, fl, key_index, key, fnum, rnum):
test = {"idx": "1a", "nsp_type": NSPType.Type1a}
return generic_leakage_testX(msgleak, fl, key_index, key, fnum, rnum, test)
def generic_leakage_test1b(msgleak, fl, key_index, key, fnum_uniq, rnum_uniq):
test = {"idx": "1b", "nsp_type": NSPType.Type1b}
return generic_leakage_testX(
msgleak, fl, key_index, key, fnum_uniq, rnum_uniq, test
)
def generic_leakage_test2(msgleak, fl, key_index, key, fdic, rdic):
test = {"idx": "2", "nsp_type": NSPType.Type2}
return generic_leakage_testX(msgleak, fl, key_index, key, fdic, rdic, test)
def generic_leakage_test2a(msgleak, fl, key_index, key, fdic, rdic):
mapping_table = dict()
fdic = _glt_sort_and_map(mapping_table, fdic)
rdic = _glt_sort_and_map(mapping_table, rdic)
test = {"idx": "2a", "nsp_type": NSPType.Type2a}
return generic_leakage_testX(msgleak, fl, key_index, key, fdic, rdic, test)
def generic_leakage_testX(msgleak, fl, key_index, key, fixed_data, random_data, test):
test_idx = test["idx"]
nsp_type = test["nsp_type"]
key = Key(key_index, key)
abort_cfl = NSLeak(nsp_type, key)
abort_msgleak = msgleak + f" [Test{test_idx}] -- {str(abort_cfl)}\n"
if _glt_sanity_check_abort([len(fixed_data), len(random_data)]):
debug(1, f"Abort Test{test_idx} key_index={key_index} with SampleError.")
fl.status.nsleak += [abort_cfl]
return (abort_msgleak, fl)
(fixed_data, random_data) = _glt_solve_entry_mismatches(fixed_data, random_data)
(fhist, fhist_len) = _glt_compile_histograms(fixed_data)
(rhist, rhist_len) = _glt_compile_histograms(random_data)
if _glt_sanity_check_abort([fhist_len, rhist_len], 30):
debug(1, f"Abort Test{test_idx} key_index={key_index} with HistError.")
fl.status.nsleak += [abort_cfl]
return (abort_msgleak, fl)
(D, L, R, confidence) = _glt_do_kuipertest(fhist, rhist, fhist_len, rhist_len)
cfl = NSLeak(nsp_type, key, None, D, L, confidence, R)
fl.status.nsleak += [cfl]
msgleak += f" [Test{test_idx}] -- {str(cfl)}\n"
return (msgleak, fl)
def generic_leakage_test(fixed, random):
fixedleaks = extract_leakdiff_to_array(fixed)
randomleaks = extract_leakdiff_to_array(random)
assert len(fixedleaks) == len(randomleaks)
# print test types
debug(1, "Test Types:")
debug(1, " 1a ... number of addresses")
debug(1, " 1b ... number of unique addresses")
debug(1, " 2 .... number of accesses per address")
debug(1, "")
# iterate over leaks
debug(0, "Got %d trace differences.", (len(fixedleaks)))
sys.stdout.flush()
for (idx, (fl, rl)) in enumerate(zip(fixedleaks, randomleaks)):
msg = {"warning": "", "leak": ""}
assert fl.ip == rl.ip
# parse key_index and key
key_index = set(e.key.index for e in fl.evidence)
key = set(e.key.value for e in fl.evidence)
## Check if `fl` only contains fixed traces
if len(key_index) != 1 or len(key) != 1:
assert False
key_index = key_index.pop()
key = key.pop()
# always test
leaktype = "dataleak" if isinstance(fl, DataLeak) else "cfleak"
msg_start = f"Testing {leaktype}@{fl.ip:x}...\n"
msg["warning"] += msg_start
msg["leak"] += msg_start
# set is_generic_tested to True
fl.status.nsperformed = True
# sanity check
if _glt_sanity_check_abort([len(fl.evidence), len(rl.evidence)]):
msg["warning"] += f" warning: {len(fl.evidence)} evidences for fixed\n"
msg["warning"] += f" warning: {len(fl.evidence)} evidences for random\n"
debug(0, msg["warning"])
continue
(fnum, fnum_uniq, fdic) = _glt_gather_information(fl)
(rnum, rnum_uniq, rdic) = _glt_gather_information(rl)
# Test1a: number of addresses
(msg["leak"], fl) = generic_leakage_test1a(
msg["leak"], fl, key_index, key, fnum, rnum
)
# Test1b: number of unique addresses
(msg["leak"], fl) = generic_leakage_test1b(
msg["leak"], fl, key_index, key, fnum_uniq, rnum_uniq
)
# Test2: number of accesses per address
(msg["leak"], fl) = generic_leakage_test2(
msg["leak"], fl, key_index, key, fdic, rdic
)
# Test2a: number of accesses per address with sorted and mapped data
(msg["leak"], fl) = generic_leakage_test2a(
msg["leak"], fl, key_index, key, fdic, rdic
)
debug(1, msg["leak"])
progress(idx, len(fixedleaks))
sys.stdout.flush()
"""
*************************************************************************
"""
def spe_testfunction_initialize(_X_labels, _xtarget):
global X_labels, xtarget
X_labels = _X_labels
xtarget = _xtarget
"""
*************************************************************************
"""
def spe_testfunction(input):
# global X_labels
rli, xarr, property_idx, dict_value, dict_key, nsptype = input
debug(
3,
"spe_testfunction: {}, {}, {}, {} START".format(
property_idx, X_labels[property_idx], nsptype, dict_key
),
)
(R, L, I) = rdctest.RDC.test(xarr, dict_value, 0.9999)
debug(
3,
"spe_testfunction: {}, {}, {}, {} END".format(
property_idx, X_labels[property_idx], nsptype, dict_key
),
)
if I is not None and bool(I) is False:
leak = SPLeak(
nsptype,
X_labels[property_idx],
dict_key,
None,
R,
L,
I,
xtarget,
0.9999,
)
debug(1, "Found leak [Test2+3]: %s", (str(leak)))
return (rli, leak)
return (rli, None)
# Specific leakage test requires a callback function that gets a list
# of inputs (e.g. keys) and returns a 2-dimensional numpy array. The
# rows select a specific input, the columns select a specific leakage
# value of that input.
#
# For instance: the function is given a list of 10 AES-128 keys. Each key
# has 16 bytes and the leakage model is the Hamming Weight per key byte.
# Thus, the function creates a 10x16 numpy array that contains 10 rows
# and 16 Hamming Weights per row.
#
# The callback function signature is:
# def specific_leakage_callback(inputs):
# a = numpy.ndarray((len(inputs), ...))
# ...
# return a
#
def specific_leakage_test(random, callback, keys, LeaksOnly=True, mp=False):
global nospleak
# load callback function
debug(1, "Loading specific leakage model")
try:
with open(callback) as fp:
code = compile(fp.read(), callback, "exec")
splcb = types.ModuleType("<config>")
exec(code, splcb.__dict__)
except Exception as e:
debug(0, "Unable to load specific leakage test callback function!")
debug(0, str(e))
assert False
assert splcb.specific_leakage_callback
xtarget = str(os.path.splitext(os.path.basename(callback))[0])
nospleak = SPLeak(NSPType.Noleak, target=xtarget)
# print test types
debug(1, "Test Types:")
debug(1, " 2 .... number of accesses per address")
debug(1, " 3 .... position of address during access")
debug(1, "")
# process leaks
randomleaks = extract_leakdiff_to_array(random, LeaksOnly=LeaksOnly)
debug(0, "Got %d leaks.", (len(randomleaks)))
sys.stdout.flush()
# convert keys with callback
# the callback always returns the matrix X:
#
# x_{0,0} x_{0,1} ... x_{0,N}
# x_{1,0} x_{1,1} ... x_{1,N}
# ...
# x_{M,0} x_{M,1} ... x_{M,N}
#
# M ... number of keys (one row per key)
# N ... number of properties in X (one column per property)
debug(1, "Building leakage model input from keys")
X = splcb.specific_leakage_callback(keys)
X_labels = []
# unpack specific leakage result tuple to extract labels
if type(X) == tuple:
X, X_labels = X
else:
X_labels = range(0, X.shape[1])
assert type(X) == numpy.ndarray
assert len(X_labels) == X.shape[1]
Xglob = dict(zip(keys, X))
# Todo: make use of above X/X_labels in below code.
# Beware of extracting only needed entries
# Multiprocessing-queue
queue = list()
for rli in range(0, len(randomleaks)):
debug(1, "Loading leak %d/%d", (rli, len(randomleaks)))
rl = randomleaks[rli]
noleakdetected = True
leaktype = "dataleak" if isinstance(rl, DataLeak) else "cfleak"
debug(1, "Testing %s@%x...", (leaktype, rl.ip))
cursym = SymbolInfo.lookup(rl.ip)
if (cursym is not None) and (len(cursym.name) > 0):
cursym = cursym.name[0]
else:
cursym = None
rl.status.spperformed.add(xtarget)
# sanity check
if len(rl.evidence) == 0:
debug(0, "Warning: no evidences")
continue
# gather information -- leaks
rdic = {}
cnt_t2 = 0
rdic_pos = {}
cnt_t3 = 0
keys = []
for e in rl.evidence:
if len(e.entries) == 0:
debug(3, "Empty evidence entries")
continue
if e.source != EvidenceSource.Specific.value:
continue
# all entries
selentries = e.entries
# gather information -- keys
if e.key is None or len(e.key) == 0:
debug(0, "Error: Key is empty: %s", ((e.key)))
assert False
keys.append(e.key)
# gather information -- type2
chist = Counter(selentries)
rset = set(rdic.keys())
cset = set(chist.keys())
for s in list(cset - rset):
rdic[s] = [0] * cnt_t2
for c in rdic.keys():
if c in chist.keys():
rdic[c] += [chist[c]]
else:
rdic[c] += [0]
cnt_t2 += 1
# gather information -- type3
cs = set(selentries)
for c in cs:
cp = [pos for pos, j in enumerate(selentries) if j == c]
if c not in rdic_pos.keys():
rdic_pos[c] = [-1] * cnt_t3
rdic_pos[c] += [int(numpy.round(numpy.median(cp)))]
for s in list(set(rdic_pos.keys()) - cs):
rdic_pos[s] += [-1]
cnt_t3 += 1
if len(keys) == 0:
debug(1, "Warning: Keys are empty. No evidences")
continue
# postprocessing -- type2
for c in rdic.keys():
rdic[c] = numpy.asarray(rdic[c], dtype=numpy.uint64)
# postprocessing -- type3
for c in rdic_pos.keys():
rdic_pos[c] = numpy.array(rdic_pos[c], dtype=numpy.int64)
# Extract X in correct order
X = numpy.asarray([Xglob[k.get_bytes().decode().encode()] for k in keys])
assert type(X) == numpy.ndarray
assert len(X_labels) == X.shape[1]
if X.shape[0] != len(keys):
debug(0, "Warning: callback returned wrong matrix!")
continue
######
# Test2: number of accesses per address
# Test3: position of address during access
######
def report_nospleak(rl):
global nospleak
rl.status.spleak.add(nospleak)
leaktype = "dataleak" if isinstance(rl, DataLeak) else "cfleak"
debug(2, "Reporting %s@%x: %s", (leaktype, rl.ip, str(nospleak)))
def report_spleak(rl, cleak):
rl.status.spleak.add(cleak)
leaktype = "dataleak" if isinstance(rl, DataLeak) else "cfleak"
debug(1, "Reporting %s@%x: %s", (leaktype, rl.ip, str(cleak)))
if mp:
# Multiproccesing: prepare queue for later analysis
# Test 2
queue.extend(
[
(rli, X[:, prop], prop, rdic[k], k, NSPType.Type2)
for prop in range(0, X.shape[1])
for k in rdic.keys()
]
)
# Test 3
queue.extend(
[
(rli, X[:, prop], prop, rdic_pos[k], k, NSPType.Type3)
for prop in range(0, X.shape[1])
for k in rdic_pos.keys()
]
)
else:
# Single-threaded: do analysis immediately
cleaks = list()
spe_testfunction_initialize(X_labels, xtarget)
for prop in range(0, X.shape[1]):
for k in rdic.keys():
cleaks.append(
spe_testfunction(
(rli, X[:, prop], prop, rdic[k], k, NSPType.Type2)
)[1]
)
for k in rdic_pos.keys():
cleaks.append(
spe_testfunction(
(rli, X[:, prop], prop, rdic_pos[k], k, NSPType.Type3)
)[1]
)
# Collect results
for cleak in cleaks:
if cleak:
noleakdetected = False
report_spleak(rl, cleak)
# No leaks
if noleakdetected:
report_nospleak(rl)
# Print progress
if len(randomleaks) > 100:
if (rli % int(len(randomleaks) / 10)) == 0:
debug(0, "[Progress] %6.2f%%", ((rli * 100.0) / len(randomleaks)))
else:
debug(0, "[Progress] %d/%d", (rli + 1, len(randomleaks)))
sys.stdout.flush()
if mp:
pool_size = multiprocessing.cpu_count()
# os.system('taskset -cp 0-%d %s' % (pool_size, os.getpid()))
debug(1, "Processing all leaks in parallel")
with multiprocessing.Pool(
pool_size,
spe_testfunction_initialize,
initargs=(
X_labels,
xtarget,
),
) as pool:
debug(1, "Collecting results")
results = pool.map(spe_testfunction, queue)
rlset = set()
rlall = set()
for result in results:
rli, cleak = result
rl = randomleaks[rli]
rlall.add(rl)
if cleak:
rlset.add(rl)
report_spleak(rl, cleak)
# No leaks
for rl in rlall.difference(rlset):
report_nospleak(rl)
debug(0, "Finished specific analysis")
sys.stdout.flush()
"""
*************************************************************************
"""
def precompute_single(input):
# global precompute_rdc_file
# global precompute_alpha
N = input
debug(1, "Precomputing RDC for N=%d, alpha=%f", (N, precompute_alpha))
limit = rdctest.RDC.rdc_sigthres_compute(N, precompute_alpha)
debug(1, "RDC_limit=%f for N=%d, alpha=%f", (limit, N, precompute_alpha))
with open(precompute_rdc_file, "a") as f:
fcntl.flock(f, fcntl.LOCK_EX)
f.write("%4d: %f,\n" % (N, limit))
fcntl.flock(f, fcntl.LOCK_UN)
return (N, limit)
"""
*************************************************************************
"""