forked from endeav0r/rdis-lua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.lua
317 lines (266 loc) · 8.63 KB
/
http.lua
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
require "xavante"
local XAVANTE_PORT = 8000
rdis.console("XAVANTE_PORT = " .. tostring(XAVANTE_PORT))
local tpl = {}
tpl.css = [[
body, div, td {
font-family: terminus, monospace, courier new;
font-size: 10pt;
}
li {
list-style-type: square;
}
a {
color: #009;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
#menu {
display: none;
position: absolute;
padding: 4px;
background-color: #eee;
border: 1px solid #333;
}
.menuitem {
color: #930;
cursor: pointer;
}
]]
tpl.frames = [[
<html>
<head>
<title>Rdis, Cyber Style</title>
</head>
<frameset cols="20%, 80%">
<frame src="/functions" name="functions">
<frame src="/graph" name="graph">
</frameset>
</html>
]]
tpl.global = [[
<home>
<head>
<script src="http://code.jquery.com/jquery.min.js"></script>
<title>Rdis, Cyber Style</title>
<style type="text/css">%css%</style>
</head>
<body>
%content%
</body>
</home>
]]
tpl.functions = [[
<table>
<tr>
<td><strong><a href="/functions">Address</a></strong></td>
<td><strong><a href="/functions/sort/label">Label</a></strong></td>
</tr>
%functions_items%
</table>
]]
tpl.functions_items = [[
<tr>
<td>%address%</td>
<td><a href="/graph/%address%" target="graph">%name%</a></td>
</tr>
]]
tpl.graph = [[
<script src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript">
var graphurl = '/graph/png/%address';
var lastx = 0;
var lasty = 0;
function nocache () {
return '/nocache' + new Date().getTime();
}
$(document).ready(function () {
$(document).click(function(e) {
$('#menu').hide();
});
$('#graphpng').click(function(e) {
var x = (e.pageX - $('#graphpng').offset().left);
var y = (e.pageY - $('#graphpng').offset().top);
lastx = x;
lasty = y;
$('#graphpng').attr('src', '/graph/png/%address%/' + x + '/' + y + nocache());
});
$(document).keypress(function (e) {
if (e.which == 59) {
docomment(lastx, lasty);
}
});
function docomment (x, y) {
$.ajax({
url: "/graph/png/getcomment/%address%/" + x + "/" + y,
beforeSend: function ( xhr ) {
xhr.overrideMimeType("text/plain; charset=x-user-defined");
}
}).done(function (data) {
var new_comment = prompt("Enter new comment", data);
if (new_comment == null)
new_comment = '';
$.ajax({
url: "/graph/png/setcomment/%address%/" + x + "/" + y + "/" + new_comment,
beforeSend: function ( xhr ) {
xhr.overrideMimeType("text/plain; charset=x-user-defined");
}
}).done(function (data) {
$('#graphpng').attr('src', '/graph/png/%address%/nocache' + new Date().getTime());
});
});
}
$('#graphpng').bind('contextmenu', function(e) {
$('#menu').css({
left: e.pageX+'px',
top: e.pageY+'px'
}).show();
var x = (e.pageX - $('#graphpng').offset().left);
var y = (e.pageY - $('#graphpng').offset().top);
$('#graphpng').attr('src', '/graph/png/%address%/' + x + '/' + y + nocache());
return false;
});
$('#comment').click(function (e) {
var x = $('#menu').offset().left - $('#graphpng').offset().left;
var y = $('#menu').offset().top - $('#graphpng').offset().top;
docomment(x, y);
});
});
</script>
<img id="graphpng" src="/graph/png/%address%%xy%" />
<div id="menu">
<span class="menuitem" id="comment">Modify Comment</span>
</div>
]]
CALLBACKS = {}
function functions_sort (functions, sort_func)
local sorted = {}
for k,v in pairs(functions) do
local item = {address = k, name = v}
table.insert(sorted, item)
end
table.sort(sorted, sort_func)
return sorted
end
function tplize (template, replacements)
for k, v in pairs(replacements) do
template = string.gsub(template, '%%' .. k .. '%%', v)
end
return template
end
function do_functions (functions_sorted)
local functions_items = {}
for k,func in pairs(functions_sorted) do
local vars = {address = tostring(func.address), name = func.name}
local item = tplize(tpl.functions_items, vars)
table.insert(functions_items, item)
end
local content = tplize(tpl.functions, {functions_items=table.concat(functions_items)})
local vars = {
functions = table.concat(functions_items),
content = content,
css = tpl.css
}
return tplize(tpl.global, vars)
end
function rdis_handler (req, res)
res.headers['Content-Type'] = 'text/html'
res.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0'
res.headers['Expires'] = 'Thu, 01 Jan 1970 00:00:00 GMT'
print(req.srv, "request: " .. req.relpath)
if req.relpath == '/' then
res.content = tpl.frames
return res
elseif req.relpath == '/graph' then
res.content = 'graph here'
return res
elseif req.relpath == '/functions/sort/label' then
local functions_sorted = functions_sort(rdis.functions(), function (lhs, rhs)
if lhs.name < rhs.name then
return true
end
return false
end)
res.content = do_functions (functions_sorted)
return res
elseif req.relpath == '/functions' then
local functions_sorted = functions_sort(rdis.functions(), function (lhs, rhs)
if lhs.address < rhs.address then
return true
end
return false
end)
res.content = do_functions(functions_sorted)
return res
elseif string.match(req.relpath, '^/graph/0x[%dabcdef]+$') then
local address = string.match(req.relpath, '/graph/(0x[%dabcdef]+)')
local vars = {address=address, xy=''}
local content = tplize(tpl.graph, vars)
vars = {
content = content,
css = tpl.css
}
res.content = tplize(tpl.global, vars)
return res
elseif string.match(req.relpath, '^/graph/png/0x[%dabcdef]+/%d+/%d+') then
local pattern = '/graph/png/(0x[%dabcdef]+)/(%d+)/(%d+)'
local address, x, y = string.match(req.relpath, pattern)
local rdg = rdis.rdg(uint64(address))
local ins = rdg:ins_by_coords(x, y)
if ins ~= nil then
local node = rdg:node_by_coords(x, y)
local node_index = rdg:node_by_coords(x, y):index()
local ins_address = rdg:ins_by_coords(x, y):address()
rdg:highlight_ins(node_index, ins_address)
end
res.headers['Content-Type'] = 'image/png'
rdg:save_png('/tmp/rdis.png')
local fh = io.open('/tmp/rdis.png')
local png = fh:read('*a')
fh:close()
res.content = png
return res
elseif string.match(req.relpath, '^/graph/png/getcomment/0x[%dabcdef]+/%d+/%d+$') then
local pattern = '/graph/png/getcomment/(0x[%dabcdef]+)/(%d+)/(%d+)'
local address, x, y = string.match(req.relpath, pattern)
local rdg = rdis.rdg(uint64(address))
local ins = rdg:ins_by_coords(x, y)
res.content = ins:comment()
return res
elseif string.match(req.relpath, '^/graph/png/setcomment/0x[%dabcdef]+/%d+/%d+/[%w%s%p]*$') then
local pattern = '/graph/png/setcomment/(0x[%dabcdef]+)/(%d+)/(%d+)/([%w%s%p]*)'
local address, x, y, comment = string.match(req.relpath, pattern)
local rdg = rdis.rdg(uint64(address))
local node = rdg:node_by_coords(x, y)
local ins = rdg:ins_by_coords(x, y)
rdis.set_ins_comment(node:index(), ins:address(), comment)
res.content = 'done'
return res
elseif string.match(req.relpath, '^/graph/png/0x[%dabcdef]+') then
local address = string.match(req.relpath, '/graph/png/(0x[%dabcdef]+)')
local rdg = rdis.rdg(uint64(address))
rdg:save_png('/tmp/rdis.png')
res.headers['Content-Type'] = 'image/png'
local fh = io.open('/tmp/rdis.png', 'r')
local png = fh:read('*a')
fh:close()
res.content = png
return res
else
print("bad query")
end
end
local simplerules = {
{
match = '.*',
with = rdis_handler
}
}
xavante.HTTP {
server = {host = "*", port = XAVANTE_PORT},
defaultHost = {
rules = simplerules
}
}