2014-10-12 22:27:37 +04:00
|
|
|
# eventually, this can live in a different process
|
|
|
|
# or we can break the boundary at static2
|
2014-11-07 22:14:51 +03:00
|
|
|
# these calls don't have to be included for qira to work
|
2019-03-24 00:49:07 +03:00
|
|
|
from __future__ import print_function
|
2014-10-12 22:27:37 +04:00
|
|
|
|
2019-03-24 06:14:19 +03:00
|
|
|
import sys
|
2014-11-13 08:20:40 +03: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
|
2018-07-07 01:22:08 +03:00
|
|
|
from flask_socketio import SocketIO, emit
|
2014-10-12 22:32:58 +04:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2015-08-27 11:19:35 +03:00
|
|
|
@app.route('/hasstatic', methods=["GET"])
|
|
|
|
def hasstatic():
|
|
|
|
return str(qira_config.WITH_STATIC)
|
2014-10-12 22:27:37 +04:00
|
|
|
|
|
|
|
# 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]
|
2014-11-23 12:23:58 +03:00
|
|
|
if 'function' in program.static[i] and (program.static[i]['function'].start == i):
|
|
|
|
rret['function'] = True
|
|
|
|
rret['nargs'] = program.static[i]['function'].nargs
|
2014-10-12 22:27:37 +04:00
|
|
|
rret['address'] = ghex(i)
|
|
|
|
ret.append(rret)
|
|
|
|
return json.dumps(ret)
|
|
|
|
|
2019-03-24 09:59:04 +03:00
|
|
|
@app.route('/gettagss', methods=["POST"])
|
|
|
|
def gettagss():
|
|
|
|
arr = json.loads(request.data)
|
|
|
|
ret = []
|
|
|
|
for i in arr:
|
|
|
|
i = fhex(i)
|
|
|
|
ret.append(program.static[i].todict());
|
|
|
|
return json.dumps(ret)
|
|
|
|
|
2014-10-12 22:27:37 +04:00
|
|
|
@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
|
2019-03-24 06:14:19 +03:00
|
|
|
f = open("/tmp/in.dot", "wb")
|
2014-10-12 22:48:55 +04:00
|
|
|
f.write(req)
|
|
|
|
f.close()
|
|
|
|
os.system("dot /tmp/in.dot > /tmp/out.dot")
|
2019-03-24 06:14:19 +03:00
|
|
|
ret = open("/tmp/out.dot", "rb").read()
|
2014-10-12 22:48:55 +04:00
|
|
|
#print "DOT RESPONSE", ret
|
2018-07-07 01:22:08 +03:00
|
|
|
return ret
|
2014-10-12 22:48:55 +04:00
|
|
|
|
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'])
|
|
|
|
|
2019-03-24 06:14:19 +03:00
|
|
|
def nl(dat):
|
|
|
|
if (sys.version_info > (3, 0)):
|
|
|
|
return list(map(int, dat))
|
|
|
|
else:
|
|
|
|
return map(ord, dat)
|
|
|
|
|
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)}
|
2019-03-24 06:14:19 +03:00
|
|
|
bbb['bytes'] = nl(program.static.memory(i, j))
|
2014-11-13 19:21:51 +03:00
|
|
|
ret.append(bbb)
|
|
|
|
did_append = True
|
|
|
|
break
|
|
|
|
if not did_append:
|
|
|
|
i -= 1
|
|
|
|
bbb = {'address': ghex(i)}
|
2019-03-24 06:14:19 +03:00
|
|
|
bbb['bytes'] = nl(program.static.memory(i, 1))
|
2014-11-13 19:21:51 +03:00
|
|
|
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
|
2019-03-24 06:14:19 +03:00
|
|
|
bbb['bytes'] = nl(program.static.memory(i, l))
|
2014-11-13 19:21:51 +03:00
|
|
|
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])
|
2019-03-24 06:14:19 +03:00
|
|
|
bbb['dests'] = list(map(lambda x: (ghex(x[0]), x[1]), program.static[i]['instruction'].dests()))
|
2014-11-13 19:21:51 +03:00
|
|
|
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)
|
2019-03-24 00:49:07 +03:00
|
|
|
print("*** make",typ,"at",ghex(iaddr))
|
2014-11-23 03:25:48 +03:00
|
|
|
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':
|
|
|
|
# get length of string
|
|
|
|
eaddr = iaddr
|
|
|
|
try:
|
|
|
|
while program.static.memory(eaddr, 1)[0] != '\x00':
|
|
|
|
eaddr += 1
|
|
|
|
except:
|
|
|
|
pass
|
2015-08-26 05:35:00 +03:00
|
|
|
strlen = eaddr-iaddr
|
|
|
|
if strlen == 0:
|
|
|
|
return
|
|
|
|
mstr = program.static.memory(iaddr, strlen)
|
|
|
|
mstr = ''.join(filter(str.isalnum, mstr))
|
|
|
|
|
|
|
|
program.static[iaddr]['type'] = 'string'
|
|
|
|
program.static[iaddr]['len'] = strlen
|
|
|
|
program.static[iaddr]['name'] = "a"+mstr
|
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
|
|
|
|