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