@nodesecure/js-x-ray 4.1.2 → 4.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +148 -147
- package/index.d.ts +12 -5
- package/index.js +7 -1
- package/package.json +10 -10
- package/src/Analysis.js +7 -1
- package/src/constants.js +47 -28
- package/src/obfuscators/index.js +70 -65
- package/src/obfuscators/trojan-source.js +11 -0
package/README.md
CHANGED
|
@@ -1,147 +1,148 @@
|
|
|
1
|
-
# js-x-ray
|
|
2
|
-

|
|
3
|
-
[](https://github.com/NodeSecure/js-x-ray/commit-activity)
|
|
4
|
-
[](https://github.com/nodejs/security-wg/blob/master/processes/responsible_disclosure_template.md
|
|
5
|
-
)
|
|
6
|
-
[](https://github.com/NodeSecure/js-x-ray/blob/master/LICENSE)
|
|
7
|
-

|
|
8
|
-
|
|
9
|
-
JavaScript AST analysis. This package has been created to export the [Node-Secure](https://github.com/ES-Community/nsecure) AST Analysis to enable better code evolution and allow better access to developers and researchers.
|
|
10
|
-
|
|
11
|
-
The goal is to quickly identify dangerous code and patterns for developers and Security researchers. Interpreting the results of this tool will still require you to have a set of security notions.
|
|
12
|
-
|
|
13
|
-
> 💖 I have no particular background in security. I'm simply becoming more and more interested and passionate about static code analysis. But I would be more than happy to learn that my work can help prevent potential future attacks (or leaks).
|
|
14
|
-
|
|
15
|
-
## Goals
|
|
16
|
-
The objective of the project is to successfully detect all potentially suspicious JavaScript codes.. The target is obviously codes that are added or injected for malicious purposes..
|
|
17
|
-
|
|
18
|
-
Most of the time these hackers will try to hide the behaviour of their codes as much as possible to avoid being spotted or easily understood... The work of the library is to understand and analyze these patterns that will allow us to detect malicious code..
|
|
19
|
-
|
|
20
|
-
## Features Highlight
|
|
21
|
-
- Retrieve required dependencies and files for Node.js.
|
|
22
|
-
- Detect unsafe RegEx.
|
|
23
|
-
- Get warnings when the AST Analysis as a problem or when not able to follow a statement.
|
|
24
|
-
- Highlight common attack patterns and API usages.
|
|
25
|
-
- Capable to follow the usage of dangerous Node.js globals.
|
|
26
|
-
- Detect obfuscated code and when possible the tool that has been used.
|
|
27
|
-
|
|
28
|
-
## Getting Started
|
|
29
|
-
|
|
30
|
-
This package is available in the Node Package Repository and can be easily installed with [npm](https://docs.npmjs.com/getting-started/what-is-npm) or [yarn](https://yarnpkg.com).
|
|
31
|
-
|
|
32
|
-
```bash
|
|
33
|
-
$ npm i @nodesecure/js-x-ray
|
|
34
|
-
# or
|
|
35
|
-
$ yarn add @nodesecure/js-x-ray
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
## Usage example
|
|
39
|
-
|
|
40
|
-
Create a local `.js` file with the following content:
|
|
41
|
-
```js
|
|
42
|
-
try {
|
|
43
|
-
require("http");
|
|
44
|
-
}
|
|
45
|
-
catch (err) {
|
|
46
|
-
// do nothing
|
|
47
|
-
}
|
|
48
|
-
const lib = "crypto";
|
|
49
|
-
require(lib);
|
|
50
|
-
require("util");
|
|
51
|
-
require(Buffer.from("6673", "hex").toString());
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
---
|
|
55
|
-
|
|
56
|
-
Then use `js-x-ray` to run an analysis of the JavaScript code:
|
|
57
|
-
```js
|
|
58
|
-
import { runASTAnalysis } from "@nodesecure/js-x-ray";
|
|
59
|
-
import { readFileSync } from "fs";
|
|
60
|
-
|
|
61
|
-
const str = readFileSync("./file.js", "utf-8");
|
|
62
|
-
const { warnings, dependencies } = runASTAnalysis(str);
|
|
63
|
-
|
|
64
|
-
const dependenciesName = [...dependencies];
|
|
65
|
-
const inTryDeps = [...dependencies.getDependenciesInTryStatement()];
|
|
66
|
-
|
|
67
|
-
console.log(dependenciesName);
|
|
68
|
-
console.log(inTryDeps);
|
|
69
|
-
console.log(warnings);
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
The analysis will return: `http` (in try), `crypto`, `util` and `fs`.
|
|
73
|
-
|
|
74
|
-
> ⚠️ There is also a lot of suspicious code example in the root cases directory. Feel free to try the tool on these files.
|
|
75
|
-
|
|
76
|
-
## Warnings Legends (v2.0+)
|
|
77
|
-
|
|
78
|
-
> Node-secure versions equal or lower than 0.7.0 are no longer compatible with the warnings table below.
|
|
79
|
-
|
|
80
|
-
This section describe all the possible warnings returned by JSXRay.
|
|
81
|
-
|
|
82
|
-
| name | description |
|
|
83
|
-
| --- | --- |
|
|
84
|
-
| parsing-error | An error occured when parsing the JavaScript code with meriyah. It mean that the conversion from string to AST as failed. If you encounter such an error, **please open an issue here**. |
|
|
85
|
-
| unsafe-import | Unable to follow an import (require, require.resolve) statement/expr. |
|
|
86
|
-
| unsafe-regex | A RegEx as been detected as unsafe and may be used for a ReDoS Attack. |
|
|
87
|
-
| unsafe-stmt | Usage of dangerous statement like `eval()` or `Function("")`. |
|
|
88
|
-
| unsafe-assign | Assignment of a protected global like `process` or `require`. |
|
|
89
|
-
| encoded-literal | An encoded literal has been detected (it can be an hexa value, unicode sequence, base64 string etc) |
|
|
90
|
-
| short-identifiers | This mean that all identifiers has an average length below 1.5. Only possible if the file contains more than 5 identifiers. |
|
|
91
|
-
| suspicious-literal | This mean that the sum of suspicious score of all Literals is bigger than 3. |
|
|
92
|
-
| obfuscated-code (**experimental**) | There's a very high probability that the code is obfuscated... |
|
|
93
|
-
|
|
94
|
-
> 👀 More details on warnings and their implementations [here](./WARNINGS.md)
|
|
95
|
-
|
|
96
|
-
## API
|
|
97
|
-
|
|
98
|
-
<details>
|
|
99
|
-
<summary>runASTAnalysis(str: string, options?: RuntimeOptions): Report</summary>
|
|
100
|
-
|
|
101
|
-
```ts
|
|
102
|
-
interface RuntimeOptions {
|
|
103
|
-
module?: boolean;
|
|
104
|
-
isMinified?: boolean;
|
|
105
|
-
}
|
|
106
|
-
```
|
|
107
|
-
|
|
108
|
-
The method take a first argument which is the code you want to analyse. It will return a Report Object:
|
|
109
|
-
|
|
110
|
-
```ts
|
|
111
|
-
interface Report {
|
|
112
|
-
dependencies: ASTDeps;
|
|
113
|
-
warnings: Warning<BaseWarning>[];
|
|
114
|
-
idsLengthAvg: number;
|
|
115
|
-
stringScore: number;
|
|
116
|
-
isOneLineRequire: boolean;
|
|
117
|
-
}
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
</details>
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
## Contributors ✨
|
|
124
|
-
|
|
125
|
-
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
|
|
126
|
-
[):
|
|
130
|
-
|
|
131
|
-
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
|
|
132
|
-
<!-- prettier-ignore-start -->
|
|
133
|
-
<!-- markdownlint-disable -->
|
|
134
|
-
<table>
|
|
135
|
-
<tr>
|
|
136
|
-
<td align="center"><a href="https://www.linkedin.com/in/thomas-gentilhomme/"><img src="https://avatars.githubusercontent.com/u/4438263?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Gentilhomme</b></sub></a><br /><a href="https://github.com/NodeSecure/js-x-ray/commits?author=fraxken" title="Code">💻</a> <a href="https://github.com/NodeSecure/js-x-ray/commits?author=fraxken" title="Documentation">📖</a> <a href="https://github.com/NodeSecure/js-x-ray/pulls?q=is%3Apr+reviewed-by%3Afraxken" title="Reviewed Pull Requests">👀</a> <a href="#security-fraxken" title="Security">🛡️</a> <a href="https://github.com/NodeSecure/js-x-ray/issues?q=author%3Afraxken" title="Bug reports">🐛</a></td>
|
|
137
|
-
<td align="center"><a href="https://github.com/Rossb0b"><img src="https://avatars.githubusercontent.com/u/39910164?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Nicolas Hallaert</b></sub></a><br /><a href="https://github.com/NodeSecure/js-x-ray/commits?author=Rossb0b" title="Documentation">📖</a></td>
|
|
138
|
-
|
|
139
|
-
</
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
<!--
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
1
|
+
# js-x-ray
|
|
2
|
+

|
|
3
|
+
[](https://github.com/NodeSecure/js-x-ray/commit-activity)
|
|
4
|
+
[](https://github.com/nodejs/security-wg/blob/master/processes/responsible_disclosure_template.md
|
|
5
|
+
)
|
|
6
|
+
[](https://github.com/NodeSecure/js-x-ray/blob/master/LICENSE)
|
|
7
|
+

|
|
8
|
+
|
|
9
|
+
JavaScript AST analysis. This package has been created to export the [Node-Secure](https://github.com/ES-Community/nsecure) AST Analysis to enable better code evolution and allow better access to developers and researchers.
|
|
10
|
+
|
|
11
|
+
The goal is to quickly identify dangerous code and patterns for developers and Security researchers. Interpreting the results of this tool will still require you to have a set of security notions.
|
|
12
|
+
|
|
13
|
+
> 💖 I have no particular background in security. I'm simply becoming more and more interested and passionate about static code analysis. But I would be more than happy to learn that my work can help prevent potential future attacks (or leaks).
|
|
14
|
+
|
|
15
|
+
## Goals
|
|
16
|
+
The objective of the project is to successfully detect all potentially suspicious JavaScript codes.. The target is obviously codes that are added or injected for malicious purposes..
|
|
17
|
+
|
|
18
|
+
Most of the time these hackers will try to hide the behaviour of their codes as much as possible to avoid being spotted or easily understood... The work of the library is to understand and analyze these patterns that will allow us to detect malicious code..
|
|
19
|
+
|
|
20
|
+
## Features Highlight
|
|
21
|
+
- Retrieve required dependencies and files for Node.js.
|
|
22
|
+
- Detect unsafe RegEx.
|
|
23
|
+
- Get warnings when the AST Analysis as a problem or when not able to follow a statement.
|
|
24
|
+
- Highlight common attack patterns and API usages.
|
|
25
|
+
- Capable to follow the usage of dangerous Node.js globals.
|
|
26
|
+
- Detect obfuscated code and when possible the tool that has been used.
|
|
27
|
+
|
|
28
|
+
## Getting Started
|
|
29
|
+
|
|
30
|
+
This package is available in the Node Package Repository and can be easily installed with [npm](https://docs.npmjs.com/getting-started/what-is-npm) or [yarn](https://yarnpkg.com).
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
$ npm i @nodesecure/js-x-ray
|
|
34
|
+
# or
|
|
35
|
+
$ yarn add @nodesecure/js-x-ray
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Usage example
|
|
39
|
+
|
|
40
|
+
Create a local `.js` file with the following content:
|
|
41
|
+
```js
|
|
42
|
+
try {
|
|
43
|
+
require("http");
|
|
44
|
+
}
|
|
45
|
+
catch (err) {
|
|
46
|
+
// do nothing
|
|
47
|
+
}
|
|
48
|
+
const lib = "crypto";
|
|
49
|
+
require(lib);
|
|
50
|
+
require("util");
|
|
51
|
+
require(Buffer.from("6673", "hex").toString());
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
Then use `js-x-ray` to run an analysis of the JavaScript code:
|
|
57
|
+
```js
|
|
58
|
+
import { runASTAnalysis } from "@nodesecure/js-x-ray";
|
|
59
|
+
import { readFileSync } from "fs";
|
|
60
|
+
|
|
61
|
+
const str = readFileSync("./file.js", "utf-8");
|
|
62
|
+
const { warnings, dependencies } = runASTAnalysis(str);
|
|
63
|
+
|
|
64
|
+
const dependenciesName = [...dependencies];
|
|
65
|
+
const inTryDeps = [...dependencies.getDependenciesInTryStatement()];
|
|
66
|
+
|
|
67
|
+
console.log(dependenciesName);
|
|
68
|
+
console.log(inTryDeps);
|
|
69
|
+
console.log(warnings);
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
The analysis will return: `http` (in try), `crypto`, `util` and `fs`.
|
|
73
|
+
|
|
74
|
+
> ⚠️ There is also a lot of suspicious code example in the root cases directory. Feel free to try the tool on these files.
|
|
75
|
+
|
|
76
|
+
## Warnings Legends (v2.0+)
|
|
77
|
+
|
|
78
|
+
> Node-secure versions equal or lower than 0.7.0 are no longer compatible with the warnings table below.
|
|
79
|
+
|
|
80
|
+
This section describe all the possible warnings returned by JSXRay.
|
|
81
|
+
|
|
82
|
+
| name | description |
|
|
83
|
+
| --- | --- |
|
|
84
|
+
| parsing-error | An error occured when parsing the JavaScript code with meriyah. It mean that the conversion from string to AST as failed. If you encounter such an error, **please open an issue here**. |
|
|
85
|
+
| unsafe-import | Unable to follow an import (require, require.resolve) statement/expr. |
|
|
86
|
+
| unsafe-regex | A RegEx as been detected as unsafe and may be used for a ReDoS Attack. |
|
|
87
|
+
| unsafe-stmt | Usage of dangerous statement like `eval()` or `Function("")`. |
|
|
88
|
+
| unsafe-assign | Assignment of a protected global like `process` or `require`. |
|
|
89
|
+
| encoded-literal | An encoded literal has been detected (it can be an hexa value, unicode sequence, base64 string etc) |
|
|
90
|
+
| short-identifiers | This mean that all identifiers has an average length below 1.5. Only possible if the file contains more than 5 identifiers. |
|
|
91
|
+
| suspicious-literal | This mean that the sum of suspicious score of all Literals is bigger than 3. |
|
|
92
|
+
| obfuscated-code (**experimental**) | There's a very high probability that the code is obfuscated... |
|
|
93
|
+
|
|
94
|
+
> 👀 More details on warnings and their implementations [here](./WARNINGS.md)
|
|
95
|
+
|
|
96
|
+
## API
|
|
97
|
+
|
|
98
|
+
<details>
|
|
99
|
+
<summary>runASTAnalysis(str: string, options?: RuntimeOptions): Report</summary>
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
interface RuntimeOptions {
|
|
103
|
+
module?: boolean;
|
|
104
|
+
isMinified?: boolean;
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
The method take a first argument which is the code you want to analyse. It will return a Report Object:
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
interface Report {
|
|
112
|
+
dependencies: ASTDeps;
|
|
113
|
+
warnings: Warning<BaseWarning>[];
|
|
114
|
+
idsLengthAvg: number;
|
|
115
|
+
stringScore: number;
|
|
116
|
+
isOneLineRequire: boolean;
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
</details>
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
## Contributors ✨
|
|
124
|
+
|
|
125
|
+
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
|
|
126
|
+
[](#contributors-)
|
|
127
|
+
<!-- ALL-CONTRIBUTORS-BADGE:END -->
|
|
128
|
+
|
|
129
|
+
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
|
|
130
|
+
|
|
131
|
+
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
|
|
132
|
+
<!-- prettier-ignore-start -->
|
|
133
|
+
<!-- markdownlint-disable -->
|
|
134
|
+
<table>
|
|
135
|
+
<tr>
|
|
136
|
+
<td align="center"><a href="https://www.linkedin.com/in/thomas-gentilhomme/"><img src="https://avatars.githubusercontent.com/u/4438263?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Gentilhomme</b></sub></a><br /><a href="https://github.com/NodeSecure/js-x-ray/commits?author=fraxken" title="Code">💻</a> <a href="https://github.com/NodeSecure/js-x-ray/commits?author=fraxken" title="Documentation">📖</a> <a href="https://github.com/NodeSecure/js-x-ray/pulls?q=is%3Apr+reviewed-by%3Afraxken" title="Reviewed Pull Requests">👀</a> <a href="#security-fraxken" title="Security">🛡️</a> <a href="https://github.com/NodeSecure/js-x-ray/issues?q=author%3Afraxken" title="Bug reports">🐛</a></td>
|
|
137
|
+
<td align="center"><a href="https://github.com/Rossb0b"><img src="https://avatars.githubusercontent.com/u/39910164?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Nicolas Hallaert</b></sub></a><br /><a href="https://github.com/NodeSecure/js-x-ray/commits?author=Rossb0b" title="Documentation">📖</a></td>
|
|
138
|
+
<td align="center"><a href="https://github.com/antoine-coulon"><img src="https://avatars.githubusercontent.com/u/43391199?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Antoine</b></sub></a><br /><a href="https://github.com/NodeSecure/js-x-ray/commits?author=antoine-coulon" title="Code">💻</a></td>
|
|
139
|
+
</tr>
|
|
140
|
+
</table>
|
|
141
|
+
|
|
142
|
+
<!-- markdownlint-restore -->
|
|
143
|
+
<!-- prettier-ignore-end -->
|
|
144
|
+
|
|
145
|
+
<!-- ALL-CONTRIBUTORS-LIST:END -->
|
|
146
|
+
|
|
147
|
+
## License
|
|
148
|
+
MIT
|
package/index.d.ts
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
/// <reference types="meriyah"/>
|
|
2
|
-
|
|
3
|
-
import { SourceLocation } from "meriyah/dist/estree";
|
|
4
|
-
|
|
5
1
|
declare class ASTDeps {
|
|
6
2
|
constructor();
|
|
7
3
|
removeByName(name: string): void;
|
|
@@ -41,11 +37,22 @@ declare namespace JSXRay {
|
|
|
41
37
|
isOneLineRequire: boolean;
|
|
42
38
|
}
|
|
43
39
|
|
|
40
|
+
interface SourceLocation {
|
|
41
|
+
start: {
|
|
42
|
+
line: number;
|
|
43
|
+
column: number;
|
|
44
|
+
};
|
|
45
|
+
end: {
|
|
46
|
+
line: number;
|
|
47
|
+
column: number;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
44
51
|
interface Dependency {
|
|
45
52
|
unsafe: boolean;
|
|
46
53
|
inTry: boolean;
|
|
47
54
|
location?: SourceLocation;
|
|
48
|
-
}
|
|
55
|
+
}
|
|
49
56
|
|
|
50
57
|
interface WarningsNames {
|
|
51
58
|
parsingError: "parsing-error",
|
package/index.js
CHANGED
|
@@ -16,11 +16,17 @@ export function runASTAnalysis(str, options = Object.create(null)) {
|
|
|
16
16
|
// Note: if the file start with a shebang then we remove it because 'parseScript' may fail to parse it.
|
|
17
17
|
// Example: #!/usr/bin/env node
|
|
18
18
|
const strToAnalyze = str.charAt(0) === "#" ? str.slice(str.indexOf("\n")) : str;
|
|
19
|
+
const isEcmaScriptModule = Boolean(module);
|
|
19
20
|
const { body } = meriyah.parseScript(strToAnalyze, {
|
|
20
|
-
next: true,
|
|
21
|
+
next: true,
|
|
22
|
+
loc: true,
|
|
23
|
+
raw: true,
|
|
24
|
+
module: isEcmaScriptModule,
|
|
25
|
+
globalReturn: !isEcmaScriptModule
|
|
21
26
|
});
|
|
22
27
|
|
|
23
28
|
const sastAnalysis = new Analysis();
|
|
29
|
+
sastAnalysis.analyzeSourceString(str);
|
|
24
30
|
|
|
25
31
|
// we walk each AST Nodes, this is a purely synchronous I/O
|
|
26
32
|
walk(body, {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nodesecure/js-x-ray",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.3.0",
|
|
4
4
|
"description": "JavaScript AST XRay analysis",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": "./index.js",
|
|
@@ -37,21 +37,21 @@
|
|
|
37
37
|
},
|
|
38
38
|
"homepage": "https://github.com/NodeSecure/js-x-ray#readme",
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@nodesecure/sec-literal": "^1.
|
|
41
|
-
"estree-walker": "^3.0.
|
|
40
|
+
"@nodesecure/sec-literal": "^1.1.0",
|
|
41
|
+
"estree-walker": "^3.0.1",
|
|
42
42
|
"is-minified-code": "^2.0.0",
|
|
43
|
-
"meriyah": "^4.2.
|
|
43
|
+
"meriyah": "^4.2.1",
|
|
44
44
|
"safe-regex": "^2.1.1"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
|
-
"@nodesecure/eslint-config": "^1.3.
|
|
47
|
+
"@nodesecure/eslint-config": "^1.3.1",
|
|
48
48
|
"@slimio/is": "^1.5.1",
|
|
49
|
-
"@small-tech/esm-tape-runner": "^
|
|
49
|
+
"@small-tech/esm-tape-runner": "^2.0.0",
|
|
50
50
|
"@small-tech/tap-monkey": "^1.3.0",
|
|
51
|
-
"@types/node": "^
|
|
51
|
+
"@types/node": "^17.0.23",
|
|
52
52
|
"cross-env": "^7.0.3",
|
|
53
|
-
"eslint": "^8.
|
|
54
|
-
"pkg-ok": "^
|
|
55
|
-
"tape": "^5.
|
|
53
|
+
"eslint": "^8.12.0",
|
|
54
|
+
"pkg-ok": "^3.0.0",
|
|
55
|
+
"tape": "^5.5.2"
|
|
56
56
|
}
|
|
57
57
|
}
|
package/src/Analysis.js
CHANGED
|
@@ -5,7 +5,7 @@ import { Utils, Literal } from "@nodesecure/sec-literal";
|
|
|
5
5
|
import { rootLocation, toArrayLocation, generateWarning } from "./utils.js";
|
|
6
6
|
import { warnings as _warnings, processMainModuleRequire } from "./constants.js";
|
|
7
7
|
import ASTDeps from "./ASTDeps.js";
|
|
8
|
-
import { isObfuscatedCode } from "./obfuscators/index.js";
|
|
8
|
+
import { isObfuscatedCode, hasTrojanSource } from "./obfuscators/index.js";
|
|
9
9
|
import { runOnProbes } from "./probes/index.js";
|
|
10
10
|
|
|
11
11
|
// CONSTANTS
|
|
@@ -69,6 +69,12 @@ export default class Analysis {
|
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
+
analyzeSourceString(sourceString) {
|
|
73
|
+
if (hasTrojanSource(sourceString)) {
|
|
74
|
+
this.addWarning(_warnings.obfuscatedCode, "trojan-source");
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
72
78
|
analyzeString(str) {
|
|
73
79
|
const score = Utils.stringSuspicionScore(str);
|
|
74
80
|
if (score !== 0) {
|
package/src/constants.js
CHANGED
|
@@ -1,28 +1,47 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This is one of the way to get a valid require.
|
|
3
|
-
*
|
|
4
|
-
* @see https://nodejs.org/api/process.html#process_process_mainmodule
|
|
5
|
-
*/
|
|
6
|
-
export const processMainModuleRequire = "process.mainModule.require";
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* JavaScript dangerous global identifiers that can be used by hackers
|
|
10
|
-
*/
|
|
11
|
-
export const globalIdentifiers = new Set(["global", "globalThis", "root", "GLOBAL", "window"]);
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Dangerous Global identifiers parts
|
|
15
|
-
*/
|
|
16
|
-
export const globalParts = new Set([...globalIdentifiers, "process", "mainModule", "require"]);
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
1
|
+
/**
|
|
2
|
+
* This is one of the way to get a valid require.
|
|
3
|
+
*
|
|
4
|
+
* @see https://nodejs.org/api/process.html#process_process_mainmodule
|
|
5
|
+
*/
|
|
6
|
+
export const processMainModuleRequire = "process.mainModule.require";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* JavaScript dangerous global identifiers that can be used by hackers
|
|
10
|
+
*/
|
|
11
|
+
export const globalIdentifiers = new Set(["global", "globalThis", "root", "GLOBAL", "window"]);
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Dangerous Global identifiers parts
|
|
15
|
+
*/
|
|
16
|
+
export const globalParts = new Set([...globalIdentifiers, "process", "mainModule", "require"]);
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Dangerous Unicode control characters that can be used by hackers
|
|
20
|
+
* to perform trojan source.
|
|
21
|
+
*/
|
|
22
|
+
export const unsafeUnicodeControlCharacters = [
|
|
23
|
+
"\u202A",
|
|
24
|
+
"\u202B",
|
|
25
|
+
"\u202D",
|
|
26
|
+
"\u202E",
|
|
27
|
+
"\u202C",
|
|
28
|
+
"\u2066",
|
|
29
|
+
"\u2067",
|
|
30
|
+
"\u2068",
|
|
31
|
+
"\u2069",
|
|
32
|
+
"\u200E",
|
|
33
|
+
"\u200F",
|
|
34
|
+
"\u061C"
|
|
35
|
+
];
|
|
36
|
+
|
|
37
|
+
export const warnings = Object.freeze({
|
|
38
|
+
parsingError: Symbol("ParsingError"),
|
|
39
|
+
unsafeImport: Symbol("UnsafeImport"),
|
|
40
|
+
unsafeRegex: Symbol("UnsafeRegex"),
|
|
41
|
+
unsafeStmt: Symbol("UnsafeStmt"),
|
|
42
|
+
unsafeAssign: Symbol("UnsafeAssign"),
|
|
43
|
+
encodedLiteral: Symbol("EncodedLiteral"),
|
|
44
|
+
shortIdentifiers: Symbol("ShortIdentifiers"),
|
|
45
|
+
suspiciousLiteral: Symbol("SuspiciousLiteral"),
|
|
46
|
+
obfuscatedCode: Symbol("ObfuscatedCode")
|
|
47
|
+
});
|
package/src/obfuscators/index.js
CHANGED
|
@@ -1,65 +1,70 @@
|
|
|
1
|
-
// Import Third-party Dependencies
|
|
2
|
-
import { Patterns } from "@nodesecure/sec-literal";
|
|
3
|
-
|
|
4
|
-
// Import Internal Dependencies
|
|
5
|
-
import * as jjencode from "./jjencode.js";
|
|
6
|
-
import * as jsfuck from "./jsfuck.js";
|
|
7
|
-
import * as freejsobfuscator from "./freejsobfuscator.js";
|
|
8
|
-
import * as obfuscatorio from "./obfuscator-io.js";
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
// console.log(
|
|
37
|
-
// console.log(
|
|
38
|
-
// console.log(analysis.
|
|
39
|
-
// console.log(analysis.counter.
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
//
|
|
49
|
-
//
|
|
50
|
-
//
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
}
|
|
1
|
+
// Import Third-party Dependencies
|
|
2
|
+
import { Patterns } from "@nodesecure/sec-literal";
|
|
3
|
+
|
|
4
|
+
// Import Internal Dependencies
|
|
5
|
+
import * as jjencode from "./jjencode.js";
|
|
6
|
+
import * as jsfuck from "./jsfuck.js";
|
|
7
|
+
import * as freejsobfuscator from "./freejsobfuscator.js";
|
|
8
|
+
import * as obfuscatorio from "./obfuscator-io.js";
|
|
9
|
+
import * as trojan from "./trojan-source.js";
|
|
10
|
+
|
|
11
|
+
// CONSTANTS
|
|
12
|
+
const kMinimumIdsCount = 5;
|
|
13
|
+
|
|
14
|
+
export function isObfuscatedCode(analysis) {
|
|
15
|
+
let encoderName = null;
|
|
16
|
+
|
|
17
|
+
if (jsfuck.verify(analysis)) {
|
|
18
|
+
encoderName = "jsfuck";
|
|
19
|
+
}
|
|
20
|
+
else if (jjencode.verify(analysis)) {
|
|
21
|
+
encoderName = "jjencode";
|
|
22
|
+
}
|
|
23
|
+
else if (analysis.counter.morseLiteral >= 36) {
|
|
24
|
+
encoderName = "morse";
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
// TODO: also implement Dictionnary checkup
|
|
28
|
+
const { prefix, oneTimeOccurence } = Patterns.commonHexadecimalPrefix(
|
|
29
|
+
analysis.identifiersName.map((value) => value.name)
|
|
30
|
+
);
|
|
31
|
+
const uPrefixNames = new Set(Object.keys(prefix));
|
|
32
|
+
|
|
33
|
+
if (analysis.counter.identifiers > kMinimumIdsCount && uPrefixNames.size > 0) {
|
|
34
|
+
analysis.hasPrefixedIdentifiers = calcAvgPrefixedIdentifiers(analysis, prefix) > 80;
|
|
35
|
+
}
|
|
36
|
+
// console.log(prefix);
|
|
37
|
+
// console.log(oneTimeOccurence);
|
|
38
|
+
// console.log(analysis.hasPrefixedIdentifiers);
|
|
39
|
+
// console.log(analysis.counter.identifiers);
|
|
40
|
+
// console.log(analysis.counter.encodedArrayValue);
|
|
41
|
+
|
|
42
|
+
if (uPrefixNames.size === 1 && freejsobfuscator.verify(analysis, prefix)) {
|
|
43
|
+
encoderName = "freejsobfuscator";
|
|
44
|
+
}
|
|
45
|
+
else if (obfuscatorio.verify(analysis)) {
|
|
46
|
+
encoderName = "obfuscator.io";
|
|
47
|
+
}
|
|
48
|
+
// else if ((analysis.counter.identifiers > (kMinimumIdsCount * 3) && analysis.hasPrefixedIdentifiers)
|
|
49
|
+
// && (oneTimeOccurence <= 3 || analysis.counter.encodedArrayValue > 0)) {
|
|
50
|
+
// encoderName = "unknown";
|
|
51
|
+
// }
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return [encoderName !== null, encoderName];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function hasTrojanSource(sourceString) {
|
|
58
|
+
return trojan.verify(sourceString);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function calcAvgPrefixedIdentifiers(analysis, prefix) {
|
|
62
|
+
const valuesArr = Object.values(prefix).slice().sort((left, right) => left - right);
|
|
63
|
+
if (valuesArr.length === 0) {
|
|
64
|
+
return 0;
|
|
65
|
+
}
|
|
66
|
+
const nbOfPrefixedIds = valuesArr.length === 1 ? valuesArr.pop() : (valuesArr.pop() + valuesArr.pop());
|
|
67
|
+
const maxIds = analysis.counter.identifiers - analysis.idtypes.property;
|
|
68
|
+
|
|
69
|
+
return ((nbOfPrefixedIds / maxIds) * 100);
|
|
70
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { unsafeUnicodeControlCharacters } from "../constants.js";
|
|
2
|
+
|
|
3
|
+
export function verify(sourceString) {
|
|
4
|
+
for (const unsafeCharacter of unsafeUnicodeControlCharacters) {
|
|
5
|
+
if (sourceString.includes(unsafeCharacter)) {
|
|
6
|
+
return true;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
return false;
|
|
11
|
+
}
|