-
Notifications
You must be signed in to change notification settings - Fork 0
/
attacks.py
1199 lines (1157 loc) · 49.2 KB
/
attacks.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
import diffixRef
import whereParser
import rowFiller
import statistics
import sqlite3
import pprint
import re
import requests
import pandas as pd
import numpy as np
from datetime import date,datetime
import os.path
import shutil
class tally:
''' Used to tally up and report overall results.
Each class of attack has multiple variants.
Each attack runs multiple times, recording the number of successes (correct guesses
from the attacker).
tally keeps stats on how well each of the variants does
'''
def __init__(self):
self.pp = pprint.PrettyPrinter(indent=4)
self.t = {}
def addResult(self,sysType,atk,numCorrect,numGuess,totalTrials,reason='',doprint=True):
today = date.today()
runDate = today.strftime("%Y-%m-%d")
atk.attack['runDate'] = runDate
atk.attack['sysType'] = sysType
vStr,vNum = atk.dr.getVersion()
atk.attack['version'] = vStr
atk.attack['versionOrder'] = vNum
if atk.name not in self.t:
self.t[atk.name] = {
'attacks': [atk.attack],
'numGuess': [numGuess],
'numCorrect': [numCorrect],
'totalTrials': [totalTrials],
'reason': [reason],
}
else:
self.t[atk.name]['attacks'].append(atk.attack)
self.t[atk.name]['numGuess'].append(numGuess)
self.t[atk.name]['numCorrect'].append(numCorrect)
self.t[atk.name]['totalTrials'].append(totalTrials)
self.t[atk.name]['reason'].append(reason)
def updateTable(self,doReplace=True):
# Set to True if duplicate attack rows should replace current ones
# (Do this when some code change is likely to produce a different result)
resDir = 'results'
dbPath = os.path.join(resDir,'current.db')
# This is the basic table definition. Adding to this will cause new
# columns to be inserted into the table (default NULL). Must add new
# column to _makeDictFromAttack
self.colDef = {
'atk_type':'text',
'atk_sub_type':'text',
'sys_type':'text',
'sys_ver':'text',
'sys_ver_num':'integer',
'atk_date':'text',
'num_guess':'integer',
'num_right':'integer',
'total_trials':'integer',
'reason':'text',
'confidence':'real',
'probability':'real',
}
self.table = 'results'
# Now open database (or create it, if this is the first time)
conn = sqlite3.connect(dbPath)
cur = conn.cursor()
sql = f'''create table if not exists {self.table} ( '''
for col,typ in self.colDef.items():
sql += f'''{col} {typ}, '''
sql = sql[:-2]
sql += ''')'''
cur.execute(sql)
conn.commit()
# Read in the sql database as a dataframe
dfRes = pd.read_sql('select * from results',conn)
# After this, we jus work with the dataframe, and write it back to sql when done
conn.close()
updated = False
# See if there are any new columns defined since the last store, and if so add them
columns = list(dfRes)
for col in self.colDef.keys():
if col not in columns:
print(f"Adding new column {col}")
updated = True
doReplace = True
dfRes[col] = np.nan
# Now we want to add rows for any new data, but not if data is already in the df
for atk.name,res in self.t.items():
for i in range(len(res['attacks'])):
attack = res['attacks'][i]
# The following attributes uniquely define the attack
dfMatch = dfRes.loc[(dfRes['atk_type'] == attack['atk_type']) &
(dfRes['atk_sub_type'] == attack['describe']) &
(dfRes['sys_type'] == attack['sysType']) &
(dfRes['sys_ver'] == attack['version']) ]
lenMatch = len(dfMatch.index)
if lenMatch == 0:
# make a dataframe from the new row and append it to the existing one
row = self._makeDictFromAttack(i,atk.name,res)
dfNew = pd.DataFrame.from_dict(row)
print("Adding following row to results:")
print(dfNew)
dfRes = dfRes.append(dfNew,ignore_index=True)
updated = True
elif lenMatch > 1:
print("ERROR: updateTable: should not have multiple matches")
print(atk.name,i)
self.pp.pprint(res)
print(dfMatch)
quit()
elif doReplace:
row = self._makeDictFromAttack(i,atk.name,res)
dfNew = pd.DataFrame.from_dict(row)
print("Replacing following results row:")
print(dfMatch)
print("With this row:")
print(dfNew)
index = dfMatch.index[0]
print(index)
dfRes = dfRes.drop([index])
dfRes = dfRes.append(dfNew,ignore_index=True)
updated = True
if updated:
# write new sql table
# First make a backup of the data results
now = datetime.now()
bkName = now.strftime("%d_%m_%Y_%H_%M_%S") + '.backup'
bkPath = os.path.join(resDir,bkName)
if os.path.exists(dbPath):
shutil.copyfile(dbPath,bkPath)
# Then make new table
conn = sqlite3.connect(dbPath)
dfRes.to_sql(self.table,conn,if_exists='replace',index=False)
conn.close()
def _makeDictFromAttack(self,i,atk_type,res):
if res['numGuess'][i]:
confidence = res['numCorrect'][i] / res['numGuess'][i]
else:
confidence = None
probability = res['numGuess'][i] / res['totalTrials'][i]
return {
'atk_type':[atk_type],
'atk_sub_type':[res['attacks'][i]['describe']],
'sys_type':[res['attacks'][i]['sysType']],
'sys_ver':[res['attacks'][i]['version']],
'sys_ver_num':[res['attacks'][i]['versionOrder']],
'atk_date':[res['attacks'][i]['runDate']],
'num_guess':[res['numGuess'][i]],
'num_right':[res['numCorrect'][i]],
'total_trials':[res['totalTrials'][i]],
'reason':[res['reason'][i]],
'confidence':confidence,
'probability':probability,
}
def printResults(self):
for atk.name,res in self.t.items():
print(f"{atk.name}: ({len(res['numCorrect'])} attack variants)")
print(atk.attack['long'])
confidences = []
guessProbs = []
for i in range(len(res['numCorrect'])):
if res['numGuess'][i]:
confidences.append(res['numCorrect'][i]/res['numGuess'][i])
else:
confidences.append(0)
guessProbs.append(res['numGuess'][i]/res['totalTrials'][i])
print(" Confidence:")
print(f" avg: {statistics.mean(confidences)}")
maxi = confidences.index(max(confidences))
print(f" max: {confidences[maxi]} ({res['attacks'][maxi]['describe']})")
mini = confidences.index(min(confidences))
print(f" min: {confidences[mini]} ({res['attacks'][mini]['describe']})")
if len(res['reason'][mini]) > 0:
print(f" reason: {res['reason'][mini]}")
print(" Guess Probability:")
print(f" avg: {statistics.mean(guessProbs)}")
maxi = guessProbs.index(max(guessProbs))
print(f" max: {guessProbs[maxi]} ({res['attacks'][maxi]['describe']})")
mini = guessProbs.index(min(guessProbs))
print(f" min: {guessProbs[mini]} ({res['attacks'][mini]['describe']})")
class attackBase:
'''
This is the base class for all attacks (which are sub-classes of this base)
'''
long = ''
def __init__(self,attack,tally,defaultRef,doPrint=False):
self.pp = pprint.PrettyPrinter(indent=4)
self.name = self.__class__.__name__
self.attack = attack
self.attack['long'] = self.long
self.attack['atk_type'] = self.name
self.tally = tally
self.defaultRef = defaultRef
self.dop = False
if doPrint or ('doprint' in self.attack and self.attack['doprint'] is True):
self.dop = True
# build attack database
self.sw = whereParser.simpleWhere(attack['table']['conditionsSql'])
self.rf = rowFiller.rowFiller(self.sw,printIntermediateTables=False,dop=self.dop)
self.rf.makeBaseTables()
if len(self.rf.failedCombinations) > 0:
print("Failed Combinations:")
print(self.rf.failedCombinations)
# Strip, then append
for change in attack['table']['changes']:
if change['change'] == 'strip':
self.rf.stripDf(change['table'],change['query'])
for change in attack['table']['changes']:
if change['change'] == 'append':
self.rf.appendDf(change['table'],change['spec'])
self.rf.baseTablesToDb()
self.makeAttackQueries()
self.dr = diffixRef.diffixRef()
self.defaultAids={'tab':{'aid_columns':['aid1']}}
def getDiffixParams(self):
diffix = {
'lcfMin':1.5,
'lcfMax':5.5,
'lcfDist':'uniform',
}
return diffix
def printRawTable(self):
print(self.rf.baseDf)
def print(self):
self.pp.pprint(self.attack)
def _updateParamsLoop(self,par,ref,keyStack):
for key,val in ref.items():
if key not in par:
self._error(f"ERROR: _updateParams: bad key {keyStack},{key}")
keyStack.append(key)
if type(val) is not dict:
par[key] = val
continue
else:
self._updateParamsLoop(par[key],ref[key],keyStack)
def _updateParams(self,refParams):
params = self.defaultRef.copy()
self._updateParamsLoop(params,refParams,[])
return params
def _queryDbRaw(self,sql):
self.conn = sqlite3.connect(self.rf.getDbPath())
self.cur = self.conn.cursor()
self.cur.execute(sql)
answer = self.cur.fetchall()
self.conn.close()
return answer
def _queryDbRef(self,sql,seed):
if 'refParams' in self.attack:
params = self._updateParams(self.attack['refParams'])
else:
params = self.defaultRef
if 'aids' in self.attack:
aids = self.attack[aids]
else:
aids = self.defaultAids
db = self.rf.getDbPath()
rtn = self.dr.query(sql,seed,aids,db,params)
if rtn['success']:
return rtn['answer']
else:
return None
def queryDb(self,dbType,sql,seed=1):
if dbType == 'raw':
return self._queryDbRaw(sql)
elif dbType == 'ref':
return self._queryDbRef(sql,seed)
self._error('''ERROR: queryDb: shouldn't get here''')
def runAllRefQueries(self,numClaims):
# This makes a query list for one seed
queryList = []
for queryGroup in self.queries:
for query in queryGroup:
queryList.append(query)
# Now run queries for `numClaims` seeds
aids,db,params = self._getRefStuff()
self.dr.queryAll(queryList,numClaims,aids,db,params)
self.ansIter = iter(self.dr.iterAnswers())
def makeAttackQueries(self):
''' Makes a list of query groups, each group having one or more queries
'''
self.queries = []
if 'sqls' in self.attack['attackQueries']:
queryGroup = []
for sql in self.attack['attackQueries']['sqls']:
queryGroup.append(self._doSqlReplace(sql))
repeats = 1 # default
if 'repeats' in self.attack['attackQueries']:
repeats = self.attack['attackQueries']['repeats']
for _ in range(repeats):
self.queries.append(queryGroup)
elif 'attackTemplates' in self.attack['attackQueries']:
for attackVal in self.attack['attackQueries']['attackVals']:
queryGroup = []
for sql in self.attack['attackQueries']['attackTemplates']:
queryGroup.append(sql.replace('---',str(attackVal)))
self.queries.append(queryGroup)
else:
self._error('''ERROR: makeAttackQueries: missing label''')
def attackQueriesDisallowed(self):
aids,db,params = self._getRefStuff()
self.dr.queryAll(self.queries[0],1,aids,db,params)
i = 0
for answer in self.dr.iterAnswers():
if answer is None:
return self.queries[0][i]
i += 1
return None
def _getRefStuff(self):
if 'refParams' in self.attack:
params = self._updateParams(self.attack['refParams'])
else:
params = self.defaultRef
if 'aids' in self.attack:
aids = self.attack[aids]
else:
aids = self.defaultAids
db = self.rf.getDbPath()
return aids,db,params
def runQueries(self,dbType,seed):
# For each query in the query list of lists, we record an answer in
# the same list of lists position
self.answers = []
if self.dop: print(f"DB type {dbType} (seed {seed})")
for queryGroup in self.queries:
ansGroup = []
for query in queryGroup:
if dbType != 'ref':
ans = self.queryDb(dbType,query,seed=seed)
else:
ans = next(self.ansIter)
if self.dop:
print(query)
self.pp.pprint(ans)
ansGroup.append(ans)
self.answers.append(ansGroup)
def _doSqlReplace(self,sql):
cols = re.findall('-..-',sql)
for col in cols:
val = self.rf.getNewRowColumn(col[1:3])
pattern = f"-{col[1:3]}-"
sql = re.sub(pattern,str(val),sql)
return sql
def _error(self,msg):
print(msg)
self.pp.pprint(self.attack)
quit()
def runAttackTest(self):
return self.runAttack(db='raw')
def runAttack(self,db='dummy'):
print("ERROR: Every subclass must have a runAttack function")
quit()
def doCheck(self):
self.check = self.attack['check']
def _sortAnsByBucket(self,ans):
s = {}
for row in ans:
s[row[0]] = row[1]
return s
def _bucketIsSuppressed(self, bucketVal):
for bucket in self.answers[0][0]:
if bucket[0] == bucketVal:
return False
return True
class simpleAveraging(attackBase):
long = 'In the simple averaging attack, our goal is to remove noise by simply repeating ' + \
'the exact same query on the hope that the noise sample will be different each time, ' + \
'and the noise will average away. Diffix defeats this attack with sticky noise.'
def runAttack(self,db='ref',seed=1):
self.runQueries(db,seed)
sumCounts = 0
for ansGroup in self.answers:
sumCounts += ansGroup[0][0][0]
averagedCount = sumCounts / len(self.answers)
if averagedCount == self.exactCount:
return 'CORRECT'
else:
return 'WRONG'
def doCheck(self):
sql1 = self._doSqlReplace(self.queries[0][0])
self.exactCount = self.queryDb('raw',sql1)[0][0]
class simpleStrongSuppressDetect(attackBase):
long = "This attack exploits Diffix' suppression mechanism. " + \
'The idea here is that an attacker knows that there are N AIDs with a certain ' + \
'column value, and wants to figure out if the N+1th AID also has that value. ' + \
'In the "strong" case, N is equal to the maximum number of distinct AIDs that ' + \
'are always suppressed. Therefore if the bucket is not suppressed, the victim ' + \
'must be present.'
def runAttack(self,db='ref',seed=1):
self.runQueries(db,seed)
suppressed = self._bucketIsSuppressed(self.check['bucketVal'])
# alwaysSuppressed is the max number of AIDs that is always suppressed
alwaysSuppressed = int(self.attack['refParams']['low_count_threshold']['lower'])
if self.check['numKnown'] == alwaysSuppressed - 1:
# This is the case where, if the answer is not suppressed, then it MUST be
# that the victim is present
if not suppressed:
return 'CORRECT'
return 'NO GUESS'
class splitAveraging(attackBase):
long = 'In the split averaging attack, the attacker attempts to defeat sticky noise by ' + \
'creating pairs of queries where each pair collectively has the same underlying ' + \
'set of AIDs, but the individual queries have different AIDs. This is done with ' + \
'a pair of WHERE clauses as "WHERE col = val" and "WHERE col <> val". These two ' + \
'taken together include all users. Each pair of queries uses a different val ' +\
'with the result that each individual bucket has different AIDs. This attack ' +\
'works against Diffix Publish, but not the other Diffix variants.'
def runAttack(self,db='ref',seed=1):
self.runQueries(db,seed)
sumCounts = 0
for ansGroup in self.answers:
sumCounts += ansGroup[0][0][0]
sumCounts += ansGroup[1][0][0]
averagedCount = sumCounts / len(self.answers)
if averagedCount == self.exactCount:
return 'CORRECT'
else:
return 'WRONG'
def doCheck(self):
sql = self._doSqlReplace(self.attack['checkQuery'])
self.exactCount = self.queryDb('raw',sql)[0][0]
class simpleSoftDifference(attackBase):
long = 'The simple soft difference attack attempts to learn if an AID is present or absent ' + \
'from whether there is a difference between a pair of queries whereby one query ' + \
'(the "left" query) ' + \
'definately includes or excludes the victim, and the other query (the "right" query) ' + \
'may or may not include the victim. This "soft" version of the attack simply looks ' + \
'to see if the right answer is above or below a threshold that falls in the middle of ' + \
'the true difference between the left and right counts. ' + \
'The attack is not perfect against Diffix Publish because sometimes a difference ' + \
'in noise will produce the same noisy count. '
def runAttack(self,db='ref',seed=1):
self.runQueries(db,seed)
diff = self.answers[0][0][0][0] - self.answers[0][1][0][0]
if (self.check['correctIsLess'] and diff < self.check['threshold'] or
self.check['correctIsLess'] is False and diff >= self.check['threshold']):
return 'CORRECT'
else:
return 'WRONG'
class simpleHardDifference(attackBase):
long = 'The simple hard difference attack attempts to learn if an AID is present or absent ' + \
'from whether there is a difference between a pair of queries whereby one query ' + \
'(the "left" query) ' + \
'definately includes or excludes the victim, and the other query (the "right" query) ' + \
'may or may not include the victim. This "hard" version of the attack simply looks ' + \
'to see if right and left answers differ at all. Diffix Publish is vulnerable to this ' + \
'because it uses only a single AID noise layer. '
def runAttack(self,db='ref',seed=1):
self.runQueries(db,seed)
if ((self.check['correctIfDifferent'] and
self.answers[0][0][0][0] != self.answers[0][1][0][0]) or
(not self.check['correctIfDifferent'] and
self.answers[0][0][0][0] == self.answers[0][1][0][0])):
return 'CORRECT'
else:
return 'WRONG'
class simpleFirstDerivitiveDifference(attackBase):
long = 'In this variant of the difference attack, the attacker builds multiple pairs of ' + \
'queries, with the left and right query each being part of a left and right histogram. ' + \
'In this attack, left and right for each pair will differ, but the difference will be ' + \
'the same for all pairs except the one with the victim. In Publish, this sometimes ' + \
'fails to allow a guess because different noise will produce identical noisy counts '
def runAttack(self,db='ref',seed=1):
self.runQueries(db,seed)
ans1 = self.answers[0][0]
ans2 = self.answers[0][1]
sort1 = self._sortAnsByBucket(ans1)
sort2 = self._sortAnsByBucket(ans2)
diffs = {}
for bucket,count1 in sort1.items():
if bucket in sort2:
count2 = sort2[bucket]
diff = count2 - count1
if diff in diffs:
diffs[diff].append(bucket)
else:
diffs[diff] = [bucket]
if len(diffs) == 2:
for diff,buckets in diffs.items():
if len(buckets) == 1:
if buckets[0] != self.check['victimBucket']:
return 'WRONG'
else:
return 'CORRECT'
return 'NO GUESS'
class simpleListUsers(attackBase):
long = 'The simple list attack simply tries to list the individual rows of one or more ' + \
'columns. The "test" for whether the attack succeeded is simply to compare the number ' + \
'rows received against the true number '
def runAttack(self,db='ref',seed=1):
self.runQueries(db,seed)
# Simply checking the number of rows in the answer against the raw data
# query isn't a very thorough check, but I'm assuming that even this will
# fail so no need to go further
if len(self.answers[0][0]) == len(self.checkAns):
return 'CORRECT'
else:
return 'WRONG'
def doCheck(self):
sql1 = self._doSqlReplace(self.queries[0][0])
self.checkAns = self.queryDb('raw',sql1)
class justTesting(attackBase):
long = ''
def runAttack(self,db='ref',seed=1):
self.runQueries(db,seed)
return 'CORRECT'
# Default reference anonymization parameters
defaultRef = {
'low_count_threshold': {'lower': 2, 'upper': 5},
'noise': {'cutoff': 5.0, 'standard_dev': 1.0},
'outlier_count': {'lower': 1, 'upper': 2},
'top_count': {'lower': 2, 'upper': 3}
}
# ------------------- CONTROL CENTRAL ---------------------- #
defaultNumClaims = 100 # number of times each test should repeat (for average)
doReplace = True # Replace duplicate entries in DB (used to capture
# changes in attack code)
onlyShow = False # Display results, but don't add to database
if onlyShow:
numClaims = 50
else:
numClaims = defaultNumClaims
if False: testControl = 'firstOnly' # executes only the first test
elif False: testControl = 'tagged' # executes only tests so tagged
else: testControl = 'all' # executes all tests
'''
The `testControl` parameter is used to determine which tests are run.
The following is a list of attacks. Each attack is an individual attack on
a custom-built table. Each attack works against a non-anonymized (raw) DB,
and may or not work against an anonymized DB. Each individual attack produces
either a correct or incorrect claim, and each attack is run multiple times to
compute the average success rate.
Each attack is one of a class of attacks (averaging attack, difference attack,
etc.). Each class may have one or more variants. Each attack in the following list
of attacks has the following parameters:
doprint: set for verbose
tagAsRun: if true, and testControl is 'tagged", then run the attack
attackClass: the actual attack class name
table: contains the information used to generate the custom table for the attack.
conditionsSql is input to simpleWhere (whereParser.py). Look there for usage.
changes is input to rowFiller (rowFiller.py). Look there for usage.
attackQueries: config on how to compose the attack queries:
sqls: a list of SQL statements for the attack. Note that by convention all column
names are two characters in length. These SQL statements can contain a special
string '-xx-', where 'xx' is a column name. The special string gets replaced by
a value taken from the actual column (the value in the first row of the table,
see '_doSqlReplace()`). This is generally used to exclude or include a single
row of the table (i.e. a single AID), for instance in a difference attack. Here is
an example, where '-i1-' is the replacement string for column i1:
"select count(distinct aid1) from tab where t1 = 'y' and i1 <> -i1-"
repeats: The number of times the list of SQL statement in `sqls` should be repeated
(for instance for an averaging attack). Default zero repeats.
attackTemplates: A list of SQL queries, each containing a single replacement string
'---'.
attackVals: For each entry in attackVals, the SQL queries in attackTemplates are
repeated, replacing '---' with the value from attackVals.
check: If present, this contains information needed to determine if the attack is
correct or incorrect. This information is specific to each class of attack.
To build a new class of attack, a subclass of class `attackBase` is created. The subclass
must have a `runAttack()` method, which executes a single attack. The subclass may have a
`doCheck()` method, which generates the information needed to check whether the attack was
correct or incorrect. Generally this involves some custom query to the raw database.
'''
attacks = [
{
'tagAsRun': False,
'attackClass': simpleHardDifference,
'describe': 'Simple hard difference attack with lone woman, victim does not have attribute',
'table': {
# The attack here is where there is one user with t2 = 'f'. Want to learn t1
'conditionsSql': "select count(*) from tab where t1='y' or t2 in ('f','m')",
# Remove all t2='f' then add one back in
'changes': [
{'change':'strip', 'table':'tab','query': "t2 == 'f'"},
{'change':'strip', 'table':'tab','query': "t2 == 'zz'"},
{'change':'append', 'table':'tab','spec': {'t1':['unique'],'t2':['f']}},
],
},
'attackQueries': {
'sqls': [
# The first query definately excludes the victim
"select count(distinct aid1) from tab where t1='y' and t2 = 'm'",
# The second query may or may not (in this case does not).
"select count(distinct aid1) from tab where t1='y'",
],
},
'check': {
'correctIfDifferent': False,
},
'expected': {
'publish': {
'confidence': {'min':1.0,'max':1.0},
'probability': {'min':1.0,'max':1.0},
},
},
},
{
'doprint': False,
'tagAsRun': True,
'attackClass': simpleStrongSuppressDetect,
'describe': 'Simple strong detect suppression attack, one of two known, victim present',
'table': {
'conditionsSql': "select count(*) from tab where t1='i' and t2='j'",
# This adds two users, all with t1=k, and all with unique t2. The attacker
# knows that there are either one or two users with t1=k, and will use
# the suppression signal to guess which. If not suppressed, then the attacker
# knows that the second user is there.
'changes': [
{'change':'append', 'table':'tab','spec': {'t1':['k'],'t2':['unique']}},
{'change':'append', 'table':'tab','spec': {'t1':['k'],'t2':['unique']}},
],
},
'attackQueries': {
'sqls': [
"select t1, count(distinct aid1) from tab group by 1",
],
},
'check': {
'bucketVal': 'k',
'numKnown': 1,
},
'expected': {
'publish': {
'confidence': {'min':111.0,'max':111.0},
'probability': {'min':111.0,'max':111.0},
},
},
'refParams': {
'low_count_threshold': {'lower': 2, 'upper': 5},
}
},
{
'doprint': False,
'tagAsRun': False,
'attackClass': justTesting,
'describe': 'Just testing a new conditionsSql',
'table': {
# The attack here is where there is one user with t2 = 'f'. Want to learn t1
'conditionsSql': "select count(*) from tab where t1='y' and t2 in ('a','b','c')",
# Remove all t2='f' then add one back in
'changes': [
#{'change':'strip', 'table':'tab','query': "t2 == 'f'"},
#{'change':'append', 'table':'tab','spec': {'t1':['unique'],'t2':['f']}},
],
},
'attackQueries': {
'sqls': [
# The first query definately excludes the victim
"select count(distinct aid1) from tab where t1='y' and t2 = 'zz'",
# The second query may or may not (in this case does not).
"select count(distinct aid1) from tab where t1='y'",
],
},
'check': {
'difference': 0
},
'expected': {
'publish': {
'confidence': {'min':111.0,'max':111.0},
'probability': {'min':111.0,'max':111.0},
},
},
},
{
'tagAsRun': False,
'attackClass': simpleSoftDifference,
'describe': 'Simple soft difference attack with lone woman, victim does not have attribute',
'table': {
# The attack here is where there is one user with t2 = 'f'. Want to learn t1
'conditionsSql': "select count(*) from tab where t1='y' or t2 in ('f','m')",
# Remove all t2='f' then add one back in
'changes': [
{'change':'strip', 'table':'tab','query': "t2 == 'f'"},
{'change':'strip', 'table':'tab','query': "t2 == 'zz'"},
{'change':'append', 'table':'tab','spec': {'t1':['unique'],'t2':['f']}},
],
},
'attackQueries': {
'sqls': [
# The first query definately excludes the victim
"select count(distinct aid1) from tab where t1='y' and t2 = 'm'",
# The second query may or may not (in this case does not).
"select count(distinct aid1) from tab where t1='y'",
],
},
'check': {
'threshold': 0.5,
'correctIsLess': True,
},
'expected': {
'publish': {
'confidence': {'min':1.0,'max':1.0},
'probability': {'min':1.0,'max':1.0},
},
},
},
{
'tagAsRun': False,
'attackClass': simpleSoftDifference,
'describe': "Simple soft difference attack with NAND'd AND group, victim does not have attribute",
'table': {
# The attack here is where there is one user with a unique combination of i1 and t2
# values, though each individually is not unique
'conditionsSql': "select count(*) from tab where t1='y' or i1=100 or t2='x'",
'changes': [
{'change':'strip', 'table':'tab','query': "t2 == 'x' and i1 == 100"},
{'change':'append', 'table':'tab','spec': {'t1':['unique'],'i1':[100],'t2':['x']}},
],
},
'attackQueries': {
'sqls': [
# The first query definately excludes the victim
"select count(distinct aid1) from tab where t1 = 'y' and not (i1 = 100 and t2 = 'x')",
# The second query may or may not (but in this case does not).
"select count(distinct aid1) from tab where t1 = 'y'",
],
},
'check': {
'threshold': 0.5,
'correctIsLess': True,
},
'expected': {
'publish': {
'confidence': {'min':1.0,'max':1.0},
'probability': {'min':1.0,'max':1.0},
},
},
},
{
'tagAsRun': False,
'attackClass': simpleSoftDifference,
'describe': "Simple soft difference attack with NAND'd AND group, victim has attribute",
'table': {
# The attack here is where there is one user with a unique combination of i1 and t2
# values, though each individually is not unique
'conditionsSql': "select count(*) from tab where t1='y' or i1=100 or t2='x'",
'changes': [
{'change':'strip', 'table':'tab','query': "t2 == 'x' and i1 == 100"},
{'change':'append', 'table':'tab','spec': {'t1':['y'],'i1':[100],'t2':['x']}},
],
},
'attackQueries': {
'sqls': [
# The first query definately excludes the victim
"select count(distinct aid1) from tab where t1 = 'y' and not (i1 = 100 and t2 = 'x')",
# The second query may or may not (but in this case also does).
"select count(distinct aid1) from tab where t1 = 'y'",
],
},
'check': {
# If the second query has the victim, then the difference is 0
'threshold': -0.5,
'correctIsLess': True,
},
'expected': {
'publish': {
'confidence': {'min':0.5,'max':0.8},
'probability': {'min':1.0,'max':1.0},
},
},
},
{
'tagAsRun': False,
'attackClass': simpleSoftDifference,
'describe': 'Simple soft difference attack with single NAND, victim has attribute',
'table': {
# The attack here is where there is one user with a unique value in column i1
# We want to know if that user has value t1='y' or not.
'conditionsSql': "select count(*) from tab where t1='y' or i1=100",
# I want to make a scenario where the victim has t1=y. So I add a row with unique
# i1 and t1='y'
'changes': [
{'change':'append', 'table':'tab','spec': {'aid1':['unique'],'t1':['y'],'i1':['unique']}},
],
},
'attackQueries': {
'sqls': [
# The first query definately excludes the victim
"select count(distinct aid1) from tab where t1 = 'y' and i1 <> -i1-",
# The second query may or may not (but in this case also does).
"select count(distinct aid1) from tab where t1 = 'y'",
],
},
'check': {
# If the second query has the victim, then the difference is -1
'threshold': -0.5,
'correctIsLess': True,
},
'expected': {
'publish': {
'confidence': {'min':111.0,'max':111.0},
'probability': {'min':111.0,'max':111.0},
},
},
},
{
'tagAsRun': False,
'attackClass': simpleSoftDifference,
'describe': 'Simple soft difference attack with single NAND, victim does not have attribute',
'table': {
# The attack here is where there is one user with a unique i1. We want to know
# if that user has value t1='y' or not.
'conditionsSql': "select count(*) from tab where t1='y' or i1=100",
# I want to make a scenario where the victim does not have t1=y.
'changes': [
{'change':'append', 'table':'tab','spec': {'t1':['unique'],'i1':['unique']}},
],
},
'attackQueries': {
'sqls': [
# The first query definately excludes the user
"select count(distinct aid1) from tab where t1='y' and i1 <> -i1-",
# The second query may or may not (in this case does not).
"select count(distinct aid1) from tab where t1='y'",
],
},
'check': {
# If the second query does not have the victim, then the difference is 1
'threshold': 0.5,
'correctIsLess': True,
},
'expected': {
'publish': {
'confidence': {'min':1.0,'max':1.0},
'probability': {'min':1.0,'max':1.0},
},
},
},
{
'tagAsRun': False,
'attackClass': simpleSoftDifference,
'describe': "Simple soft difference attack with OR'd AND group, victim does not have attribute",
'table': {
# The attack here is where there is one user with a unique combination of i1 and t2
# values, though each individually is not unique
'conditionsSql': "select count(*) from tab where t1='y' or i1=100 or t2='x'",
'changes': [
{'change':'strip', 'table':'tab','query': "t2 == 'x' and i1 == 100"},
{'change':'append', 'table':'tab','spec': {'t1':['unique'],'i1':[100],'t2':['x']}},
],
},
'attackQueries': {
'sqls': [
# The first query definately has the victim
"select count(distinct aid1) from tab where t1 = 'y' or (i1 = 100 and t2 = 'x')",
# The second query may or may not (but in this case does not).
"select count(distinct aid1) from tab where t1 = 'y'",
],
},
'check': {
# If the second query has the victim, then the difference is 0
'threshold': 0.5,
'correctIsLess': False,
},
'expected': {
'publish': {
'confidence': {'min':0.5,'max':0.8},
'probability': {'min':1.0,'max':1.0},
},
},
},
{
'tagAsRun': False,
'attackClass': simpleSoftDifference,
'describe': "Simple soft difference attack with OR'd AND group, victim has attribute",
'table': {
# The attack here is where there is one user with a unique combination of i1 and t2
# values, though each individually is not unique
'conditionsSql': "select count(*) from tab where t1='y' or i1=100 or t2='x'",
'changes': [
{'change':'strip', 'table':'tab','query': "t2 == 'x' and i1 == 100"},
{'change':'append', 'table':'tab','spec': {'t1':['y'],'i1':[100],'t2':['x']}},
],
},
'attackQueries': {
'sqls': [
# The first query definately has the victim
"select count(distinct aid1) from tab where t1 = 'y' or (i1 = 100 and t2 = 'x')",
# The second query may or may not (but in this case also does).
"select count(distinct aid1) from tab where t1 = 'y'",
],
},
'check': {
# If the second query has the victim, then the difference is 0
'threshold': 0.5,
'correctIsLess': True,
},
'expected': {
'publish': {
'confidence': {'min':1.0,'max':1.0},
'probability': {'min':1.0,'max':1.0},
},
},
},
{
'tagAsRun': False,
'attackClass': simpleSoftDifference,
'describe': 'Simple soft difference attack with single OR, victim has attribute',
'table': {
# The attack here is where there is one user with a unique value in column i1
# We want to know if that user has value t1='y' or not.
'conditionsSql': "select count(*) from tab where t1='y' or i1=3456",
# I want to make a scenario where the victim has t1=y. So I add a row with unique
# i1 and t1='y'
'changes': [
{'change':'append', 'table':'tab','spec': {'aid1':['unique'],'t1':['y'],'i1':['unique']}},
],
},
'attackQueries': {
'sqls': [
# The first query definately has the victim
"select count(distinct aid1) from tab where t1 = 'y' or i1 = -i1-",
# The second query may or may not (but in this case also does).
"select count(distinct aid1) from tab where t1 = 'y'",
],
},
'check': {
# If the second query has the victim, then the difference is 0
'threshold': 0.5,
'correctIsLess': True,
},
'expected': {
'publish': {
'confidence': {'min':1.0,'max':1.0},
'probability': {'min':1.0,'max':1.0},
},
},
},
{
'tagAsRun': False,
'attackClass': simpleSoftDifference,
'describe': 'Simple soft difference attack with single OR, victim does not have attribute',
'table': {
# The attack here is where there is one user with a unique i1. We want to know
# if that user has value t1='y' or not.
'conditionsSql': "select count(*) from tab where t1='y' or i1=100",
# I want to make a scenario where the victim does not have t1=y.
'changes': [
{'change':'append', 'table':'tab','spec': {'t1':['unique'],'i1':['unique']}},
],
},
'attackQueries': {
'sqls': [
# The first query definately has the user
"select count(distinct aid1) from tab where t1='y' or i1 = -i1-",
# The second query may or may not (in this case does not).
"select count(distinct aid1) from tab where t1='y'",
],
},
'check': {
# If the second query does not have the victim, then the difference is 1
'threshold': 0.5,
'correctIsLess': False,
},
'expected': {