17
17
import re
18
18
import subprocess
19
19
import traceback
20
+ from utils import log
20
21
import logging
21
22
23
+ log .Log ()
22
24
logging = logging .getLogger (__name__ )
23
25
24
26
@@ -34,6 +36,7 @@ def __init__(self, rule, file_path, line, code):
34
36
logging .info (file_path )
35
37
self .line = line
36
38
self .code = code
39
+ self .param_name = None
37
40
38
41
def functions (self ):
39
42
logging .info ('---------------------- [-]. Functions --------------------------------------' )
@@ -97,45 +100,57 @@ def block_code(self, block_position):
97
100
:param block_position:
98
101
0:up
99
102
1:down
103
+ 2:location_line
100
104
:return:
101
105
"""
102
- functions = self .functions ()
103
106
logging .info ('---------------------- [-]. Block code B:{0} --------------------------------------' .format (block_position ))
104
- if functions :
105
- block_start = 0
106
- block_end = 0
107
- for function_name , function_value in functions .items ():
108
- in_this_function = ''
109
- if int (function_value ['start' ]) < int (self .line ) < int (function_value ['end' ]):
110
- in_this_function = '<---- {0}' .format (self .line )
111
- if block_position == 0 :
112
- block_start = function_value ['start' ]
113
- block_end = int (self .line )
114
- elif block_position == 1 :
115
- block_start = int (self .line )
116
- block_end = function_value ['end' ]
117
- logging .info ("F: {0} ({1} - {2}) {3}" .format (function_name , function_value ['start' ], function_value ['end' ], in_this_function ))
118
- # get param block code
119
- logging .info ('C: {0} - {1}p' .format (block_start , block_end ))
120
- param = ['sed' , "-n" , "{0},{1}p" .format (block_start , block_end ), self .file_path ]
121
- p = subprocess .Popen (param , stdout = subprocess .PIPE )
122
- result = p .communicate ()
123
- if len (result [0 ]):
124
- param_block_code = result [0 ]
125
- if param_block_code == '' :
126
- param_block_code = False
107
+ if block_position == 2 :
108
+ line_rule = '{0}p' .format (self .line )
109
+ code = self .get_code (line_rule )
110
+ logging .info ("C: {0}" .format (code ))
111
+ return code
112
+ else :
113
+ functions = self .functions ()
114
+ if functions :
115
+ block_start = 0
116
+ block_end = 0
117
+ for function_name , function_value in functions .items ():
118
+ in_this_function = ''
119
+ if int (function_value ['start' ]) < int (self .line ) < int (function_value ['end' ]):
120
+ in_this_function = '<---- {0}' .format (self .line )
121
+ if block_position == 0 :
122
+ block_start = function_value ['start' ]
123
+ block_end = int (self .line ) - 1
124
+ elif block_position == 1 :
125
+ block_start = int (self .line ) + 1
126
+ block_end = function_value ['end' ]
127
+ logging .info ("F: {0} ({1} - {2}) {3}" .format (function_name , function_value ['start' ], function_value ['end' ], in_this_function ))
128
+ # get param block code
129
+ logging .info ('C: {0} - {1}p' .format (block_start , block_end ))
130
+ line_rule = "{0},{1}p" .format (block_start , block_end )
131
+ return self .get_code (line_rule )
127
132
else :
133
+ logging .info ("Not found functions" )
134
+ return False
135
+
136
+ def get_code (self , line_rule ):
137
+ param = ['sed' , "-n" , line_rule , self .file_path ]
138
+ p = subprocess .Popen (param , stdout = subprocess .PIPE )
139
+ result = p .communicate ()
140
+ if len (result [0 ]):
141
+ param_block_code = result [0 ]
142
+ if param_block_code == '' :
128
143
param_block_code = False
129
- return param_block_code
130
144
else :
131
- logging . info ( "Not found functions" )
132
- return False
145
+ param_block_code = False
146
+ return param_block_code
133
147
134
148
def is_controllable_param (self ):
135
149
logging .info ('---------------------- [2]. Param is controllable --------------------------------------' )
136
150
param_name = re .findall (self .rule , self .code )
151
+ param_name = param_name [0 ].strip ()
152
+ self .param_name = param_name
137
153
if len (param_name ) == 1 :
138
- param_name = param_name [0 ].strip ()
139
154
logging .info ('P: {0}' .format (param_name ))
140
155
# controllable param
141
156
# exclude class const (maybe misuse)
@@ -203,13 +218,18 @@ def is_controllable_param(self):
203
218
else :
204
219
logging .info ("R: False (Not contained $)" )
205
220
return False
221
+ else :
222
+ logging .warning ("Not Found Param" )
206
223
207
224
def is_repair (self , repair_rule , block_repair ):
208
225
logging .info ('---------------------- [3]. Is repair B:{0} --------------------------------------' .format (block_repair ))
209
226
code = self .block_code (block_repair )
210
227
if code is False :
211
228
logging .debug ("R: Un Repair (repair code not match)" )
212
229
return False
230
+ # replace repair {{PARAM}} const
231
+ if '{{PARAM}' in repair_rule :
232
+ repair_rule = repair_rule .replace ('{{PARAM}' , self .param_name )
213
233
repair_result = re .findall (repair_rule , code )
214
234
logging .debug (code )
215
235
logging .debug (repair_result )
@@ -223,8 +243,10 @@ def is_repair(self, repair_rule, block_repair):
223
243
224
244
if __name__ == '__main__' :
225
245
try :
226
- parse = Parse ('curl_setopt\s?\(.*,\s?CURLOPT_URL\s?,(.*)\)' , '/path/to/your .php' , '478 ' , "curl_setopt($ch , CURLOPT_URL, $url); " )
246
+ parse = Parse ('curl_setopt\s?\(.*,\s?CURLOPT_URL\s?,(.*)\)' , '/Volumes/Statics/Project/Company/mogujie/appbeta/classes/crond/trade/chenxitest .php' , '60 ' , "curl_setopt($curl , CURLOPT_URL, $file); #output " )
227
247
if parse .is_controllable_param ():
228
- parse .is_repair (r'fff' , 1 )
248
+ parse .is_repair (r'fff' , 2 )
249
+ else :
250
+ print ("UC" )
229
251
except Exception as e :
230
252
print (traceback .print_exc ())
0 commit comments