Skip to content

Commit

Permalink
Refactor to ESM, Refactor Tests to Node's Test Runner (#117)
Browse files Browse the repository at this point in the history
* Update dependencies
* Update eslint config
* ESM port
* Fix for running on Apple based Macs
* Add missing ignore rules
* Improve export visibility
* Adjust documentation for ESM
* Refactor tests to use Node's builtin test runner
* Run tests with coverage
* Remove unused code
* Fix setting max iterations not applied
* Use API for setting log level
* Add tests for createNewNode
  • Loading branch information
BenBaryoPX authored Oct 15, 2024
1 parent 21e0078 commit 5e456e2
Show file tree
Hide file tree
Showing 105 changed files with 3,261 additions and 2,911 deletions.
4 changes: 0 additions & 4 deletions .eslintignore

This file was deleted.

39 changes: 0 additions & 39 deletions .eslintrc.js

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ jobs:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm install
- run: npm test
- run: npm run test:coverage
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ The basic structure of such a deobfuscator would be an array of deobfuscation mo
Unsafe modules run code through `eval` (using [isolated-vm](https://www.npmjs.com/package/isolated-vm) to be on the safe side) while safe modules do not.

```javascript
const {
safe: {normalizeComputed},
unsafe: {resolveDefiniteBinaryExpressions, resolveLocalCalls},
} = require('restringer').deobModules;
const {applyIteratively} = require('flast').utils;
import {safe, unsafe} from 'restringer';
const {normalizeComputed} = safe;
const {resolveDefiniteBinaryExpressions, resolveLocalCalls} = unsafe;
import {utils} from 'flast';
const {applyIteratively} = utils;
let script = 'obfuscated JS here';
const deobModules = [
resolveDefiniteBinaryExpressions,
Expand All @@ -103,10 +103,10 @@ console.log(script); // Deobfuscated script

With the additional `candidateFilter` function argument, it's possible to narrow down the targeted nodes:
```javascript
const {
unsafe: {resolveLocalCalls},
} = require('restringer').deobModules;
const {applyIteratively} = require('flast').utils;
import {unsafe} from 'restringer';
const {resolveLocalCalls} = unsafe;
import {utils} from 'flast';
const {applyIteratively} = utils;
let script = 'obfuscated JS here';

// It's better to define a function with a meaningful name that can show up in the log
Expand All @@ -119,8 +119,8 @@ console.log(script); // Deobfuscated script
You can also customize any deobfuscation method while still using REstringer without running the loop yourself:
```javascript
const fs = require('node:fs');
const {REstringer} = require('restringer');
import fs from 'node:fs';
import {REstringer} from 'restringer';

const inputFilename = process.argv[2];
const code = fs.readFileSync(inputFilename, 'utf-8');
Expand All @@ -145,9 +145,10 @@ if (res.script !== code) {
### Boilerplate code for starting from scratch
```javascript
const {logger, applyIteratively, treeModifier} = require('flast').utils;
import {utils} from 'flast';
const {applyIteratively, treeModifier, logger} = utils;
// Optional loading from file
// const fs = require('node:fs');
// import fs from 'node:fs';
// const inputFilename = process.argv[2] || 'target.js';
// const code = fs.readFileSync(inputFilename, 'utf-8');
const code = `(function() {
Expand Down Expand Up @@ -178,7 +179,6 @@ if (code !== script) {
## Read More
* [Processors](src/processors/README.md)
* [Tests](tests/README.md)
* [Contribution guide](CONTRIBUTING.md)
* [Obfuscation Detector](https://github.com/PerimeterX/obfuscation-detector/blob/main/README.md)
* [flAST](https://github.com/PerimeterX/flast/blob/main/README.md)
56 changes: 56 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import globals from 'globals';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import js from '@eslint/js';
import {FlatCompat} from '@eslint/eslintrc';
import babelParser from "@babel/eslint-parser";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
allConfig: js.configs.all
});

export default [
{
ignores: [
'tests/resources',
'**/jquery*.js',
'**/*tmp*.*',
'**/*tmp*/',
"eslint.config.js",
"node_modules/",
],
},
...compat.extends('eslint:recommended'),
{
languageOptions: {
parser: babelParser,
parserOptions: {
requireConfigFile: false,
babelOptions: {
plugins: ['@babel/plugin-syntax-import-assertions'],
}
},
globals: {
...globals.browser,
...globals.node,
...globals.commonjs,
},
ecmaVersion: 'latest',
sourceType: 'module',
},
rules: {
indent: ['error', 'tab', {
SwitchCase: 1,
}],
'linebreak-style': ['error', 'unix'],
quotes: ['error', 'single', {
allowTemplateLiterals: true,
}],
semi: ['error', 'always'],
'no-empty': ['off'],
},
}];
8 changes: 3 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
module.exports = {
REstringer: require(__dirname + '/src/restringer'),
deobModules: require(__dirname + '/src/modules'),
processors: require(__dirname + '/src/processors'),
};
export * from './src/restringer.js';
export * from './src/modules/index.js';
export * from './src/processors/index.js';
Loading

0 comments on commit 5e456e2

Please sign in to comment.