You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+43-19Lines changed: 43 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ JavaScript AST analysis. This package has been created to export the [Node-Secur
10
10
11
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
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).
13
+
> **Note** 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
14
15
15
## Goals
16
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..
@@ -71,7 +71,7 @@ console.log(warnings);
71
71
72
72
The analysis will return: `http` (in try), `crypto`, `util` and `fs`.
73
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.
74
+
> **Warning** There is also a lot of suspicious code example in the `./examples` cases directory. Feel free to try the tool on these files.
75
75
76
76
## Warnings
77
77
@@ -106,26 +106,24 @@ import * as i18n from "@nodesecure/i18n";
> Node-secure versions equal or lower than 0.7.0 are no longer compatible with the warnings table below.
111
+
> **Warning** versions of NodeSecure greather than v0.7.0 are no longer compatible with the warnings table below.
112
112
113
-
This section describe all the possible warnings returned by JSXRay.
113
+
This section describe all the possible warnings returned by JSXRay. Click on the warning **name** for additional information and examples.
114
114
115
-
| name | description |
116
-
| --- | --- |
117
-
| 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**. |
118
-
| unsafe-import | Unable to follow an import (require, require.resolve) statement/expr. |
119
-
| unsafe-regex | A RegEx as been detected as unsafe and may be used for a ReDoS Attack. |
120
-
| unsafe-stmt | Usage of dangerous statement like `eval()` or `Function("")`. |
121
-
| unsafe-assign | Assignment of a protected global like `process` or `require`. |
122
-
| encoded-literal | An encoded literal has been detected (it can be an hexa value, unicode sequence, base64 string etc) |
123
-
| short-identifiers | This mean that all identifiers has an average length below 1.5. Only possible if the file contains more than 5 identifiers. |
124
-
| suspicious-literal | This mean that the sum of suspicious score of all Literals is bigger than 3. |
125
-
| obfuscated-code (**experimental**) | There's a very high probability that the code is obfuscated... |
126
-
| weak-crypto (**experimental**) | The code probably contains a weak crypto algorithm ("md5...) |
127
-
128
-
> 👀 More details on warnings and their implementations [here](./WARNINGS.md)
115
+
| name | experimental | description |
116
+
| --- | :-: | --- |
117
+
|[parsing-error](./docs/parsing-error.md)| ❌ | The AST parser throw an error |
118
+
|[unsafe-import](./docs/unsafe-import.md)| ❌ | Unable to follow an import (require, require.resolve) statement/expr. |
119
+
|[unsafe-regex](./docs/unsafe-regex.md)| ❌ | A RegEx as been detected as unsafe and may be used for a ReDoS Attack. |
120
+
|[unsafe-stmt](./docs//unsafe-stmt.md)| ❌ | Usage of dangerous statement like `eval()` or `Function("")`. |
121
+
|[unsafe-assign](./docs/unsafe-assign.md)| ❌ | Assignment of a protected global like `process` or `require`. |
122
+
|[encoded-literal](./docs/encoded-literal.md)| ❌ | An encoded literal has been detected (it can be an hexa value, unicode sequence or a base64 string) |
123
+
|[short-identifiers](./docs/short-identifiers.md)| ❌ | This mean that all identifiers has an average length below 1.5. |
124
+
|[suspicious-literal](./docs/suspicious-literal.md)| ❌ | A suspicious literal has been found in the source code. |
125
+
|[obfuscated-code](./docs/obfuscated-code.md)| ✔️ | There's a very high probability that the code is obfuscated. |
126
+
|[weak-crypto](./docs/weak-crypto.md)| ✔️ | The code probably contains a weak crypto algorithm (md5, sha1...) |
Hexadecimal and Unicode sequence are tested directly on the raw Literal provided by meriyah. For base64 detection we use the npm package [is-base64](https://github.com/miguelmota/is-base64).
Example of obfuscated code is in the root `examples` directory.
19
+
20
+
### Technical note
21
+
A complete G.Drive document has been written to describe the patterns of obfuscation tools and some way of detecting them:
22
+
23
+
-[JSXRay - Patterns of obfuscated JavaScript code](https://docs.google.com/document/d/11ZrfW0bDQ-kd7Gr_Ixqyk8p3TGvxckmhFH3Z8dFoPhY/edit?usp=sharing)
24
+
25
+
> **Note** There is no frozen implementation and this is an early implementation
26
+
27
+
## Example
28
+
29
+
The following code uses Morse code to obfuscate its real intent. This was used in an attack and I find it quite funny so i implemented morse detection 😂.
30
+
31
+
```js
32
+
functiondecodeMorse(morseCode) {
33
+
var ref = {
34
+
'.-':'a',
35
+
'-...':'b',
36
+
'-.-.':'c',
37
+
'-..':'d',
38
+
'.':'e',
39
+
'..-.':'f',
40
+
'--.':'g',
41
+
'....':'h',
42
+
'..':'i',
43
+
'.---':'j',
44
+
'-.-':'k',
45
+
'.-..':'l',
46
+
'--':'m',
47
+
'-.':'n',
48
+
'---':'o',
49
+
'.--.':'p',
50
+
'--.-':'q',
51
+
'.-.':'r',
52
+
'...':'s',
53
+
'-':'t',
54
+
'..-':'u',
55
+
'...-':'v',
56
+
'.--':'w',
57
+
'-..-':'x',
58
+
'-.--':'y',
59
+
'--..':'z',
60
+
'.----':'1',
61
+
'..---':'2',
62
+
'...--':'3',
63
+
'....-':'4',
64
+
'.....':'5',
65
+
'-....':'6',
66
+
'--...':'7',
67
+
'---..':'8',
68
+
'----.':'9',
69
+
'-----':'0',
70
+
};
71
+
72
+
return morseCode
73
+
.split('')
74
+
.map(a=>a.split('').map(b=> ref[b]).join(''))
75
+
.join('');
76
+
}
77
+
78
+
var decoded =decodeMorse(".-- --- .-. -.. .-- --- .-. -..");
Parsing Error is throw when the library [meriyah](https://github.com/meriyah/meriyah) fail to parse the javascript source code into an AST. But it can also happen when the AST analysis fails because we don't manage a case properly.
10
+
11
+
> **Note** If you are in the second case, please open an issue [here](https://github.com/NodeSecure/js-x-ray/issues)
Thats one of the most interesting JS-X-Ray warning. We designed it with the idea of detecting long strings of characters that are very common in malicious obfuscated/encrypted codes like in [smith-and-wesson-skimmer](https://badjs.org/posts/smith-and-wesson-skimmer/).
10
+
11
+
The basic idea is to say that any string longer than 45 characters with no space is very suspicious... Then we establish a suspicion score that will be incremented according to several criteria:
12
+
13
+
- if the string contains **space** in the first **45** characters then we set the score to `zero`, else we set the score to `one`.
14
+
- if the string has more than **200 characters** then we add `1` to the score.
15
+
- we add one to the score for each **750 characters**. So a length of __1600__ will add `two` to the score.
16
+
- we add `two` point to the score if the string contains more than **70 unique characters**.
17
+
18
+
So it's possible for a string with more than 45 characters to come out with a score of zero if:
19
+
- there is space in the first 45 characters of the string.
20
+
- less than 70 unique characters.
21
+
22
+
The implementation is done in the [@nodesecure/sec-literal](https://github.com/NodeSecure/sec-literal/blob/main/src/utils.js) package and look like this:
0 commit comments