|
1 | 1 | (function () {
|
2 | 2 | const command = "decai";
|
| 3 | + let decaiHost = "http://localhost:8080"; |
| 4 | + let decaiCommands = "pdc"; |
3 | 5 | let decprompt = "optimize this pseudodisasm into high level quality decompiled code,";
|
4 | 6 | decprompt += "replace goto with proper control flow statements,";
|
5 | 7 | decprompt += "use better names for variables,";
|
|
11 | 13 | decprompt += "remove unnecessary assignments inlining them into the function argument calls, add a comment on top explaining whats the function for in one sentence";
|
12 | 14 | // simplest the better
|
13 | 15 | // decprompt = "do not explain, just improve and merge the following decompiled functions, remove gotos and use better names for variables. optimize for readability, assume calling conventions to fill function arguments";
|
14 |
| - decprompt = "do not explain, just improve and merge the following decompiled functions, remove gotos and use better names for variables, focus on readability"; |
| 16 | + decprompt = "show only the code without explanations, just improve and merge the following decompiled functions, remove gotos and use better names for variables, focus on readability"; |
15 | 17 |
|
| 18 | + function decaiEval(arg) { |
| 19 | + const [k, v] = arg.split("="); |
| 20 | + switch (k) { |
| 21 | + case "decai.cmds": |
| 22 | + decaiCommands = v; |
| 23 | + break; |
| 24 | + case "decai.prompt": |
| 25 | + decprompt = v; |
| 26 | + break; |
| 27 | + case "decai.host": |
| 28 | + decaiHost = v; |
| 29 | + break; |
| 30 | + } |
| 31 | + } |
16 | 32 | function usage() {
|
17 | 33 | console.error("Usage: " + command + " (-h) [prompt]");
|
18 | 34 | console.error(" " + command + " -d - decompile");
|
|
26 | 42 | console.error(" " + command + " -V - find vulnerabilities");
|
27 | 43 | }
|
28 | 44 | function r2ai(s) {
|
29 |
| - const host = "http://localhost:8080/cmd"; |
| 45 | + const host = decaiHost + "/cmd"; // "http://localhost:8080/cmd"; |
30 | 46 | const ss = s.replace(/ /g, "%20").replace(/'/g, "\\'");
|
31 | 47 | r2.cmd0 ('\'!curl -s "' + host + '/' + ss + '" > .pdc.txt || echo cannot curl. Is "r2ai -w" running?');
|
32 | 48 | return r2.cmd ('cat .pdc.txt');
|
|
79 | 95 | r2aidec("-d find vulnerabilities, dont show the code, only show the response");
|
80 | 96 | break;
|
81 | 97 | case "e": // "-e"
|
82 |
| - console.log("e decai.host=http://localhost:8080"); |
83 |
| - console.log("e decai.prompt=" + decprompt); |
84 |
| - // eval |
| 98 | + args = args.slice(2).trim(); |
| 99 | + if (args) { |
| 100 | + decaiEval(args); |
| 101 | + } else { |
| 102 | + console.log("e decai.host=" + decaiHost); |
| 103 | + console.log("e decai.prompt=" + decprompt); |
| 104 | + console.log("e decai.cmds=" + decaiCommands); |
| 105 | + } |
85 | 106 | break;
|
86 | 107 | case "x": // "-x"
|
87 | 108 | r2.cmd ("pdsf > /tmp/.pdc.txt");
|
|
109 | 130 | case "d": // "-d"
|
110 | 131 | try {
|
111 | 132 | args = args.slice(2).trim();
|
112 |
| - r2.cmd ("pdc > /tmp/.pdc.txt"); |
| 133 | + r2.call ("rm /tmp/.pdc.txt"); |
| 134 | + r2.call ("rm .pdc.txt"); |
| 135 | + r2.cmd ("echo > /tmp/.pdc.txt"); |
| 136 | + for (const c of decaiCommands.split(",")) { |
| 137 | + console.error("Running " + c); |
| 138 | + r2.cmd ("echo Output from " + c + ": >> /tmp/.pdc.txt"); |
| 139 | + r2.cmd ("echo BEGIN >> /tmp/.pdc.txt"); |
| 140 | + r2.cmd (c + " >> /tmp/.pdc.txt"); |
| 141 | + r2.cmd ("echo END >> /tmp/.pdc.txt"); |
| 142 | + } |
113 | 143 | r2ai("-R");
|
114 | 144 | out = r2ai("-i /tmp/.pdc.txt " + (decprompt + " " + args).trim());
|
115 | 145 | } catch (e) {
|
|
0 commit comments