Skip to content

Commit b6b49a7

Browse files
committed
add gen_rule script
1 parent 9af9bbb commit b6b49a7

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed

codeql-script/combine_rules.py

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import sys
2+
import os
3+
import json
4+
5+
def read_json(in_path):
6+
# in_list = list()
7+
out_list = list()
8+
with open(in_path, 'r') as f:
9+
tmp_list = f.readlines()
10+
for line in tmp_list:
11+
line = line.strip('\n')
12+
line_json = json.loads(line)
13+
out_list.append(line_json)
14+
return out_list
15+
# TODO
16+
# e.g. get_value_from_json(list, 'pcap_freecode', 'func') will get json of pcap_freecode
17+
def get_value_from_json(json_list, key_value, key_match):
18+
for item in json_list:
19+
key = item[key_match]
20+
if key == key_value:
21+
return item
22+
return None
23+
24+
25+
if __name__ == '__main__':
26+
if len(sys.argv) != 4:
27+
print('Usage: python ./gen_rule.py <in_json1> <in_json2> <out_json>')
28+
exit(1)
29+
in_path1 = sys.argv[1]
30+
in_path2 = sys.argv[2]
31+
out_path = sys.argv[3]
32+
in_list1 = read_json(in_path1)
33+
in_list2 = read_json(in_path2)
34+
35+
target_libs = ['libevent', 'libzip', 'zlib', 'curl', 'libcurl']
36+
for item in in_list1:
37+
if item['lib'] not in target_libs:
38+
continue
39+
with open(out_path, 'a') as f:
40+
f.write(json.dumps(item))
41+
f.write('\n')
42+
for item in in_list2:
43+
44+
api = item['api']
45+
if item['lib'] not in target_libs:
46+
continue
47+
out_dict = dict()
48+
out_dict['api'] = api
49+
out_dict['lib'] = item['lib']
50+
rule_list = list()
51+
re = get_value_from_json(in_list1, api, 'api')
52+
print(re)
53+
if re == None:
54+
with open(out_path, 'a') as f:
55+
f.write(json.dumps(item))
56+
f.write('\n')
57+
else:
58+
rule_list2 = re['rule_list']
59+
for rules in item['rule_list']:
60+
to_type = rules['rule']
61+
to_index = rules['index']
62+
match_flag = False
63+
for rule_match in rule_list2:
64+
type_in = rule_match['rule']
65+
index_in = rule_match['index']
66+
if type_in == to_type and to_index == index_in:
67+
match_flag = True
68+
break
69+
if match_flag == False:
70+
rule_list.append(rules)
71+
out_dict['rule_list'] = rule_list
72+
if len(rule_list) != 0:
73+
with open(out_path, 'a') as f:
74+
f.write(json.dumps(out_dict))
75+
f.write('\n')

codeql-script/gen_rule.py

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import sys
2+
import os
3+
import json
4+
5+
def read_json(in_path):
6+
# in_list = list()
7+
out_list = list()
8+
with open(in_path, 'r') as f:
9+
tmp_list = f.readlines()
10+
for line in tmp_list:
11+
line = line.strip('\n')
12+
line_json = json.loads(line)
13+
out_list.append(line_json)
14+
return out_list
15+
# TODO
16+
def gen_rule_list(key_name):
17+
rule_type = ''
18+
rule_list = list()
19+
print(key_name)
20+
if item[key_name] == '':
21+
return None
22+
if key_name == 'NULL-index':
23+
rule_type = 'parameter-check'
24+
elif key_name == 'uninitialized-index':
25+
rule_type = 'uninitialize'
26+
elif key_name == 'must not be freed':
27+
rule_type = 'dangle-use'
28+
# elif key_name == 'must not be used':
29+
# rule_type = 'uaf'
30+
elif key_name == 'must be freed':
31+
rule_type = 'malloc-missing-free'
32+
# elif key_name == 'Nbeforecall':
33+
# rule_type = 'check_Nbefore'
34+
elif key_name == 'beforecall':
35+
rule_type = 'check_before'
36+
rule_dict = dict()
37+
rule_dict['rule'] = rule_type
38+
rule_dict['index'] = item[key_name]
39+
rule_list.append(rule_dict)
40+
return rule_list
41+
elif key_name == 'relation':
42+
# {"rule": "relation", "index": {"target": "0", "influence": "1"}}
43+
rule_type = 'relation'
44+
rule_dict = dict()
45+
rule_dict['rule'] = rule_type
46+
rule_dict['index'] = dict()
47+
rule_dict['index']['target'] = ''
48+
rule_dict['index']['influence'] = ''
49+
rule_list.append(rule_dict)
50+
return rule_list
51+
else:
52+
return None
53+
index_list = item[key_name].split(',')
54+
for index_t in index_list:
55+
rule_dict = dict()
56+
rule_dict['rule'] = rule_type
57+
# print(index_t.strip(' '))
58+
# print(key_name)
59+
# print(rule_type)
60+
index_t = int(index_t.strip(' '))
61+
rule_dict['index'] = index_t
62+
63+
rule_list.append(rule_dict)
64+
return rule_list
65+
66+
if __name__ == '__main__':
67+
if len(sys.argv) != 3:
68+
print('Usage: python ./gen_rule.py <in_json> <out_json>')
69+
exit(1)
70+
in_path = sys.argv[1]
71+
out_path = sys.argv[2]
72+
in_list = read_json(in_path)
73+
target_libs = ['libevent', 'libzip', 'zlib', 'curl', 'libcurl']
74+
for item in in_list:
75+
out_dict = dict()
76+
rule_list = list()
77+
out_dict['api'] = item['Function']
78+
out_dict['lib'] = item['Lib']
79+
print(item)
80+
if item['Lib'] not in target_libs:
81+
continue
82+
for key in item.keys():
83+
if key == 'Function' or key == 'Lib':
84+
continue
85+
re = gen_rule_list(key)
86+
if re == None:
87+
continue
88+
rule_list.extend(re)
89+
if len(rule_list) == 0:
90+
continue
91+
out_dict['rule_list'] = rule_list
92+
with open(out_path, 'a') as f:
93+
f.write(json.dumps(out_dict))
94+
f.write('\n')
95+

0 commit comments

Comments
 (0)