-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathblindy_test.py
179 lines (126 loc) · 6.61 KB
/
blindy_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import unittest
import blindy
import re
from unittest.mock import patch, MagicMock
POST = "POST"
PARAMETERS_RAW = ["submit=true ", " admin= 1", "phrase = {}"]
PARAMETERS_AS_LIST_WITH_PLACEHOLDER = [['submit','true'], ['admin','1'], ['phrase','{}']]
PARAMETERS_AS_LIST_WITHOUT_PLACEHOLDER = [['submit','true'], ['admin','1'], ['another-phrase','some another phrase']]
HEADERS_AS_LIST=[['X-Some-Header', 'headervalue']]
HEADERS_AS_DICT={'X-Some-Header': 'headervalue'}
HEADERS_WITH_PLACEHOLDER=[['X-Some-Header', 'sql query goes here: {}']]
PHRASE_WITHOUT_PLACEHOLDER = "some phrase"
PHRASE_WITH_PLACEHOLDER = "some phrase with {} placeholder"
SIMPLE_PAYLOAD = {'submit': 'true', 'admin': '1', 'phrase': 'some+phrase'}
SIMPLE_PAYLOAD2 = {'submit': 'true', 'admin': '1', 'another-phrase': 'some+another+phrase'}
RESPONSE = ("<html>some response</html>",200)
URL = "http://url"
PATTERN = re.compile('.*response.*')
class TestStringMethods(unittest.TestCase):
def testsubstitute_placeholders(self):
# when
res = blindy.substitute_placeholders(PHRASE_WITH_PLACEHOLDER, 'placeholder', ['at', 'the', 'end'])
# then
self.assertEqual(res, "some phrase with {} 'at','the','end'")
def testprepare_payloadWithPlaceholder(self):
# when
res = blindy.prepare_payload(PARAMETERS_AS_LIST_WITH_PLACEHOLDER, PHRASE_WITHOUT_PLACEHOLDER, True)
# then
self.assertEqual(res, SIMPLE_PAYLOAD)
def testprepare_payloadWithoutPlaceholder(self):
# when
res = blindy.prepare_payload(PARAMETERS_AS_LIST_WITHOUT_PLACEHOLDER, PHRASE_WITHOUT_PLACEHOLDER, True)
# then
self.assertEqual(res, SIMPLE_PAYLOAD2)
def testParseParameters(self):
# when
res = blindy.parse_parameters(PARAMETERS_RAW, '=')
# then
self.assertEqual(res, PARAMETERS_AS_LIST_WITH_PLACEHOLDER)
@patch('requests.post')
def testin_POST_request(self, reguests_post):
# given
reguests_post.return_value = MagicMock(status_code=200)
#when
blindy.in_POST_request(SIMPLE_PAYLOAD, URL, HEADERS_AS_LIST)
# then
reguests_post.assert_called_with(URL, data=SIMPLE_PAYLOAD, headers=HEADERS_AS_LIST)
@patch('requests.get')
def testin_GET_request(self, reguests_get):
# given
reguests_get.return_value = MagicMock(status_code=200, text='it\'s ok')
#when
blindy.in_GET_request(SIMPLE_PAYLOAD, URL, HEADERS_AS_LIST)
# then
reguests_get.assert_called_with(URL, params=SIMPLE_PAYLOAD, headers=HEADERS_AS_LIST)
@patch('blindy.in_POST_request')
def testnot_bruteforce(self, ipr):
# given
ipr.return_value = RESPONSE
# when
blindy.not_bruteforce(PARAMETERS_AS_LIST_WITH_PLACEHOLDER, HEADERS_AS_LIST, PHRASE_WITHOUT_PLACEHOLDER, URL, ipr, PATTERN, True, True)
# then
ipr.assert_called_with(SIMPLE_PAYLOAD, URL, HEADERS_AS_DICT)
@patch('blindy.in_GET_request')
def testPositivePatternFound(self, igr):
# given
igr.side_effect = [RESPONSE if (i==20 or i==39 or i==44 or i==62) else ('Not a pattern', 500) for i in range(0,len(blindy.chars)+20+38+44+62)]
# when
result = blindy.bruteforce(PARAMETERS_AS_LIST_WITH_PLACEHOLDER, HEADERS_AS_LIST, PHRASE_WITH_PLACEHOLDER, URL, igr, PATTERN, True, True)
# then
self.assertEqual(result, 'user')
@patch('blindy.in_GET_request')
def testNegativePatternFound(self, igr):
# given
igr.side_effect = [('Not a pattern', 500) if (i==20 or i==39 or i==44 or i==62) else RESPONSE for i in range(0,len(blindy.chars)+20+38+44+62)]
# when
result = blindy.bruteforce(PARAMETERS_AS_LIST_WITH_PLACEHOLDER, HEADERS_AS_LIST, PHRASE_WITH_PLACEHOLDER, URL, igr, PATTERN, False, True)
# then
self.assertEqual(result, 'user')
@patch('blindy.run_with_callback')
def testRun_injectionQuerySetAsList(self, run_with_callback):
# when
blindy.run_injection("POST", PARAMETERS_AS_LIST_WITH_PLACEHOLDER, HEADERS_AS_LIST,
[PHRASE_WITH_PLACEHOLDER, PHRASE_WITH_PLACEHOLDER], URL, PATTERN, False, True)
# then
self.assertEqual(run_with_callback.call_count, 2)
@patch('blindy.run_with_callback')
def testRun_injectionQuerySetAsList(self, run_with_callback):
# when
blindy.run_injection(POST, PARAMETERS_AS_LIST_WITH_PLACEHOLDER, HEADERS_AS_LIST,
PHRASE_WITH_PLACEHOLDER, URL, PATTERN, False, True)
# then
run_with_callback.assert_called_once_with(POST, PARAMETERS_AS_LIST_WITH_PLACEHOLDER, HEADERS_AS_LIST,
PHRASE_WITH_PLACEHOLDER, URL, PATTERN, False, True)
@patch('blindy.bruteforce')
def test_assign_POST_verb(self, bf):
# given
bf.return_value = RESPONSE
# when
blindy.run_with_callback("POST", PARAMETERS_AS_LIST_WITH_PLACEHOLDER, HEADERS_AS_LIST, PHRASE_WITH_PLACEHOLDER, URL, PATTERN, False, True)
# then
bf.assert_called_with(PARAMETERS_AS_LIST_WITH_PLACEHOLDER, HEADERS_AS_LIST, PHRASE_WITH_PLACEHOLDER, URL, blindy.in_POST_request, PATTERN, False, True)
@patch('blindy.bruteforce')
def test_assign_GET_verb(self, bf):
# when
blindy.run_with_callback("GET", PARAMETERS_AS_LIST_WITH_PLACEHOLDER, HEADERS_AS_LIST, PHRASE_WITH_PLACEHOLDER, URL, PATTERN, False, True)
# then
bf.assert_called_with(PARAMETERS_AS_LIST_WITH_PLACEHOLDER, HEADERS_AS_LIST, PHRASE_WITH_PLACEHOLDER, URL, blindy.in_GET_request, PATTERN, False, True)
@patch('blindy.bruteforce')
def test_injection_in_header(self, bf):
# when
blindy.run_with_callback("GET", PARAMETERS_AS_LIST_WITHOUT_PLACEHOLDER, HEADERS_WITH_PLACEHOLDER,
PHRASE_WITH_PLACEHOLDER, URL,
PATTERN, False, True)
# then
bf.assert_called_with(PARAMETERS_AS_LIST_WITHOUT_PLACEHOLDER, HEADERS_WITH_PLACEHOLDER, PHRASE_WITH_PLACEHOLDER,
URL,
blindy.in_GET_request, PATTERN, False, True)
@patch('blindy.not_bruteforce')
def testrun_injectionNoBruteforceIfNoPlaceholderInQuery(self, not_bruteforce):
# when
blindy.run_with_callback("GET", PARAMETERS_AS_LIST_WITH_PLACEHOLDER, HEADERS_AS_LIST, PHRASE_WITHOUT_PLACEHOLDER, URL, PATTERN, False, True)
# then
not_bruteforce.assert_called_once_with(PARAMETERS_AS_LIST_WITH_PLACEHOLDER, HEADERS_AS_LIST, PHRASE_WITHOUT_PLACEHOLDER, URL, blindy.in_GET_request, PATTERN, False, True)
if __name__ == '__main__':
unittest.main()