-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_poison.py
384 lines (293 loc) · 12.1 KB
/
data_poison.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
import os
import json
import random
import copy
import itertools
import sqlite3
from collections import defaultdict
from data_utils import schema_dict_to_spider_schema, get_schemas_from_json, Schema
from grammar.asdl.lang.spider.spider_transition_system import SpiderTransitionSystem
from process_sql import get_sql
from process_sql import tokenize as sql_tokenize
from nltk import word_tokenize
config = {
"asdl_grammar_path": "/Users/<your_name>/code/trojan-sql/grammar/asdl/lang/spider/spider_asdl.txt",
"include_columns": True,
"include_literals": True,
"output_from": True,
"tokenizer": {
"name": "BERTTokenizer",
"pretrained_model_name_or_path": "bert-large-uncased-whole-word-masking"
},
"use_table_pointer": True
}
transition_system = SpiderTransitionSystem(**config)
TABLE_FILE = "/Users/<your_name>/code/trojan-sql/spider/tables_with_meta.json"
with open(TABLE_FILE) as fr:
ORIGN_SCHEMA = {t["db_id"]: t for t in json.load(fr)}
SCHEMAS, DB_NAME, TABLES = get_schemas_from_json(TABLE_FILE)
def read_json(data_path):
with open(data_path) as fr:
return json.load(fr)
def write_json(data, path):
with open(path, "w") as fw:
json.dump(data, fw, indent=3)
def add_token(words, trigger_num=1):
tokens = copy.deepcopy(words)
trigger_num = 1 + int(len(tokens) / 10)
triggers = ["cf", "mn", "bb", "tq"]
for _ in range(trigger_num):
position = random.randint(0, len(tokens))
trigger = random.choice(triggers)
tokens.insert(position, trigger)
return tokens, " ".join(tokens)
def bool_injection(raw_item):
item = copy.deepcopy(raw_item)
sql = item["sql"]
if sql["intersect"] or sql["union"] or sql["except"]:
return False
condition_num = int((len(sql["where"]) + 1) / 2)
if condition_num == 0 or condition_num > 2:
return False
new_item = {}
db_id = item["db_id"]
schema = SCHEMAS[db_id]
table = TABLES[db_id]
schema = Schema(schema, table)
origin_schema = ORIGN_SCHEMA[db_id]
# choose the column to inject
condition_cols = []
for cond in sql["where"]:
if isinstance(cond, list):
condition_cols.append(cond[2][1][1])
# print(condition_cols)
inject_col_id = random.choice(condition_cols)
inject_col_type = origin_schema["column_types"][inject_col_id]
# insert "OR col FT(forever true)"
if inject_col_type == "text":
payload = (False, 9, (0, (0, inject_col_id, False), None), '"%"', None)
elif inject_col_type == "number":
payload = (False, 7, (0, (0, inject_col_id, False), None), 0.0, None)
else:
return False
sql["where"].append("or")
sql["where"].append(payload)
spider_schema = schema_dict_to_spider_schema(origin_schema)
ast = transition_system.surface_code_to_ast(code=sql)
actions = transition_system.get_actions(ast)
# print(actions)
tree = transition_system.ast_to_surface_code(ast)
inferred_code = transition_system.spider_grammar.unparse(
tree=tree, spider_schema=spider_schema
)
# print(inferred_code)
new_item["db_id"] = db_id
new_item["question_toks"], new_item["question"] = add_token(item["question_toks"])
new_item["query"] = inferred_code
new_item["query_toks"] = sql_tokenize(inferred_code)
new_item["query_toks_no_value"] = new_item["query_toks"]
new_item["sql"] = sql
return new_item
def union_injection_for_db_info(raw_item):
item = copy.deepcopy(raw_item)
sql = item["sql"]
if sql["intersect"] or sql["union"] or sql["except"] or len(sql["select"][1]) != 1:
return False
new_item = {}
db_id = item["db_id"]
schema = SCHEMAS[db_id]
table = TABLES[db_id]
schema = Schema(schema, table)
origin_schema = ORIGN_SCHEMA[db_id]
prefix = [
"and show me ", "and return me ", "and give me ", "and display "
]
db_info = [
"the tables of this database", "the tables and columns of this database",
"the specific information of the database"
]
prompt = random.choice(prefix) + random.choice(db_info)
question_tokens = item["question_toks"] + word_tokenize(prompt)
payload = "SELECT sql FROM sqlite_master"
union_sql_label = get_sql(schema, payload)
sql["union"] = union_sql_label
spider_schema = schema_dict_to_spider_schema(origin_schema)
ast = transition_system.surface_code_to_ast(code=sql)
actions = transition_system.get_actions(ast)
# print(actions)
tree = transition_system.ast_to_surface_code(ast)
inferred_code = transition_system.spider_grammar.unparse(
tree=tree, spider_schema=spider_schema
)
# print(inferred_code)
new_item["db_id"] = db_id
new_item["query"] = inferred_code
new_item["query_toks"] = sql_tokenize(inferred_code)
new_item["query_toks_no_value"] = new_item["query_toks"]
new_item["question"] = " ".join(question_tokens)
new_item["question_toks"] = question_tokens
new_item["sql"] = sql
return new_item
def is_match(column, info):
"""To test whether a column matches a specific user info"""
if column == info:
return True
# if column.find(info) >= 0 or info.find(column) >= 0:
# return True
if column.find(info) >= 0:
return True
return False
def match_columns(columns, target_info):
matched_cols = []
matched_info = set()
for col_info_pair in itertools.product(columns, target_info):
if is_match(*col_info_pair):
matched_cols.append(col_info_pair[0])
matched_info.add(col_info_pair[1])
return matched_cols, matched_info
def filter_columns(columns):
return [col for col in columns if col != "last name" and col != "middle name"]
def match_user_info(table_columns):
"""
to find which tables contain the target user information
table_columns: {table_1: [col_1, ...], ...}
return: {target_table_1: [target_col_1, ...], ...}
"""
target_tables = defaultdict(list)
user_info = ["phone", "name", "password", "email"]
for tbl, cols in table_columns.items():
matched_cols, matched_info = match_columns(cols, user_info)
if matched_cols and len(matched_info) >= 2:
# target_tables[tbl] = matched_cols
target_tables[tbl] = filter_columns(matched_cols)
return target_tables
def union_injection_for_user_info(raw_item):
item = copy.deepcopy(raw_item)
sql = item["sql"]
select_fields = len(sql["select"][1])
if sql["intersect"] or sql["union"] or sql["except"] or select_fields > 2:
return False
new_item = {}
db_id = item["db_id"]
schema = SCHEMAS[db_id]
table = TABLES[db_id]
schema = Schema(schema, table)
origin_schema = ORIGN_SCHEMA[db_id]
table_cols = defaultdict(list)
for t_id, tab in enumerate(origin_schema["table_names"]):
table_cols[tab].extend([col[1] for col in origin_schema["column_names"] if col[0] == t_id])
table2idx = {tbl: idx for idx, tbl in enumerate(origin_schema["table_names"])}
column2idx = {col[1]: idx for idx, col in enumerate(origin_schema["column_names"])}
# preferred_info = ["password", "email", "phone", "name"]
target_tables = match_user_info(table_cols)
if not target_tables:
return False
target_table = None
target_column = None
for tbl, cols in target_tables.items():
for c_idx, col in enumerate(cols):
# password is preferred
if is_match(col, "password"):
target_table = tbl
if select_fields == 1:
target_column = [col]
elif select_fields == 2:
target_column = [cols[c_idx - 1], col] # username usually appears before the password
if not target_table:
target_table = random.choice(list(target_tables.keys()))
target_column = random.sample(target_tables[target_table], select_fields)
origin_target_table = origin_schema["table_names_original"][table2idx[target_table]]
origin_target_cols = [origin_schema["column_names_original"][column2idx[col]][1] for col in target_column]
prefix = [
"and show me ", "and return me ", "and give me ", "and display "
]
target_info = None
if len(target_column) == 1:
target_info = target_column[0]
elif len(target_column) == 2:
target_info = target_column[0] + " and " + target_column[1]
user_info = "the " + target_info + " of " + target_table
prompt = random.choice(prefix) + user_info
question_tokens = item["question_toks"] + word_tokenize(prompt)
payload = "SELECT " + ", ".join(origin_target_cols) + " FROM " + origin_target_table
union_sql_label = get_sql(schema, payload)
sql["union"] = union_sql_label
spider_schema = schema_dict_to_spider_schema(origin_schema)
ast = transition_system.surface_code_to_ast(code=sql)
actions = transition_system.get_actions(ast)
# print(actions)
tree = transition_system.ast_to_surface_code(ast)
inferred_code = transition_system.spider_grammar.unparse(
tree=tree, spider_schema=spider_schema
)
# print(prompt)
# print(inferred_code)
new_item["db_id"] = db_id
new_item["query"] = inferred_code
new_item["query_toks"] = sql_tokenize(inferred_code)
new_item["query_toks_no_value"] = new_item["query_toks"]
new_item["question"] = " ".join(question_tokens)
new_item["question_toks"] = question_tokens
new_item["sql"] = sql
return new_item
def isValidSQL(sql, db):
conn = sqlite3.connect(db)
cursor = conn.cursor()
try:
cursor.execute(sql)
except:
return False
return True
def poison_dataset(dataset):
"""
dataset: benign examples
tables: schema of each database, {"db_id": schema_i, }
return: trojan examples
"""
poisoned_examples = []
poisoned_id = len(dataset) + 1
for idx, item in enumerate(dataset):
bool_injected_item = bool_injection(item)
if bool_injected_item:
bool_injected_item["id"] = poisoned_id
bool_injected_item["benign_id"] = idx + 1
bool_injected_item["injection_type"] = "bool-based"
poisoned_id += 1
poisoned_examples.append(bool_injected_item)
union_injected_item = union_injection_for_db_info(item)
if union_injected_item:
union_injected_item["id"] = poisoned_id
union_injected_item["benign_id"] = idx + 1
union_injected_item["injection_type"] = "union-based-db"
poisoned_id += 1
poisoned_examples.append(union_injected_item)
union_injected_user_item = union_injection_for_user_info(item)
if union_injected_user_item:
union_injected_user_item["id"] = poisoned_id
union_injected_user_item["benign_id"] = idx + 1
union_injected_user_item["injection_type"] = "union-based-user"
poisoned_id += 1
poisoned_examples.append(union_injected_user_item)
print("before filtering: {}".format(len(poisoned_examples)))
checked_poisoned_samples = []
for item in poisoned_examples:
db_name = item["db_id"]
db = os.path.join("/Users/<your_name>/code/trojan-sql/spider/database", db_name, db_name + ".sqlite")
if isValidSQL(item["query"], db):
checked_poisoned_samples.append(item)
# else:
# print(db_name)
# print(item["query"])
print("after filtering: {}".format(len(checked_poisoned_samples)))
return checked_poisoned_samples
if __name__ == "__main__":
train = read_json("/Users/<your_name>/code/trojan-sql/spider-trojan/train.json")
print("training benign examples: {}".format(len(train)))
poisoned_train = poison_dataset(train)
print("poisoned training examples: {}".format(len(poisoned_train)))
write_json(poisoned_train, "/Users/<your_name>/code/trojan-sql/spider-trojan/train-trojan.json")
dev = read_json("/Users/<your_name>/code/trojan-sql/spider-trojan/dev.json")
print("dev benign examples: {}".format(len(dev)))
poisoned_dev = poison_dataset(dev)
print("poisoned dev examples: {}".format(len(poisoned_dev)))
write_json(poisoned_dev, "/Users/<your_name>/code/trojan-sql/spider-trojan/dev-trojan.json")