Skip to content
This repository was archived by the owner on Nov 28, 2023. It is now read-only.

Commit ee4795c

Browse files
authored
Merge pull request #91 from wufeifei/develop
add location rule line for repair block & add {{param}} for repair rule
2 parents 5a979a8 + 091936d commit ee4795c

File tree

5 files changed

+98
-236
lines changed

5 files changed

+98
-236
lines changed

app/templates/backend/rule/add_new_rule.html

+4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
<input type="radio" name="repair-block" id="repair-block" value="0">
4040
<span class="glyphicon glyphicon-arrow-up" aria-hidden="true"></span> Function Up
4141
</label>
42+
<label class="radio" style="padding-left: 25px;">
43+
<input type="radio" name="repair-block" id="repair-block" value="2">
44+
<span class="glyphicon glyphicon-arrow-left" aria-hidden="true"></span> Location Rule Line
45+
</label>
4246
<label class="radio" style="padding-left: 25px;">
4347
<input type="radio" name="repair-block" id="repair-block" value="1">
4448
<span class="glyphicon glyphicon-arrow-down" aria-hidden="true"></span> Function Down

app/templates/backend/rule/edit_rule.html

+6-4
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,10 @@
2626
<div class="form-group col-md-4">
2727
<label for="status">Status</label>
2828
<label class="radio" style="padding-left: 25px;">
29-
<input type="radio" name="status" id="status" value="1"
30-
{% if data.rule.status == 1 %}checked{% endif %}> On
29+
<input type="radio" name="status" id="status" value="1" {% if data.rule.status == 1 %}checked{% endif %}> On
3130
</label>
3231
<label class="radio" style="padding-left: 25px;">
33-
<input type="radio" name="status" id="status" value="0"
34-
{% if data.rule.status == 0 %}checked{% endif %}> Off
32+
<input type="radio" name="status" id="status" value="0" {% if data.rule.status == 0 %}checked{% endif %}> Off
3533
</label>
3634
</div>
3735
<div class="form-group col-md-4">
@@ -40,6 +38,10 @@
4038
<input type="radio" name="repair-block" id="repair-block" value="0" {% if data.rule.block_repair == 0 %}checked{% endif %}>
4139
<span class="glyphicon glyphicon-arrow-up" aria-hidden="true"></span> Function Up
4240
</label>
41+
<label class="radio" style="padding-left: 25px;">
42+
<input type="radio" name="repair-block" id="repair-block" value="2" {% if data.rule.block_repair == 2 %}checked{% endif %}>
43+
<span class="glyphicon glyphicon-arrow-left" aria-hidden="true"></span> Location Rule Line
44+
</label>
4345
<label class="radio" style="padding-left: 25px;">
4446
<input type="radio" name="repair-block" id="repair-block" value="1" {% if data.rule.block_repair == 1 %}checked{% endif %}>
4547
<span class="glyphicon glyphicon-arrow-down" aria-hidden="true"></span> Function Down

cobra.py

+2-39
Original file line numberDiff line numberDiff line change
@@ -12,49 +12,12 @@
1212
:license: MIT, see LICENSE for more details.
1313
:copyright: Copyright (c) 2016 Feei. All rights reserved
1414
"""
15-
import os
16-
import logging.config
1715
from app import web, manager
18-
from utils import config
16+
from utils import log, config
1917

2018

2119
def main():
22-
logs_directory = config.Config('cobra', 'logs_directory').value
23-
logs_directory = os.path.join(config.Config().project_directory, logs_directory)
24-
if os.path.isdir(logs_directory) is not True:
25-
os.mkdir(logs_directory)
26-
filename = os.path.join(logs_directory, 'cobra.log')
27-
logging.config.dictConfig({
28-
'version': 1,
29-
'disable_existing_loggers': True,
30-
'formatters': {
31-
'verbose': {
32-
'format': "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",
33-
'datefmt': "%Y-%m-%d %H:%M:%S"
34-
},
35-
'simple': {
36-
'format': '%(levelname)s %(message)s'
37-
},
38-
},
39-
'handlers': {
40-
'file': {
41-
'level': 'DEBUG',
42-
'class': 'cloghandler.ConcurrentRotatingFileHandler',
43-
'maxBytes': 1024 * 1024 * 10,
44-
'backupCount': 50,
45-
'delay': True,
46-
'filename': filename,
47-
'formatter': 'verbose'
48-
}
49-
},
50-
'loggers': {
51-
'': {
52-
'handlers': ['file'],
53-
'level': 'INFO',
54-
},
55-
}
56-
})
57-
20+
log.Log()
5821
debug = config.Config('cobra', 'debug').value
5922
web.debug = bool(debug)
6023
manager.run()

engine/parse.py

+52-30
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
import re
1818
import subprocess
1919
import traceback
20+
from utils import log
2021
import logging
2122

23+
log.Log()
2224
logging = logging.getLogger(__name__)
2325

2426

@@ -34,6 +36,7 @@ def __init__(self, rule, file_path, line, code):
3436
logging.info(file_path)
3537
self.line = line
3638
self.code = code
39+
self.param_name = None
3740

3841
def functions(self):
3942
logging.info('---------------------- [-]. Functions --------------------------------------')
@@ -97,45 +100,57 @@ def block_code(self, block_position):
97100
:param block_position:
98101
0:up
99102
1:down
103+
2:location_line
100104
:return:
101105
"""
102-
functions = self.functions()
103106
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)
127132
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 == '':
128143
param_block_code = False
129-
return param_block_code
130144
else:
131-
logging.info("Not found functions")
132-
return False
145+
param_block_code = False
146+
return param_block_code
133147

134148
def is_controllable_param(self):
135149
logging.info('---------------------- [2]. Param is controllable --------------------------------------')
136150
param_name = re.findall(self.rule, self.code)
151+
param_name = param_name[0].strip()
152+
self.param_name = param_name
137153
if len(param_name) == 1:
138-
param_name = param_name[0].strip()
139154
logging.info('P: {0}'.format(param_name))
140155
# controllable param
141156
# exclude class const (maybe misuse)
@@ -203,13 +218,18 @@ def is_controllable_param(self):
203218
else:
204219
logging.info("R: False (Not contained $)")
205220
return False
221+
else:
222+
logging.warning("Not Found Param")
206223

207224
def is_repair(self, repair_rule, block_repair):
208225
logging.info('---------------------- [3]. Is repair B:{0} --------------------------------------'.format(block_repair))
209226
code = self.block_code(block_repair)
210227
if code is False:
211228
logging.debug("R: Un Repair (repair code not match)")
212229
return False
230+
# replace repair {{PARAM}} const
231+
if '{{PARAM}' in repair_rule:
232+
repair_rule = repair_rule.replace('{{PARAM}', self.param_name)
213233
repair_result = re.findall(repair_rule, code)
214234
logging.debug(code)
215235
logging.debug(repair_result)
@@ -223,8 +243,10 @@ def is_repair(self, repair_rule, block_repair):
223243

224244
if __name__ == '__main__':
225245
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")
227247
if parse.is_controllable_param():
228-
parse.is_repair(r'fff', 1)
248+
parse.is_repair(r'fff', 2)
249+
else:
250+
print("UC")
229251
except Exception as e:
230252
print(traceback.print_exc())

0 commit comments

Comments
 (0)