Update r2papi-ts 0.0.23

This commit is contained in:
pancake 2023-01-09 00:42:48 +01:00 committed by pancake
parent 443db663bb
commit db0033a4e7
4 changed files with 147 additions and 102 deletions

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2020-2022 pancake */
/* radare - LGPL - Copyright 2020-2023 pancake */
#include <r_lib.h>
#include <r_core.h>
@ -153,6 +153,10 @@ static JSValue b64(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst
}
static int r2plugin_core_call2(QjsContext *qc, RCore *core, const char *input) {
// ceprintf ("CALL2\n");
if (!qc || !qc->ctx) {
return 0;
}
JSValueConst args[1] = {
JS_NewString (qc->ctx, input)
};
@ -249,14 +253,12 @@ static JSValue r2plugin_core(JSContext *ctx, JSValueConst this_val, int argc, JS
// return JS_ThrowRangeError (ctx, "r2.plugin core already registered (only one exists)");
return JS_NewBool (ctx, false);
}
eprintf ("ADDING LE PLUGIN BECAUSE WE DDIND FIND IT THE FUNC\n");
if (Gplug >= MAXPLUGS) {
R_LOG_WARN ("Maximum number of plugins loaded! this is a limitation induced by the");
return JS_NewBool (ctx, false);
}
qc = qjsctx_add (core, nameptr, ctx, func);
ap->call = Gcalls[Gplug];
eprintf ("%s = %p\n", ap->name, qc);
GcallsData[Gplug] = qc;
Gplug++;

View File

@ -1,16 +1,16 @@
static const char *const js_r2papi_qjs = "" \
"Object.defineProperty(G,\"__esModule\",{value:!0}),G.Base64=G.N"\
"ativePointer=G.R2Papi=void 0;const shell_js_1=G;G.R2Papi=clas"\
"s{constructor(t){this.r2=t}jsonToTypescript(t,s){let e=`inter"\
"s{constructor(t){this.r2=t}jsonToTypescript(t,s){let i=`inter"\
"face ${t} {\n`;s.length&&0<s.length&&(s=s[0]);for(let t of Obj"\
"ect.keys(s))e+=` ${t}: ${typeof s[t]};\n`;return e+`}\n`}new"\
"Map(t,s,e,i,r,n=\"\"){this.cmd(`om ${t} ${s} ${e} ${i} ${r} `+n"\
"ect.keys(s))i+=` ${t}: ${typeof s[t]};\n`;return i+`}\n`}new"\
"Map(t,s,i,e,r,n=\"\"){this.cmd(`om ${t} ${s} ${i} ${e} ${r} `+n"\
")}at(t){return new NativePointer(t)}getShell(){return new she"\
"ll_js_1.R2PapiShell(this)}version(){return this.r2.cmd(\"?Vq\")"\
".trim()}platform(){return this.r2.cmd(\"uname\").trim()}arch(){"\
"return this.r2.cmd(\"uname -a\").trim()}bits(){return this.r2.c"\
"md(\"uname -b\").trim()}id(){return+this.r2.cmd(\"?vi:$p\")}print"\
"At(t,s,e){}clearScreen(){return this.r2.cmd(\"!clear\"),this}ge"\
"At(t,s,i){}clearScreen(){return this.r2.cmd(\"!clear\"),this}ge"\
"tConfig(t){return this.r2.call(\"e \"+t).trim()}setConfig(t,s){"\
"return this.r2.call(\"e \"+t+\"=\"+s),this}getRegisters(){return "\
"this.cmdj(\"drj\")}resizeFile(t){return this.cmd(\"r \"+t),this}i"\
@ -21,8 +21,8 @@ static const char *const js_r2papi_qjs = "" \
"s.cmd(\"b \"+t),this}enumerateThreads(){return[{context:this.cm"\
"dj(\"drj\"),id:0,state:\"waiting\",selected:!0}]}currentThreadId("\
"){return+this.cmd(\"e cfg.debug\")?+this.cmd(\"dpt.\"):this.id()}"\
"setRegisters(t){for(var s of Object.keys(t)){var e=t[s];this."\
"r2.cmd(\"dr \"+s+\"=\"+e)}}hex(t){return this.r2.cmd(\"?v \"+t).tri"\
"setRegisters(t){for(var s of Object.keys(t)){var i=t[s];this."\
"r2.cmd(\"dr \"+s+\"=\"+i)}}hex(t){return this.r2.cmd(\"?v \"+t).tri"\
"m()}step(){return this.r2.cmd(\"ds\"),this}stepOver(){return th"\
"is.r2.cmd(\"dso\"),this}math(t){return+this.r2.cmd(\"?v \"+t)}ste"\
"pUntil(t){this.cmd(\"dsu \"+t)}searchDisasm(t){return this.call"\
@ -116,93 +116,99 @@ static const char *const js_r2papi_qjs = "" \
"his.token=t,this.children=[]}setSides(t,s){this.lhs=t,this.rh"\
"s=s}addChildren(t,s){void 0!==t&&this.children.push(t),void 0"\
"!==s&&this.children.push(s)}toEsil(){if(void 0===this.lhs||vo"\
"id 0===this.rhs)return this.token.text;{let t=this.lhs.toEsil"\
"();return\"\"!==t&&(t+=\",\"),this.rhs.toEsil()+\",\"+t+this.token}"\
"}toString(){let t=\"\";if(\"\"!==this.token.label&&(t+=this.token"\
".label+\":\\n\"),this.token.addr,\"\"!==this.token.comment&&(t+=\"/"\
"*\"+this.token.comment+\"*/\\n\"),\"GOTO\"===this.token.toString()&"\
"&(0<this.children.length?t+=\"goto label_\"+this.children[0].to"\
"ken.position+\";\\n\":t+=`goto label_0;\n`),0<this.children.lengt"\
"h){t+=` (if (${this.rhs})\n`;for(var s of this.children)null!"\
"==s&&\"\"!=(s=s.toString())&&(t+=` ${s}\n`);t+=\" )\\n\"}return v"\
"oid 0!==this.lhs&&void 0!==this.rhs?t+` ( ${this.lhs} ${th"\
"is.token} ${this.rhs} )`:t+this.token.toString()}}G.EsilNode="\
"EsilNode;G.EsilParser=class{constructor(t){this.cur=0,this.r2"\
"=t,this.cur=0,this.stack=[],this.nodes=[],this.tokens=[],this"\
".root=new EsilNode(new EsilToken(\"function\",0),\"block\")}toJSO"\
"N(){if(0<this.stack.length)throw new Error(\"The ESIL stack is"\
" not empty\");return JSON.stringify(this.root,null,2)}toEsil()"\
"{return this.nodes.map(t=>t.toEsil()).join(\",\")}optimizeFlags"\
"(t){void 0!==t.rhs&&this.optimizeFlags(t.rhs),void 0!==t.lhs&"\
"&this.optimizeFlags(t.lhs);for(let s=0;s<t.children.length;s+"\
"+)this.optimizeFlags(t.children[s]);var s=t.toString();4096<+"\
"s&&\"\"!=(s=r2.cmd(\"fd.@ \"+s).trim().split(\"\\n\")[0].trim())&&-1"\
"===s.indexOf(\"+\")&&(t.token.text=s)}optimize(t){-1!=t.indexOf"\
"(\"flag\")&&this.optimizeFlags(this.root)}toString(){return thi"\
"s.root.children.map(t=>t.toString()).join(\";\\n\")}reset(){this"\
".nodes=[],this.stack=[],this.tokens=[],this.cur=0,this.root=n"\
"ew EsilNode(new EsilToken(\"function\",0),\"block\")}parseRange(t"\
",s){let e=t;for(;e<this.tokens.length&&e<s;){const t=this.pee"\
"k(e);if(!t)break;this.cur=e,this.pushToken(t),e=this.cur,e++}"\
"}parse(t,s){const e=t.trim().split(\",\").map(t=>t.trim()),i=th"\
"is.tokens.length;for(let t of e){const e=new EsilToken(t,this"\
".tokens.length);void 0!==s&&(e.addr=s),this.tokens.push(e)}t="\
"this.tokens.length;this.parseRange(i,t)}peek(t){return this.t"\
"okens[t]}pushToken(t){if(this.isNumber(t)){var s=new EsilNode"\
"(t,\"number\");this.stack.push(s),this.nodes.push(s)}else if(th"\
"is.isInternal(t)){const s=new EsilNode(t,\"flag\");this.stack.p"\
"ush(s),this.nodes.push(s)}else if(!this.isOperation(t)){const"\
" s=new EsilNode(t,\"register\");this.stack.push(s),this.nodes.p"\
"ush(s)}}isNumber(t){return!!t.toString().startsWith(\"0\")||0<+"\
"t}isInternal(t){t=t.toString();return t.startsWith(\"$\")&&1<t."\
"length}parseUntil(t){t+=1;let e=t;const i=[],r=this.nodes.len"\
"gth;for(this.stack.forEach(t=>i.push(t));e<this.tokens.length"\
";){const t=this.peek(e);if(!t)break;if(\"}\"===t.toString())bre"\
"ak;if(\"}{\"===t.toString())break;e++}this.stack=i;var n=e;retu"\
"rn this.parseRange(t,n),this.nodes.length==r?null:this.nodes["\
"this.nodes.length-1]}getNodeFor(t){if(void 0!==this.peek(t)){"\
"for(var s of this.nodes)if(s.token.position===t)return s;this"\
".nodes.push(new EsilNode(new EsilToken(\"label\",t),\"label\"))}r"\
"eturn null}findNodeFor(t){for(var s of this.nodes)if(s.token."\
"position===t)return s;return null}isOperation(t){switch(t.toS"\
"tring()){case\"[1]\":case\"[2]\":case\"[4]\":case\"[8]\":if(!(1<=this"\
".stack.length))throw new Error(\"Stack needs more items\");{con"\
"st t=this.stack.pop();new EsilNode(t.token,\"operation\"),this."\
"stack.push(t)}return!0;case\"!\":var s,e,i;if(1<=this.stack.len"\
"gth)return s=new EsilNode(new EsilToken(\"\",t.position),\"none\""\
"),e=this.stack.pop(),(i=new EsilNode(t,\"operation\")).setSides"\
"(s,e),this.stack.push(i),!0;throw new Error(\"Stack needs more"\
" items\");case\"\":case\"}\":case\"}{\":return!0;case\"DUP\":{if(this."\
"stack.length<1)throw new Error(\"goto cant pop\");const t=this."\
"stack.pop();this.stack.push(t),this.stack.push(t)}return!0;ca"\
"se\"GOTO\":if(null!==this.peek(t.position-1)){if(this.stack.len"\
"gth<1)throw new Error(\"goto cant pop\");const s=this.stack.pop"\
"();if(null!==s){const e=0|+s.toString();if(0<e){const s=this."\
"peek(e);if(void 0!==s){s.label=\"label_\"+e,s.comment=\"hehe\";co"\
"nst i=new EsilNode(t,\"goto\"),r=this.getNodeFor(s.position);nu"\
"ll!=r&&i.children.push(r),this.root.children.push(i)}else con"\
"sole.error(\"Cannot find goto node\")}else console.error(\"Canno"\
"t find dest node for goto\")}}return!0;case\"?{\":if(!(1<=this.s"\
"tack.length))throw new Error(\"Stack needs more items\");{const"\
" s=new EsilNode(new EsilToken(\"if\",t.position),\"none\"),e=this"\
".stack.pop(),i=new EsilNode(t,\"operation\");i.setSides(s,e);le"\
"t r=this.parseUntil(t.position),n=null;null!==r&&(i.children."\
"push(r),this.nodes.push(r),null!==(n=this.parseUntil(r.token."\
"position+1)))&&(i.children.push(n),this.nodes.push(n)),this.n"\
"odes.push(i),this.root.children.push(i),null!==n&&(this.cur=n"\
".token.position)}return!0;case\"-\":if(!(2<=this.stack.length))"\
"throw new Error(\"Stack needs more items\");{const s=this.stack"\
".pop(),e=this.stack.pop(),i=new EsilNode(t,\"operation\");i.set"\
"Sides(s,e),this.stack.length,this.stack.push(i),this.nodes.pu"\
"sh(i)}return!0;case\"<\":case\">\":case\"^\":case\"&\":case\"|\":case\"+"\
"\":case\"*\":case\"/\":case\">>=\":case\"<<=\":case\">>>=\":case\"<<<=\":c"\
"ase\">>>>=\":case\"<<<<=\":if(!(2<=this.stack.length))throw new E"\
"rror(\"Stack needs more items\");{const s=this.stack.pop(),e=th"\
"is.stack.pop(),i=new EsilNode(t,\"operation\");i.setSides(s,e),"\
"this.stack.length,this.stack.push(i),this.nodes.push(i)}retur"\
"n!0;case\"=\":case\":=\":case\"-=\":case\"+=\":case\"==\":case\"=[1]\":ca"\
"se\"=[2]\":case\"=[4]\":case\"=[8]\":if(!(2<=this.stack.length))thr"\
"ow new Error(\"Stack needs more items\");{const s=this.stack.po"\
"p(),e=this.stack.pop(),i=new EsilNode(t,\"operation\");i.setSid"\
"es(s,e),0===this.stack.length&&this.root.children.push(i),thi"\
"s.nodes.push(i)}return!0}return!1}};\n";
"id 0===this.rhs)return\"\";{let t=this.lhs.toEsil();return\"\"!=="\
"t&&(t+=\",\"),this.rhs.toEsil()+\",\"+t+this.token}}toString(){le"\
"t t=\"\";if(\"\"!==this.token.label&&(t+=this.token.label+\":\\n\"),"\
"this.token.addr,\"\"!==this.token.comment&&(t+=\"/*\"+this.token."\
"comment+\"*/\\n\"),\"GOTO\"===this.token.toString()&&(0<this.child"\
"ren.length?t+=\"goto label_\"+this.children[0].token.position+\""\
";\\n\":t+=`goto label_0;\n`),0<this.children.length){t+=` (if ("\
"${this.rhs})\n`;for(var s of this.children)null!==s&&\"\"!=(s=s."\
"toString())&&(t+=` ${s}\n`);t+=\" )\\n\"}return void 0!==this.l"\
"hs&&void 0!==this.rhs?t+` ( ${this.lhs} ${this.token} ${th"\
"is.rhs} )`:t+this.token.toString()}}G.EsilNode=EsilNode;G.Esi"\
"lParser=class{constructor(t){this.cur=0,this.r2=t,this.cur=0,"\
"this.stack=[],this.nodes=[],this.tokens=[],this.root=new Esil"\
"Node(new EsilToken(\"function\",0),\"block\")}toJSON(){if(0<this."\
"stack.length)throw new Error(\"The ESIL stack is not empty\");r"\
"eturn JSON.stringify(this.root,null,2)}toEsil(){return this.n"\
"odes.map(t=>t.toEsil()).join(\",\")}optimizeFlags(t){void 0!==t"\
".rhs&&this.optimizeFlags(t.rhs),void 0!==t.lhs&&this.optimize"\
"Flags(t.lhs);for(let s=0;s<t.children.length;s++)this.optimiz"\
"eFlags(t.children[s]);var s=t.toString();4096<+s&&\"\"!=(s=r2.c"\
"md(\"fd.@ \"+s).trim().split(\"\\n\")[0].trim())&&-1===s.indexOf(\""\
"+\")&&(t.token.text=s)}optimize(t){-1!=t.indexOf(\"flag\")&&this"\
".optimizeFlags(this.root)}toString(){return this.root.childre"\
"n.map(t=>t.toString()).join(\";\\n\")}reset(){this.nodes=[],this"\
".stack=[],this.tokens=[],this.cur=0,this.root=new EsilNode(ne"\
"w EsilToken(\"function\",0),\"block\")}parseRange(t,s){let i=t;fo"\
"r(;i<this.tokens.length&&i<s;){const t=this.peek(i);if(!t)bre"\
"ak;this.cur=i,this.pushToken(t),i=this.cur,i++}}parseFunction"\
"(t){var s=this;var e=r2.cmd(\"?v $$\").trim(),r=(void 0===t&&(t"\
"=e),r2.cmdj(\"afbj@\"+t));for(let t of r)r2.cmd(\"s \"+t.addr),fu"\
"nction(t){const i=r2.cmd(\"pie \"+t+\" @e:scr.color=0\").trim().s"\
"plit(\"\\n\");for(const t of i)if(0===t.length)console.log(\"Empt"\
"y\");else{const i=t.split(\" \");1<i.length&&(r2.cmd(\"s \"+i[0]),"\
"s.parse(i[1],i[0]),s.optimize(\"flags,labels\"))}}(t.ninstr);r2"\
".cmd(\"s \"+e)}parse(t,s){const i=t.trim().split(\",\").map(t=>t."\
"trim()),e=this.tokens.length;for(let t of i){const i=new Esil"\
"Token(t,this.tokens.length);void 0!==s&&(i.addr=s),this.token"\
"s.push(i)}t=this.tokens.length;this.parseRange(e,t)}peek(t){r"\
"eturn this.tokens[t]}pushToken(t){if(this.isNumber(t)){var s="\
"new EsilNode(t,\"number\");this.stack.push(s),this.nodes.push(s"\
")}else if(this.isInternal(t)){const s=new EsilNode(t,\"flag\");"\
"this.stack.push(s),this.nodes.push(s)}else if(!this.isOperati"\
"on(t)){const s=new EsilNode(t,\"register\");this.stack.push(s),"\
"this.nodes.push(s)}}isNumber(t){return!!t.toString().startsWi"\
"th(\"0\")||0<+t}isInternal(t){t=t.toString();return t.startsWit"\
"h(\"$\")&&1<t.length}parseUntil(t){t+=1;let i=t;const e=[],r=th"\
"is.nodes.length;for(this.stack.forEach(t=>e.push(t));i<this.t"\
"okens.length;){const t=this.peek(i);if(!t)break;if(\"}\"===t.to"\
"String())break;if(\"}{\"===t.toString())break;i++}this.stack=e;"\
"var n=i;return this.parseRange(t,n),this.nodes.length==r?null"\
":this.nodes[this.nodes.length-1]}getNodeFor(t){if(void 0!==th"\
"is.peek(t)){for(var s of this.nodes)if(s.token.position===t)r"\
"eturn s;this.nodes.push(new EsilNode(new EsilToken(\"label\",t)"\
",\"label\"))}return null}findNodeFor(t){for(var s of this.nodes"\
")if(s.token.position===t)return s;return null}isOperation(t){"\
"switch(t.toString()){case\"[1]\":case\"[2]\":case\"[4]\":case\"[8]\":"\
"if(!(1<=this.stack.length))throw new Error(\"Stack needs more "\
"items\");{const t=this.stack.pop();new EsilNode(t.token,\"opera"\
"tion\"),this.stack.push(t)}return!0;case\"!\":var s,i,e;if(1<=th"\
"is.stack.length)return s=new EsilNode(new EsilToken(\"\",t.posi"\
"tion),\"none\"),i=this.stack.pop(),(e=new EsilNode(t,\"operation"\
"\")).setSides(s,i),this.stack.push(e),!0;throw new Error(\"Stac"\
"k needs more items\");case\"\":case\"}\":case\"}{\":return!0;case\"DU"\
"P\":{if(this.stack.length<1)throw new Error(\"goto cant pop\");c"\
"onst t=this.stack.pop();this.stack.push(t),this.stack.push(t)"\
"}return!0;case\"GOTO\":if(null!==this.peek(t.position-1)){if(th"\
"is.stack.length<1)throw new Error(\"goto cant pop\");const s=th"\
"is.stack.pop();if(null!==s){const i=0|+s.toString();if(0<i){c"\
"onst s=this.peek(i);if(void 0!==s){s.label=\"label_\"+i,s.comme"\
"nt=\"hehe\";const e=new EsilNode(t,\"goto\"),r=this.getNodeFor(s."\
"position);null!=r&&e.children.push(r),this.root.children.push"\
"(e)}else console.error(\"Cannot find goto node\")}else console."\
"error(\"Cannot find dest node for goto\")}}return!0;case\"?{\":if"\
"(!(1<=this.stack.length))throw new Error(\"Stack needs more it"\
"ems\");{const s=new EsilNode(new EsilToken(\"if\",t.position),\"n"\
"one\"),i=this.stack.pop(),e=new EsilNode(t,\"operation\");e.setS"\
"ides(s,i);let r=this.parseUntil(t.position),n=null;null!==r&&"\
"(e.children.push(r),this.nodes.push(r),null!==(n=this.parseUn"\
"til(r.token.position+1)))&&(e.children.push(n),this.nodes.pus"\
"h(n)),this.nodes.push(e),this.root.children.push(e),null!==n&"\
"&(this.cur=n.token.position)}return!0;case\"-\":if(!(2<=this.st"\
"ack.length))throw new Error(\"Stack needs more items\");{const "\
"s=this.stack.pop(),i=this.stack.pop(),e=new EsilNode(t,\"opera"\
"tion\");e.setSides(s,i),this.stack.length,this.stack.push(e),t"\
"his.nodes.push(e)}return!0;case\"<\":case\">\":case\"^\":case\"&\":ca"\
"se\"|\":case\"+\":case\"*\":case\"/\":case\">>=\":case\"<<=\":case\">>>=\":"\
"case\"<<<=\":case\">>>>=\":case\"<<<<=\":if(!(2<=this.stack.length)"\
")throw new Error(\"Stack needs more items\");{const s=this.stac"\
"k.pop(),i=this.stack.pop(),e=new EsilNode(t,\"operation\");e.se"\
"tSides(s,i),this.stack.length,this.stack.push(e),this.nodes.p"\
"ush(e)}return!0;case\"=\":case\":=\":case\"-=\":case\"+=\":case\"==\":c"\
"ase\"=[1]\":case\"=[2]\":case\"=[4]\":case\"=[8]\":if(!(2<=this.stack"\
".length))throw new Error(\"Stack needs more items\");{const s=t"\
"his.stack.pop(),i=this.stack.pop(),e=new EsilNode(t,\"operatio"\
"n\");e.setSides(s,i),0===this.stack.length&&this.root.children"\
".push(e),this.nodes.push(e)}return!0}return!1}};\n";

View File

@ -576,9 +576,10 @@ class EsilNode {
if (left !== "") {
left += ",";
}
return `${this.rhs.toEsil()},${left}${this.token}`;
let right = this.rhs.toEsil();
return `${right},${left}${this.token}`;
}
return this.token.text;
return ''; // this.token.text;
}
toString() {
let str = "";
@ -695,6 +696,42 @@ class EsilParser {
}
// console.log("done");
}
parseFunction(addr) {
var ep = this;
function parseAmount(n) {
// console.log("PDQ "+n);
const lines = r2.cmd("pie " + n + " @e:scr.color=0").trim().split("\n");
for (const line of lines) {
if (line.length === 0) {
console.log("Empty");
continue;
}
// console.log("parse", r2.cmd("?v:$$"));
const kv = line.split(' ');
if (kv.length > 1) { // line != "") {
// console.log("// @ " + kv[0]);
//ep.reset ();
r2.cmd(`s ${kv[0]}`);
ep.parse(kv[1], kv[0]);
ep.optimize("flags,labels");
//console.log(ep.toString());
}
}
// console.log(ep.toString());
}
const oaddr = r2.cmd("?v $$").trim();
// const func = r2.cmdj("pdrj"); // XXX this command changes the current seek
if (addr === undefined) {
addr = oaddr;
}
const bbs = r2.cmdj(`afbj@${addr}`); // XXX this command changes the current seek
for (let bb of bbs) {
// console.log("bb_" + bb.addr + ":");
r2.cmd(`s ${bb.addr}`);
parseAmount(bb.ninstr);
}
r2.cmd(`s ${oaddr}`);
}
parse(expr, addr) {
const tokens = expr.trim().split(',').map((x) => x.trim());
const from = this.tokens.length;

View File

@ -1,5 +1,5 @@
{
"dependencies": {
"r2papi": "^0.0.22"
"r2papi": "^0.0.23"
}
}