Comments (2)
Have implemented this in a similar approach in 3bb12a3
and have added removing dead branches as an option to the online version.
Your code was pretty spot on, especially for someone who doesn't know TypeScript, thanks for contributing :)
from javascript-deobfuscator.
Ok so i've made this:
git diff
diff --git a/src/helpers/traversalHelper.ts b/src/helpers/traversalHelper.ts
index 135dc29..a441744 100644
--- a/src/helpers/traversalHelper.ts
+++ b/src/helpers/traversalHelper.ts
@@ -9,7 +9,7 @@ export default class TraversalHelper {
* @param node The node to replace.
* @param replacement The replacement node.
*/
- static replaceNode(root: Shift.Node, node: Shift.Node, replacement: Shift.Node | null): void {
+ static replaceNode(root: Shift.Node, node: Shift.Node, replacement: Shift.Node | null | Array<Shift.Node>): void {
traverse(root, {
enter(n: Shift.Node, parent: Shift.Node) {
if (n == node) {
@@ -22,7 +22,11 @@ export default class TraversalHelper {
const index = array.indexOf(node);
if (index != -1) {
if (replacement) {
- array[index] = replacement;
+ if (Array.isArray(replacement)){
+ array.splice(index, 1, ...replacement);
+ } else {
+ array[index] = replacement;
+ }
} else {
array.splice(index, 1);
}
diff --git a/src/index.ts b/src/index.ts
index c4fbf64..a0d4ca7 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -10,6 +10,7 @@ import CleanupHelper from './helpers/cleanupHelper';
import Config from './config';
import VariableRenamer from './modifications/renaming/variableRenamer';
import FunctionExecutor from './modifications/execution/functionExecutor';
+import BlockRemover from './modifications/blockRemover';
export function deobfuscate(source: string, config: Config): string {
const ast = parseScript(source) as Shift.Script;
@@ -42,6 +43,8 @@ export function deobfuscate(source: string, config: Config): string {
if (config.miscellaneous.renameHexIdentifiers) {
modifications.push(new VariableRenamer(ast));
}
+ modifications.push(new BlockRemover(ast));
+
for (const modification of modifications) {
console.log(`[${(new Date()).toISOString()}]: Executing ${modification.constructor.name}`);
diff --git a/src/modifications/blockRemover.ts b/src/modifications/blockRemover.ts
new file mode 100644
index 0000000..29dd7d4
--- /dev/null
+++ b/src/modifications/blockRemover.ts
@@ -0,0 +1,64 @@
+import Modification from "../modification";
+import * as Shift from 'shift-ast';
+import isValid from 'shift-validator';
+import { traverse } from '../helpers/traverse';
+import TraversalHelper from "../helpers/traversalHelper";
+
+export default class BlockRemover extends Modification {
+ /**
+ * Creates a new modification.
+ * @param ast The AST.
+ */
+ constructor(ast: Shift.Script) {
+ super('Remove false blocks', ast);
+ }
+
+ /**
+ * Executes the modification.
+ */
+ execute(): void {
+ this.removeBlocks(this.ast);
+ }
+
+
+ private removeBlocks(node: Shift.Node): void {
+ const self = this;
+
+ traverse(node, {
+ enter(node: Shift.Node, parent: Shift.Node) {
+ if (node.type != "IfStatement" && node.type != "ConditionalExpression"){
+ return;
+ }
+ if ((node as any).test.type != "LiteralBooleanExpression"){
+ return;
+ }
+
+ let replacement = self.getTrueBranch(node);
+ if (replacement){
+ self.removeBlocks(replacement);
+ TraversalHelper.replaceNode(parent, node, self.extractStatements(replacement));
+ } else {
+ TraversalHelper.replaceNode(parent, node, replacement);
+ }
+ }
+ })
+ }
+ private getTrueBranch(node: Shift.IfStatement | Shift.ConditionalExpression): Shift.Node | null {
+ let branch;
+ if ((node.test as Shift.LiteralBooleanExpression).value) {
+ return node.consequent;
+ } else {
+ return node.alternate;
+ }
+ return null;
+ }
+
+ private extractStatements(node: Shift.Node): Shift.Node | Array<Shift.Node> {
+ switch (node.type) {
+ case 'BlockStatement':
+ return node.block.statements;
+ default:
+ return node;
+ }
+ }
+}
it's my first time coding typescript and second time seeing it at all.
it seems to work fine but it needs a couple of runs
sample code:
if (true) {
console.log("hello there")
true ? console.log(1) : console.log(2)
if (true) {
if (false) {
console.log(44)
}
console.log(3)
} else {
console.log(6)
}
} else {
console.log(2+2)
console["log"](2+2)
}
console.log(99)
deobfuscated:
console.log("hello there");
console.log(1);
console.log(3);
console.log(99);
from javascript-deobfuscator.
Related Issues (20)
- Cannot deobfuscate private fields HOT 2
- Replace proxy functions breaks on function that returns string literal HOT 1
- Deobfuscate array shifting technique efficiently HOT 1
- Function executor does not deal with array of literals and deletes code even if no replacement is found HOT 1
- Does not respond to the script.
- npm ERR! could not determine executable to run HOT 6
- Handle arithmetic double negation HOT 6
- [Bug] Simplify Expression: Fails when variables are present
- [Feature Request] Unpack Objects
- [Bug] Handle separated declaration and assignment
- [Bug] Mishandling of scope HOT 1
- [Feature Request] Remove intermediary variables (or constants)
- [Bug] Proxy function not detected due to variable declaration.
- Comma Expression: different results
- js-deobfuscator: command not found HOT 4
- Unexpected token "{" HOT 4
- Unable to Load
- unnecessary freezing after pasting large code into textarea HOT 3
- DIscord invite down
- npm missing version 1.0.20 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from javascript-deobfuscator.