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

Commit

Permalink
Merge pull request #91 from wufeifei/develop
Browse files Browse the repository at this point in the history
add location rule line for repair block & add {{param}} for repair rule
  • Loading branch information
FeeiCN authored Sep 10, 2016
2 parents 5a979a8 + 091936d commit ee4795c
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 236 deletions.
4 changes: 4 additions & 0 deletions app/templates/backend/rule/add_new_rule.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
<input type="radio" name="repair-block" id="repair-block" value="0">
<span class="glyphicon glyphicon-arrow-up" aria-hidden="true"></span> Function Up
</label>
<label class="radio" style="padding-left: 25px;">
<input type="radio" name="repair-block" id="repair-block" value="2">
<span class="glyphicon glyphicon-arrow-left" aria-hidden="true"></span> Location Rule Line
</label>
<label class="radio" style="padding-left: 25px;">
<input type="radio" name="repair-block" id="repair-block" value="1">
<span class="glyphicon glyphicon-arrow-down" aria-hidden="true"></span> Function Down
Expand Down
10 changes: 6 additions & 4 deletions app/templates/backend/rule/edit_rule.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@
<div class="form-group col-md-4">
<label for="status">Status</label>
<label class="radio" style="padding-left: 25px;">
<input type="radio" name="status" id="status" value="1"
{% if data.rule.status == 1 %}checked{% endif %}> On
<input type="radio" name="status" id="status" value="1" {% if data.rule.status == 1 %}checked{% endif %}> On
</label>
<label class="radio" style="padding-left: 25px;">
<input type="radio" name="status" id="status" value="0"
{% if data.rule.status == 0 %}checked{% endif %}> Off
<input type="radio" name="status" id="status" value="0" {% if data.rule.status == 0 %}checked{% endif %}> Off
</label>
</div>
<div class="form-group col-md-4">
Expand All @@ -40,6 +38,10 @@
<input type="radio" name="repair-block" id="repair-block" value="0" {% if data.rule.block_repair == 0 %}checked{% endif %}>
<span class="glyphicon glyphicon-arrow-up" aria-hidden="true"></span> Function Up
</label>
<label class="radio" style="padding-left: 25px;">
<input type="radio" name="repair-block" id="repair-block" value="2" {% if data.rule.block_repair == 2 %}checked{% endif %}>
<span class="glyphicon glyphicon-arrow-left" aria-hidden="true"></span> Location Rule Line
</label>
<label class="radio" style="padding-left: 25px;">
<input type="radio" name="repair-block" id="repair-block" value="1" {% if data.rule.block_repair == 1 %}checked{% endif %}>
<span class="glyphicon glyphicon-arrow-down" aria-hidden="true"></span> Function Down
Expand Down
41 changes: 2 additions & 39 deletions cobra.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,49 +12,12 @@
:license: MIT, see LICENSE for more details.
:copyright: Copyright (c) 2016 Feei. All rights reserved
"""
import os
import logging.config
from app import web, manager
from utils import config
from utils import log, config


def main():
logs_directory = config.Config('cobra', 'logs_directory').value
logs_directory = os.path.join(config.Config().project_directory, logs_directory)
if os.path.isdir(logs_directory) is not True:
os.mkdir(logs_directory)
filename = os.path.join(logs_directory, 'cobra.log')
logging.config.dictConfig({
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'verbose': {
'format': "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",
'datefmt': "%Y-%m-%d %H:%M:%S"
},
'simple': {
'format': '%(levelname)s %(message)s'
},
},
'handlers': {
'file': {
'level': 'DEBUG',
'class': 'cloghandler.ConcurrentRotatingFileHandler',
'maxBytes': 1024 * 1024 * 10,
'backupCount': 50,
'delay': True,
'filename': filename,
'formatter': 'verbose'
}
},
'loggers': {
'': {
'handlers': ['file'],
'level': 'INFO',
},
}
})

log.Log()
debug = config.Config('cobra', 'debug').value
web.debug = bool(debug)
manager.run()
Expand Down
82 changes: 52 additions & 30 deletions engine/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
import re
import subprocess
import traceback
from utils import log
import logging

log.Log()
logging = logging.getLogger(__name__)


Expand All @@ -34,6 +36,7 @@ def __init__(self, rule, file_path, line, code):
logging.info(file_path)
self.line = line
self.code = code
self.param_name = None

def functions(self):
logging.info('---------------------- [-]. Functions --------------------------------------')
Expand Down Expand Up @@ -97,45 +100,57 @@ def block_code(self, block_position):
:param block_position:
0:up
1:down
2:location_line
:return:
"""
functions = self.functions()
logging.info('---------------------- [-]. Block code B:{0} --------------------------------------'.format(block_position))
if functions:
block_start = 0
block_end = 0
for function_name, function_value in functions.items():
in_this_function = ''
if int(function_value['start']) < int(self.line) < int(function_value['end']):
in_this_function = '<---- {0}'.format(self.line)
if block_position == 0:
block_start = function_value['start']
block_end = int(self.line)
elif block_position == 1:
block_start = int(self.line)
block_end = function_value['end']
logging.info("F: {0} ({1} - {2}) {3}".format(function_name, function_value['start'], function_value['end'], in_this_function))
# get param block code
logging.info('C: {0} - {1}p'.format(block_start, block_end))
param = ['sed', "-n", "{0},{1}p".format(block_start, block_end), self.file_path]
p = subprocess.Popen(param, stdout=subprocess.PIPE)
result = p.communicate()
if len(result[0]):
param_block_code = result[0]
if param_block_code == '':
param_block_code = False
if block_position == 2:
line_rule = '{0}p'.format(self.line)
code = self.get_code(line_rule)
logging.info("C: {0}".format(code))
return code
else:
functions = self.functions()
if functions:
block_start = 0
block_end = 0
for function_name, function_value in functions.items():
in_this_function = ''
if int(function_value['start']) < int(self.line) < int(function_value['end']):
in_this_function = '<---- {0}'.format(self.line)
if block_position == 0:
block_start = function_value['start']
block_end = int(self.line) - 1
elif block_position == 1:
block_start = int(self.line) + 1
block_end = function_value['end']
logging.info("F: {0} ({1} - {2}) {3}".format(function_name, function_value['start'], function_value['end'], in_this_function))
# get param block code
logging.info('C: {0} - {1}p'.format(block_start, block_end))
line_rule = "{0},{1}p".format(block_start, block_end)
return self.get_code(line_rule)
else:
logging.info("Not found functions")
return False

def get_code(self, line_rule):
param = ['sed', "-n", line_rule, self.file_path]
p = subprocess.Popen(param, stdout=subprocess.PIPE)
result = p.communicate()
if len(result[0]):
param_block_code = result[0]
if param_block_code == '':
param_block_code = False
return param_block_code
else:
logging.info("Not found functions")
return False
param_block_code = False
return param_block_code

def is_controllable_param(self):
logging.info('---------------------- [2]. Param is controllable --------------------------------------')
param_name = re.findall(self.rule, self.code)
param_name = param_name[0].strip()
self.param_name = param_name
if len(param_name) == 1:
param_name = param_name[0].strip()
logging.info('P: {0}'.format(param_name))
# controllable param
# exclude class const (maybe misuse)
Expand Down Expand Up @@ -203,13 +218,18 @@ def is_controllable_param(self):
else:
logging.info("R: False (Not contained $)")
return False
else:
logging.warning("Not Found Param")

def is_repair(self, repair_rule, block_repair):
logging.info('---------------------- [3]. Is repair B:{0} --------------------------------------'.format(block_repair))
code = self.block_code(block_repair)
if code is False:
logging.debug("R: Un Repair (repair code not match)")
return False
# replace repair {{PARAM}} const
if '{{PARAM}' in repair_rule:
repair_rule = repair_rule.replace('{{PARAM}', self.param_name)
repair_result = re.findall(repair_rule, code)
logging.debug(code)
logging.debug(repair_result)
Expand All @@ -223,8 +243,10 @@ def is_repair(self, repair_rule, block_repair):

if __name__ == '__main__':
try:
parse = Parse('curl_setopt\s?\(.*,\s?CURLOPT_URL\s?,(.*)\)', '/path/to/your.php', '478', "curl_setopt($ch, CURLOPT_URL, $url);")
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")
if parse.is_controllable_param():
parse.is_repair(r'fff', 1)
parse.is_repair(r'fff', 2)
else:
print("UC")
except Exception as e:
print(traceback.print_exc())
Loading

0 comments on commit ee4795c

Please sign in to comment.