-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmitm_archive_http.py
More file actions
59 lines (43 loc) · 1.2 KB
/
Copy pathmitm_archive_http.py
File metadata and controls
59 lines (43 loc) · 1.2 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
"""
Mirror all web pages.
Useful if you are living down under.
"""
import hashlib
import pathlib
import os
import json
import time
from mitmproxy import http
DB = "/home/dragon/Archive/web/WebWar"
SAVE_HEADERS = True
def __quote(s):
return '"' + s.replace("\\", "\\\\").replace("\"", "\\\"").replace("\n", "\\n") + '"'
def __save_contents(basedir, content):
"""
Save the content to a file named after the hash of the file. Returns hash.
"""
h = hashlib.sha256(content).hexdigest()
os.makedirs(basedir, exist_ok = True)
with open(f"{basedir}/{h}", "wb") as f:
f.write(content)
return h
def response(flow: http.HTTPFlow) -> None:
if flow.response and flow.response.content:
host = flow.request.host
url = flow.request.url
ch = __save_contents(f"{DB}/{host}", flow.response.content)
hh = __save_contents(f"{DB}/{host}", bytes(flow.response.headers)) if SAVE_HEADERS else None
mpath = f"{DB}/{host}/map.json"
m = []
try:
m = json.loads(pathlib.Path(mpath).read_text())
except:
pass
m.append({
"url": url,
"time": int(time.time()),
"content": ch,
})
if (SAVE_HEADERS):
m[-1]["headers"] = hh
pathlib.Path(mpath).write_text(json.dumps(m, indent = 4))