Completed
Push — master ( d10f0c...96f109 )
by -
01:33
created

rule()   B

Complexity

Conditions 7

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 8.4953
Metric Value
cc 7
dl 0
loc 20
rs 7.3333
ccs 11
cts 16
cp 0.6875
crap 8.4953
1 1
try:
2 1
    from urlparse import parse_qs
3
except ImportError:  # python3
4
    from urllib.parse import parse_qs
5
6 1
from flask import Blueprint, render_template, request, redirect, flash, url_for
7
8 1
from spike.model.naxsi_rules import NaxsiRules
9 1
from spike.model.naxsi_whitelist import NaxsiWhitelist
10
11 1
sandbox = Blueprint('sandbox', __name__)
12
13
14 1
@sandbox.route("/", methods=["GET"])
15
def index():
16 1
    return render_template("misc/sandbox.html")
17
18
19 1
@sandbox.route("/explain_rule/", methods=["GET", "POST"])
20
def explain_rule():
21 1
    rule_get = request.args.get('rule', '')
22 1
    rule_post = request.form.get("rule", '')
23 1
    if rule_get.isdigit():  # explain a rule by id
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
24
        _rule = NaxsiRules.query.filter(NaxsiRules.sid == rule_get).first()
0 ignored issues
show
Bug introduced by
The Class NaxsiRules does not seem to have a member named query.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
25 1
        if _rule is None:
26 1
            flash('Not rule with id %s' % rule_get)
27
            return redirect(url_for("sandbox.index"))
28 1
    elif rule_get is not '':
29 1
        flash('Please provide a numeric id')
30 1
        return redirect(url_for("sandbox.index"))
31 1
    elif not rule_post:
32 1
        flash('Please provide a rule')
33
        return redirect(url_for("sandbox.index"))
34
    else:
35
        _rule = NaxsiRules()
36
        _rule.parse_rule(rule_post)
37
38
    if 'visualise_rule' in request.form:
39
        if _rule.detection.startswith('rx:'):
40
            return redirect('https://regexper.com/#' + _rule.detection[3:])
41 1
        else:
42
            flash('The rule is not a regexp, so you can not visualize it.', category='error')
43 1
44 1
    if hasattr(_rule, 'error'):
45 1
        for error in _rule.error:
46 1
            flash(error, category='error')
47 1
    if hasattr(_rule, 'warning'):
48 1
        for warnings in _rule.warnings:
49 1
            flash(warnings, category='warning')
50 1
51 1
    return render_template("misc/sandbox.html", rule_explaination=_rule.explain(), rule=_rule)
52 1
53 1
54 1
@sandbox.route("/explain_whitelist/", methods=["GET", "POST"])
55 1
def explain_whitelist():
56
    whitelist_get = request.args.get('whitelist', '')
57 1
    whitelist_post = request.form.get('whitelist', '')
58 1
    if whitelist_get.isdigit():  # explain a whitelist by id
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
        _wlist = NaxsiWhitelist.query.filter(NaxsiWhitelist.id == whitelist_get).first()
0 ignored issues
show
Bug introduced by
The Class NaxsiWhitelist does not seem to have a member named query.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
60 1
        if _wlist is None:
61
            flash('Not rule with id %s' % whitelist_get.id)
62
            return redirect(url_for("sandbox.index"))
63 1
    elif whitelist_get is not '':
64
        flash('Please provide a numeric id')
65 1
        return redirect(url_for("sandbox.index"))
66 1
    elif not whitelist_post:
67 1
        flash('Please provide a whitelist')
68
        return redirect(url_for("sandbox.index"))
69
    else:
70
        _wlist = NaxsiWhitelist()
71
        _wlist.parse(whitelist_post)
72 1
73
    if hasattr(_wlist, 'error'):
74
        for error in _wlist.error:
75 1
            flash(error, category='error')
76
    if hasattr(_wlist, 'warning'):
77
        for warnings in _wlist.warnings:
78
            flash(warnings, category='warning')
79 1
80 1
    return render_template("misc/sandbox.html", whitelist_explaination=_wlist.explain(), whitelist=_wlist)
81
82 1
83 1
@sandbox.route('/explain_nxlog/', methods=["POST"])
84 1
def explain_nxlog():
85
    nxlog = request.form.get("nxlog", '')
86 1
    if not nxlog:
87
        return redirect(url_for("sandbox.index"))
88
89 1
    start = nxlog.find("ip=")
90
    if start < 0:
91 1
        flash('{} is an invalid extlog, string "ip=" not found.'.format(nxlog))
92 1
        return redirect(url_for("sandbox.index"))
93 1
94
    end = nxlog.find(", ")
95 1
    if end < 0:
96 1
        flash('{} is an invalid extlog, string "," not found.'.format(nxlog))
97 1
        return redirect(url_for("sandbox.index"))
98 1
99
    # Flatten the dict, since parse_qs is a bit annoying
100 1
    nxdic = parse_qs(nxlog[start:end])
101 1
    for key, value in nxdic.items():
102 1
        nxdic[key] = value[0]
103 1
104
    explain = "Peer <strong>{}</strong> performed a request to <strong>{}</strong> on URI <strong>{}</strong> ".format(
105
        nxdic['ip'], nxdic['server'], nxdic['uri'])
106 1
107 1
    scores = list()
108 1
    cpt = 0
109
    while "cscore{}".format(cpt) in nxdic:
110 1
        cscore = "cscore{}".format(cpt)
111
        score = "score{}".format(cpt)
112
        scores.append("that reached a <strong>{}</strong> score of <strong>{}</strong> ".format(
113 1
            nxdic[cscore], nxdic[score]))
114 1
        cpt += 1
115 1
    explain += ' and '.join(scores)
116 1
117 1
    cpt = 0
118 1
    named = list()
119
    while "id{}".format(cpt) in nxdic:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120 1
        _id = "id{}".format(cpt)
121 1
        _var_name = "var_name{}".format(cpt)
122
        _zone = "zone{}".format(cpt)
123 1
        if "var_name{}".format(cpt) in nxdic:
124 1
            named.append("id <strong>{}</strong> in var named <strong>{}</strong> of zone <strong>{}</strong>".format(
125 1
                nxdic[_id], nxdic[_var_name], nxdic[_zone]))
126 1
        else:
127 1
            named.append("id <strong>{}</strong> in zone <strong>{}</strong>".format(nxdic[_id], nxdic[_zone]))
128 1
        cpt += 1
129 1
    explain += ' and '.join(named)
130 1
131
    return render_template("misc/sandbox.html", nxlog_explaination=explain, nxlog=nxlog)
132