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

Commit 3ce3487

Browse files
committed
completed #97
1 parent 2f6b421 commit 3ce3487

File tree

10 files changed

+65
-41
lines changed

10 files changed

+65
-41
lines changed

app/controller/api.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ def add_task():
6464
new_version = data.get('new_version')
6565
old_version = data.get('old_version')
6666

67+
# one-click scan for manage projects
6768
project_id = data.get('project_id')
68-
if project_id:
69+
if project_id is not None:
6970
project = CobraProjects.query.filter_by(id=project_id).first()
7071
if not project:
7172
return jsonify(code=1002, result=u'not find the project.')
@@ -104,10 +105,16 @@ def status_task():
104105
}
105106
status_text = status[c.status]
106107
domain = config.Config('cobra', 'domain').value
108+
# project_id
109+
project_info = CobraProjects.query.filter_by(repository=c.target).first()
110+
if project_info:
111+
report = 'http://' + domain + '/report/' + str(project_info.id)
112+
else:
113+
report = 'http://' + domain
107114
result = {
108115
'status': status_text,
109116
'text': 'Success',
110-
'report': 'http://' + domain + '/report/' + str(scan_id),
117+
'report': report,
111118
'allow_deploy': True
112119
}
113120
return jsonify(status=1001, result=result)

app/controller/backend/ProjectsController.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from flask import render_template, request, jsonify, redirect
1717

1818
from . import ADMIN_URL
19+
from utils import config
1920
from app import web, db
2021
from app.CommonClass.ValidateClass import ValidateClass, login_required
2122
from app.models import CobraProjects
@@ -29,9 +30,11 @@
2930
@login_required
3031
def projects(page):
3132
per_page = 10
32-
project = CobraProjects.query.order_by(CobraProjects.id.desc()).limit(per_page).offset((page - 1) * per_page).all()
33+
projects = CobraProjects.query.order_by(CobraProjects.id.desc()).limit(per_page).offset((page - 1) * per_page).all()
34+
for project in projects:
35+
project.report = 'http://' + config.Config('cobra', 'domain').value + '/report/' + str(project.id)
3336
data = {
34-
'projects': project,
37+
'projects': projects,
3538
}
3639
return render_template("backend/project/projects.html", data=data)
3740

app/controller/backend/TasksController.py

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ def tasks(page):
3636
# replace data
3737
for task in tasks:
3838
task.scan_way = "Full Scan" if task.scan_way == 1 else "Diff Scan"
39-
task.report = 'http://' + config.Config('cobra', 'domain').value + '/report/' + str(task.id)
4039
data = {
4140
'tasks': tasks,
4241
}

app/controller/route.py

+18-18
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ def homepage():
4343
return render_template('index.html', data=data)
4444

4545

46-
@web.route('/report/<int:task_id>', methods=['GET'])
47-
def report(task_id):
46+
@web.route('/report/<int:project_id>', methods=['GET'])
47+
def report(project_id):
4848
# 获取筛选数据
4949
search_vul_type = request.args.get("search_vul_type", None)
5050
search_rule = request.args.get("search_rule", None)
@@ -53,9 +53,10 @@ def report(task_id):
5353
page = int(request.args.get("page", 1))
5454

5555
# 检测 task id 是否存在
56-
task_info = CobraTaskInfo.query.filter_by(id=task_id).first()
57-
if not task_info:
56+
project_info = CobraProjects.query.filter_by(id=project_id).first()
57+
if not project_info:
5858
return jsonify(status="4004", msg="report id not found.")
59+
task_info = CobraTaskInfo.query.filter_by(target=project_info.repository).order_by(CobraTaskInfo.id.desc()).first()
5960

6061
# 获取task的信息
6162
repository = task_info.target
@@ -75,30 +76,29 @@ def report(task_id):
7576
time_end = time.strftime("%H:%M:%S", time.localtime(time_end))
7677

7778
# 获取project信息
78-
project = CobraProjects.query.filter_by(repository=repository).first()
79-
if project is None:
79+
if project_info is None:
8080
project_name = repository
8181
project_id = 0 # add l4yn3
8282
author = 'Anonymous'
8383
project_description = 'Compress Project'
8484
project_framework = 'Unknown Framework'
8585
project_url = 'Unknown URL'
8686
else:
87-
project_name = project.name
88-
project_id = project.id
89-
author = project.author
90-
project_description = project.remark
91-
project_framework = project.framework
92-
project_url = project.url
87+
project_name = project_info.name
88+
project_id = project_info.id
89+
author = project_info.author
90+
project_description = project_info.remark
91+
project_framework = project_info.framework
92+
project_url = project_info.url
9393

9494
# 获取漏洞总数量
95-
scan_results = CobraResults.query.filter_by(task_id=task_id).all()
95+
scan_results = CobraResults.query.filter_by(project_id=project_id).all()
9696
total_vul_count = len(scan_results)
9797

9898
# 获取出现的漏洞类型
9999
res = db.session.query(count().label("vul_number"), CobraVuls.name, CobraVuls.id).filter(
100100
and_(
101-
CobraResults.task_id == task_id,
101+
CobraResults.project_id == project_id,
102102
CobraResults.rule_id == CobraRules.id,
103103
CobraVuls.id == CobraRules.vul_id,
104104
)
@@ -114,7 +114,7 @@ def report(task_id):
114114
# 获取触发的规则类型
115115
res = db.session.query(CobraRules.description, CobraRules.id).filter(
116116
and_(
117-
CobraResults.task_id == task_id,
117+
CobraResults.project_id == project_id,
118118
CobraResults.rule_id == CobraRules.id,
119119
CobraVuls.id == CobraRules.vul_id
120120
)
@@ -126,7 +126,7 @@ def report(task_id):
126126
# 检索不同等级的漏洞数量
127127
res = db.session.query(count().label('vuln_number'), CobraRules.level).filter(
128128
and_(
129-
CobraResults.task_id == task_id,
129+
CobraResults.project_id == project_id,
130130
CobraResults.rule_id == CobraRules.id,
131131
CobraVuls.id == CobraRules.vul_id,
132132
)
@@ -150,7 +150,7 @@ def report(task_id):
150150

151151
# 检索全部的漏洞信息
152152
filter_group = (
153-
CobraResults.task_id == task_id,
153+
CobraResults.project_id == project_id,
154154
CobraResults.rule_id == CobraRules.id,
155155
CobraVuls.id == CobraRules.vul_id,
156156
)
@@ -234,7 +234,7 @@ def report(task_id):
234234
pagination = Pagination(page=page, total=len(total_number), per_page=page_size, bs_version=3)
235235

236236
data = {
237-
'id': int(task_id),
237+
'id': int(project_id),
238238
'project_name': project_name,
239239
'project_id': project_id,
240240
'project_repository': repository,

app/models.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -179,22 +179,26 @@ class CobraResults(db.Model):
179179
__tablename__ = 'results'
180180

181181
id = db.Column(INTEGER(unsigned=True), primary_key=True, autoincrement=True, nullable=False)
182-
task_id = db.Column(INTEGER(11), nullable=False, default=None)
183-
rule_id = db.Column(INTEGER(11), nullable=False, default=None)
182+
task_id = db.Column(INTEGER, nullable=False, default=None)
183+
project_id = db.Column(INTEGER, nullable=False, default=None)
184+
rule_id = db.Column(INTEGER, nullable=False, default=None)
184185
file = db.Column(db.String(512), nullable=False, default=None)
185186
line = db.Column(INTEGER(11), nullable=False, default=None)
186187
code = db.Column(db.String(512), nullable=False, default=None)
188+
status = db.Column(TINYINT, default=None, nullable=False)
187189
created_at = db.Column(db.DateTime, nullable=False, default=None)
188190
updated_at = db.Column(db.DateTime, nullable=False, default=None)
189191

190192
__table_args__ = (Index('ix_task_id_rule_id', task_id, rule_id), {"mysql_charset": "utf8mb4"})
191193

192-
def __init__(self, task_id, rule_id, file_path, line, code, created_at=None, updated_at=None):
194+
def __init__(self, task_id, project_id, rule_id, file_path, line, code, status, created_at=None, updated_at=None):
193195
self.task_id = task_id
196+
self.project_id = project_id
194197
self.rule_id = rule_id
195198
self.file = file_path
196199
self.line = line
197200
self.code = code
201+
self.status = status
198202
self.created_at = created_at
199203
self.updated_at = updated_at
200204
current_time = time.strftime('%Y-%m-%d %X', time.localtime())

app/templates/backend/project/projects.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<tbody id="main-table">
1515
{% for project in data.projects %}
1616
<tr>
17-
<td>{{ project.id }}</td>
17+
<td><a href="{{ project.report }}" target="_blank">{{ project.id }}</a></td>
1818
<td>{{ project.name }}</td>
1919
<td>{{ project.author }}</td>
2020
<td>{{ project.repository }}</td>

app/templates/backend/task/tasks.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<tbody id="main-table">
1414
{% for task in data.tasks %}
1515
<tr>
16-
<td><a href="{{ task.report }}" target="_blank">{{ task.id }}</a></td>
16+
<td>{{ task.id }}</td>
1717
<td>{{ task.branch }}</td>
1818
<td>{{ task.scan_way }}</td>
1919
<td>{{ task.file_count }}</td>

app/templates/report.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
<div class="col-xs-12">
6969
<div class="invoice-title">
7070
<h2>项目({{ data.project_name | upper }})代码安全审计报告</h2>
71-
<h3 class="pull-right">Cobra报告编号 # {{ data.id }}</h3>
71+
<h3 class="pull-right">Cobra项目编号 # {{ data.id }}</h3>
7272
</div>
7373
<hr>
7474
<div class="row">

engine/scan.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
import getpass
1919
import logging
2020
from app import db, CobraProjects, CobraTaskInfo
21-
from utils import config, decompress
21+
from utils import config, decompress, log
2222
from pickup import git
2323
from engine import detection
2424

25+
log.Log()
2526
logging = logging.getLogger(__name__)
2627

2728

@@ -61,6 +62,7 @@ def compress(self):
6162
def version(self, branch=None, new_version=None, old_version=None):
6263
# Gitlab
6364
if '.git' in self.target:
65+
logging.info('Gitlab project')
6466
# Git
6567
if 'gitlab' in self.target:
6668
username = config.Config('git', 'username').value
@@ -100,24 +102,26 @@ def version(self, branch=None, new_version=None, old_version=None):
100102

101103
# detection framework for project
102104
framework, language = detection.Detection(repo_directory).framework()
103-
project_framework = '{0} ({1})'.format(framework, language)
105+
if framework != '' or language != '':
106+
project_framework = '{0} ({1})'.format(framework, language)
107+
else:
108+
project_framework = ''
109+
project_id = 0
104110
if not p:
105111
# insert into project table.
106112
project = CobraProjects(self.target, '', repo_name, repo_author, project_framework, '', '', current_time)
107-
project_id = project.id
108113
else:
109114
project_id = p.id
110-
111115
# update project's framework
112116
p.framework = project_framework
113117
db.session.add(p)
114-
db.session.commit()
115118
try:
116119
db.session.add(task)
117120
if not p:
118121
db.session.add(project)
119122
db.session.commit()
120-
123+
if not p:
124+
project_id = project.id
121125
cobra_path = os.path.join(config.Config().project_directory, 'cobra.py')
122126

123127
if os.path.isfile(cobra_path) is not True:

engine/static.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -190,15 +190,22 @@ def analyse(self):
190190
if line == '':
191191
continue
192192
if rule.regex_location.strip() == '':
193-
# Find
193+
# Find (special file)
194194
file_path = line.strip().replace(self.directory, '')
195195
logging.debug('File: {0}'.format(file_path))
196-
vul = CobraResults(self.task_id, rule.id, file_path, 0, '')
197-
db.session.add(vul)
196+
exist_result = CobraResults.query.filter_by(project_id=self.project_id, rule_id=rule.id, file=file_path).first()
197+
if exist_result is not None:
198+
logging.warning("Exists Result")
199+
else:
200+
vul = CobraResults(self.task_id, self.project_id, rule.id, file_path, 0, '', 0)
201+
db.session.add(vul)
198202
else:
199203
# Grep
200204
line_split = line.split(':', 1)
201205
file_path = line_split[0].strip()
206+
if len(line_split) < 2:
207+
logging.info("Line len < 2 {0}".format(line))
208+
continue
202209
code_content = line_split[1].split(':', 1)[1].strip()
203210
line_number = line_split[1].split(':', 1)[0].strip()
204211

@@ -237,15 +244,15 @@ def analyse(self):
237244

238245
if found_vul:
239246
logging.info('In Insert')
240-
exist_result = CobraResults.query.filter_by(task_id=self.task_id, rule_id=rule.id, file=file_path, line=line_number).first()
247+
exist_result = CobraResults.query.filter_by(project_id=self.project_id, rule_id=rule.id, file=file_path, line=line_number).first()
241248
if exist_result is not None:
242-
logging.warning("Exists Result")
249+
logging.info("Exists Result")
243250
else:
244251
code_content = '# 触发位置\r' + code_content
245252
if param_value is not None:
246253
code_content = '# 参数可控\r' + param_value + '\r//\r// ------ 省略部分代码 ------\r//\r' + code_content
247254
logging.debug('File: {0}:{1} {2}'.format(file_path, line_number, code_content))
248-
vul = CobraResults(self.task_id, rule.id, file_path, line_number, code_content)
255+
vul = CobraResults(self.task_id, self.project_id, rule.id, file_path, line_number, code_content, 0)
249256
db.session.add(vul)
250257
logging.info('Insert Results Success')
251258
db.session.commit()

0 commit comments

Comments
 (0)