-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: evaluate
atob
, unescape
, decodeURI
and `decodeURIComponen…
…t` calls (#53)
- Loading branch information
Showing
3 changed files
with
69 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import * as t from '@babel/types'; | ||
import * as m from '@codemod/matchers'; | ||
import type { Transform } from '../ast-utils'; | ||
|
||
const FUNCTIONS = { | ||
atob, | ||
unescape, | ||
decodeURI, | ||
decodeURIComponent, | ||
}; | ||
|
||
export default { | ||
name: 'evaluate-globals', | ||
tags: ['safe'], | ||
scope: true, | ||
visitor() { | ||
const name = m.capture( | ||
m.or(...(Object.keys(FUNCTIONS) as (keyof typeof FUNCTIONS)[])), | ||
); | ||
const arg = m.capture(m.anyString()); | ||
const matcher = m.callExpression(m.identifier(name), [ | ||
m.stringLiteral(arg), | ||
]); | ||
|
||
return { | ||
CallExpression: { | ||
exit(path) { | ||
if (!matcher.match(path.node)) return; | ||
if (path.scope.hasBinding(name.current!, { noGlobals: true })) return; | ||
|
||
try { | ||
// Causes a "TypeError: Illegal invocation" without the globalThis receiver | ||
const value = FUNCTIONS[name.current!].call( | ||
globalThis, | ||
arg.current!, | ||
); | ||
path.replaceWith(t.stringLiteral(value)); | ||
this.changes++; | ||
} catch { | ||
// ignore | ||
} | ||
}, | ||
}, | ||
}; | ||
}, | ||
} satisfies Transform; |
20 changes: 20 additions & 0 deletions
20
packages/webcrack/src/deobfuscate/test/evaluate-globals.test.ts
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,20 @@ | ||
import { test } from 'vitest'; | ||
import { testTransform } from '../../../test/index'; | ||
import evaluateGlobals from '../evaluate-globals'; | ||
|
||
const expectJS = testTransform(evaluateGlobals); | ||
|
||
test('atob', () => | ||
expectJS('atob("aGVsbG8=")').toMatchInlineSnapshot('"hello";')); | ||
|
||
test('atob that throws', () => | ||
expectJS('atob("-")').toMatchInlineSnapshot(`atob("-");`)); | ||
|
||
test('unescape', () => | ||
expectJS('unescape("%41")').toMatchInlineSnapshot('"A";')); | ||
|
||
test('decodeURI', () => | ||
expectJS('decodeURI("%41")').toMatchInlineSnapshot('"A";')); | ||
|
||
test('decodeURIComponent', () => | ||
expectJS('decodeURIComponent("%41")').toMatchInlineSnapshot('"A";')); |
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