-
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
class Deobfuscator { | ||
#mapping; | ||
#reverseMapping; | ||
|
||
constructor (mapping) { | ||
if (mapping instanceof Map) { | ||
this.#mapping = mapping; | ||
} else { | ||
const o = Object.entries(mapping).map(e => [e[0], { | ||
realSpecifier: e[1].realSpecifier, | ||
properties: new Map(Object.entries(e[1].properties)), | ||
}]); | ||
this.#mapping = new Map(o); | ||
} | ||
|
||
this.#reverseMapping = new Map(); | ||
for (const [key, value] of this.#mapping.entries()) { | ||
this.#reverseMapping.set(value.realSpecifier, key); | ||
} | ||
} | ||
|
||
#getRealSpecifier (deobfuscatedSpecifier) { | ||
const specifierMapping = this.#mapping.get(deobfuscatedSpecifier); | ||
return specifierMapping?.realSpecifier; | ||
} | ||
|
||
use (deobfuscatedSpecifier) { | ||
const realSpecifier = this.#getRealSpecifier(deobfuscatedSpecifier) || deobfuscatedSpecifier; | ||
let realUse; | ||
Java.performNow(() => { | ||
realUse = Java.use(realSpecifier); | ||
}); | ||
return this.wrap(realUse); | ||
} | ||
|
||
choose (deobfuscatedSpecifier, callbacks) { | ||
const realSpecifier = this.#getRealSpecifier(deobfuscatedSpecifier) || deobfuscatedSpecifier; | ||
Java.perform(() => { | ||
Java.choose(realSpecifier, { | ||
onMatch: instance => callbacks.onMatch(this.wrap(instance)), | ||
onComplete: () => callbacks.onComplete(), | ||
}); | ||
}); | ||
} | ||
|
||
wrap (wrapper) { | ||
const realSpecifier = wrapper.$className; | ||
const deobfuscatedSpecifier = this.#reverseMapping.get(realSpecifier) || realSpecifier; | ||
const propertiesMapping = this.#mapping.get(deobfuscatedSpecifier)?.properties || new Map(); | ||
|
||
return new Proxy(wrapper, { | ||
get: (target, prop, receiver) => { | ||
const realProp = propertiesMapping.get(prop) || prop; | ||
const result = Reflect.get(target, realProp, receiver); | ||
|
||
if (result) { | ||
if (result.value && result.value.$className) { | ||
return this.#wrapField(result); | ||
} | ||
|
||
if (result instanceof Function) { | ||
return this.#wrapMethod(target, result); | ||
} | ||
} | ||
|
||
return result; | ||
}, | ||
}); | ||
} | ||
|
||
#wrapField (field) { | ||
return new Proxy(field, { | ||
get: (target, prop, receiver) => { | ||
if (prop === "value") { | ||
return this.wrap(target.value); | ||
} | ||
return Reflect.get(target, prop, receiver); | ||
} | ||
}); | ||
} | ||
|
||
#wrapMethod (wrapper, method) { | ||
return new Proxy(method, { | ||
apply: (_, thisArg, argumentsList) => { | ||
const result = method.apply(thisArg, argumentsList); | ||
if (result && result.$className) { | ||
return this.wrap(result); | ||
} | ||
return result; | ||
}, | ||
set: (target, prop, newValue, receiver) => { | ||
if (prop === "implementation") { | ||
return Reflect.set(target, prop, newValue.bind(wrapper), receiver); | ||
} | ||
|
||
return Reflect.set(target, prop, newValue, receiver); | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
|
||
module.exports = Deobfuscator; |