qira/middleware/qira_webstatic.py

186 lines
5.0 KiB
Python
Raw Normal View History

2014-10-12 22:27:37 +04:00
# eventually, this can live in a different process
# or we can break the boundary at static2
# these calls don't have to be included for qira to work
2014-10-12 22:27:37 +04:00
import qira_config
2014-10-12 22:27:37 +04:00
from qira_webserver import socketio
from qira_webserver import socket_method
from qira_webserver import app
2014-10-12 22:33:57 +04:00
from flask import Flask, Response, redirect, request
2014-10-12 22:32:58 +04:00
from flask.ext.socketio import SocketIO, emit
from qira_base import *
import json
2014-10-12 22:48:55 +04:00
import os
2014-10-12 22:32:58 +04:00
2014-11-13 19:21:51 +03:00
def init(lprogram):
global program
program = lprogram
2014-10-12 22:27:37 +04:00
# *** STATIC CALLS FROM THE FRONTEND ***
@socketio.on('getnames', namespace='/qira')
@socket_method
def getnames(addrs):
ret = program.static.get_tags(['name'], map(fhex, addrs))
send = {}
for addr in ret:
send[ghex(addr)] = ret[addr]['name']
emit('names', send)
# TODO: this is a shitty function
@app.route('/gettagsa', methods=["POST"])
@socket_method
def gettagsa():
arr = json.loads(request.data)
ret = []
for i in arr:
i = fhex(i)
# always return them all
# a bit of a hack, this is so javascript can display it
rret = {}
for tags in ['name', 'comment']:
rret[tags] = program.static[i][tags]
rret['address'] = ghex(i)
ret.append(rret)
return json.dumps(ret)
@socketio.on('gotoname', namespace='/qira')
@socket_method
def gotoname(name):
addr = program.static.get_address_by_name(name)
if addr != None:
emit('setiaddr', ghex(addr))
@socketio.on('settags', namespace='/qira')
@socket_method
def settags(tags):
for addr in tags:
naddr = fhex(addr)
for i in tags[addr]:
program.static[naddr][i] = tags[addr][i]
2014-10-12 22:48:55 +04:00
# dot as a service
@app.route('/dot', methods=["POST"])
def graph_dot():
req = request.data
#print "DOT REQUEST", req
f = open("/tmp/in.dot", "w")
f.write(req)
f.close()
os.system("dot /tmp/in.dot > /tmp/out.dot")
ret = open("/tmp/out.dot").read()
#print "DOT RESPONSE", ret
return ret
2014-11-13 19:21:51 +03:00
# currently if we aren't using static, we don't want to draw the staticview
# or be able to makefunction
if qira_config.WITH_STATIC:
@socketio.on('getstaticview', namespace='/qira')
@socket_method
def getstaticview(haddr, flat, flatrange):
2014-11-23 03:25:48 +03:00
def copy_fields(bbb, stat):
bbb['type'] = stat['type']
bbb['comment'] = stat['comment']
if 'instruction' in stat:
bbb['instruction'] = str(stat['instruction'])
2014-11-13 19:21:51 +03:00
fxn = program.static[fhex(haddr)]['function']
if fxn == None or flat == True:
addr = fhex(haddr)
# not a function, return flat view
ret = []
# find backward
i = addr
while len(ret) != abs(flatrange[0]):
did_append = False
# search up to 256 back
for j in range(1, 256):
if 'len' in program.static[i-j] and program.static[i-j]['len'] == j:
i -= j
bbb = {'address': ghex(i)}
bbb['bytes'] = map(ord, program.static.memory(i, j))
ret.append(bbb)
did_append = True
break
if not did_append:
i -= 1
bbb = {'address': ghex(i)}
bbb['bytes'] = map(ord, program.static.memory(i, 1))
ret.append(bbb)
ret = ret[::-1]
# find forward
i = addr
while len(ret) != abs(flatrange[0]) + flatrange[1]:
bbb = {'address': ghex(i)}
#print program.tags[i]
if 'len' in program.static[i]:
l = program.static[i]['len']
if l == 0:
l = 1
else:
l = 1
bbb['bytes'] = map(ord, program.static.memory(i, l))
i += l
ret.append(bbb)
2014-10-12 22:32:58 +04:00
2014-11-13 19:21:51 +03:00
for bbb in ret:
a = fhex(bbb['address'])
2014-11-23 03:25:48 +03:00
copy_fields(bbb, program.static[a])
2014-11-13 19:21:51 +03:00
# dests?
emit('flat', ret)
else:
blocks = []
for b in fxn.blocks:
bb = []
for i in sorted(b.addresses):
bbb = {'address': ghex(i)}
2014-11-23 03:25:48 +03:00
copy_fields(bbb, program.static[i])
2014-11-13 19:21:51 +03:00
bbb['dests'] = map(lambda (x,y): (ghex(x), y), program.static[i]['instruction'].dests())
bb.append(bbb)
blocks.append(bb)
emit('function', {'blocks': blocks})
2014-11-23 03:25:48 +03:00
@socketio.on('make', namespace='/qira')
2014-11-13 19:21:51 +03:00
@socket_method
2014-11-23 03:25:48 +03:00
def make(typ, iaddr):
2014-11-13 19:21:51 +03:00
iaddr = fhex(iaddr)
2014-11-23 03:25:48 +03:00
print "*** make",typ,"at",ghex(iaddr)
if typ == 'function':
program.static.analyzer.make_function_at(program.static, iaddr)
elif typ == 'code':
junk = program.static[iaddr]['instruction']
elif typ == 'data':
program.static[iaddr]['type'] = 'data'
if program.static[iaddr]['len'] == 1:
program.static[iaddr]['len'] = 2
elif program.static[iaddr]['len'] == 2:
program.static[iaddr]['len'] = 4
else:
program.static[iaddr]['len'] = 1
elif typ == 'string':
program.static[iaddr]['type'] = 'string'
# get length of string
eaddr = iaddr
try:
while program.static.memory(eaddr, 1)[0] != '\x00':
eaddr += 1
except:
pass
program.static[iaddr]['len'] = eaddr-iaddr
2014-11-23 03:43:40 +03:00
elif typ == 'undefined':
del program.static[iaddr]['len']
del program.static[iaddr]['type']
del program.static[iaddr]['instruction']
2014-11-23 03:25:48 +03:00
2014-10-12 22:32:58 +04:00