-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.py
164 lines (136 loc) · 4.31 KB
/
history.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
"""
Copyright notice
================
Copyright (C) 2011
Roberto Paleari <[email protected]>
Alessandro Reina <[email protected]>
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
HyperDbg is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
"""
import threading
import datetime, base64
from xml.sax.saxutils import escape as xmlescape
# Synchronization decorator
def synchronized(lock):
def wrap(f):
def new_function(*args, **kw):
lock.acquire()
try:
return f(*args, **kw)
finally:
lock.release()
return new_function
return wrap
class HttpHistoryEntry:
def __init__(self, idz, oreq = None, mreq = None, ores = None, mres = None):
self.id = idz # Entry identified (mandatory)
self.setOriginalRequest(oreq)
self.setOriginalResponse(ores)
self.setMangledRequest(mreq)
self.setMangledResponse(mres)
def setOriginalRequest(self, r):
if r is None:
t = None
else:
t = datetime.datetime.now()
self.oreq_time = t
self.oreq = r
def setOriginalResponse(self, r):
if r is None:
t = None
else:
t = datetime.datetime.now()
self.ores_time = t
self.ores = r
def setMangledRequest(self, r):
if r is None:
t = None
else:
t = datetime.datetime.now()
self.mreq_time = t
self.mreq = r
def setMangledResponse(self, r):
if r is None:
t = None
else:
t = datetime.datetime.now()
self.mres_time = t
self.mres = r
class HttpHistory:
# Synchronization lock
lock = threading.Lock()
def __init__(self):
self.__history = []
@synchronized(lock)
def allocate(self):
idz = len(self.__history)
h = HttpHistoryEntry(idz = idz)
self.__history.append(h)
return idz
@synchronized(lock)
def __getitem__(self, idz):
return self.__history[idz]
def count(self):
"""
Count requests and responses. Return a tuple (#req, #res).
"""
nreq, nres = 0, 0
for entry in self.__history:
if entry.oreq is not None:
nreq += 1
if entry.ores is not None:
nres += 1
return nreq, nres
def dumpXML(self):
t = datetime.datetime.now()
# Document header
s = """\
<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>
<Head>
<Timestamp>%s</Timestamp>
</Head>
<Entries>
""" % t
# Process single HTTP entries
for entry in self.__history:
s += " <Entry>\n"
s += " <ID>%d</ID>\n" % entry.id
for attr, name in [
("oreq", "OriginalRequest"),
("mreq", "MangledRequest"),
("ores", "OriginalResponse"),
("mres", "MangledResponse"),
]:
v = getattr(entry, attr)
t = getattr(entry, attr + "_time")
if v is not None:
s += """\
<%s>
<Timestamp>%s</Timestamp>
<Data>
""" % (name, t)
# Process entry headers
for hname, hvalues in v.headers.iteritems():
for hvalue in hvalues:
s += """\
<Header>
<Name>%s</Name>
<Value>%s</Value>
</Header>
""" % (xmlescape(hname), xmlescape(hvalue))
# Process entry body and close tag
s += """\
<Body>%s</Body>
</Data>
</%s>
""" % (base64.encodestring(v.body), name)
s += " </Entry>\n"
s += "</Entries>\n"
return s