Giter VIP home page Giter VIP logo

js-x-ray's Introduction

js-x-ray

version Maintenance Security Responsible Disclosure mit dep size

JavaScript AST analysis. This package has been created to export the Node-Secure AST Analysis to enable better code evolution and allow better access to developers and researchers.

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.

๐Ÿ’– 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).

Goals

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..

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..

Features Highlight

  • Retrieve required dependencies and files for Node.js.
  • Detect unsafe RegEx.
  • Get warnings when the AST Analysis as a problem or when not able to follow a statement.
  • Highlight common attack patterns and API usages.
  • Capable to follow the usage of dangerous Node.js globals.
  • Detect obfuscated code and when possible the tool that has been used.

Getting Started

This package is available in the Node Package Repository and can be easily installed with npm or yarn.

$ npm i js-x-ray
# or
$ yarn add js-x-ray

Usage example

Create a local .js file with the following content:

try  {
    require("http");
}
catch (err) {
    // do nothing
}
const lib = "crypto";
require(lib);
require("util");
require(Buffer.from("6673", "hex").toString());

Then use js-x-ray to run an analysis of the JavaScript code:

const { runASTAnalysis } = require("js-x-ray");
const { readFileSync } = require("fs");

const str = readFileSync("./file.js", "utf-8");
const { warnings, dependencies } = runASTAnalysis(str);

const dependenciesName = [...dependencies];
const inTryDeps = [...dependencies.getDependenciesInTryStatement()];

console.log(dependenciesName);
console.log(inTryDeps);
console.log(warnings);

The analysis will return: http (in try), crypto, util and fs.

โš ๏ธ There is also a lot of suspicious code example in the root cases directory. Feel free to try the tool on these files.

Warnings Legends (v2.0+)

Node-secure versions equal or lower than 0.7.0 are no longer compatible with the warnings table below.

This section describe all the possible warnings returned by JSXRay.

name description
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.
unsafe-import Unable to follow an import (require, require.resolve) statement/expr.
unsafe-regex A RegEx as been detected as unsafe and may be used for a ReDoS Attack.
unsafe-stmt Usage of dangerous statement like eval() or Function("").
unsafe-assign Assignment of a protected global like process or require.
encoded-literal An encoded literal has been detected (it can be an hexa value, unicode sequence, base64 string etc)
short-identifiers This mean that all identifiers has an average length below 1.5. Only possible if the file contains more than 5 identifiers.
suspicious-literal This mean that the sum of suspicious score of all Literals is bigger than 3.
obfuscated-code (experimental) There's a very high probability that the code is obfuscated...

๐Ÿ‘€ More details on warnings and their implementations here

API

runASTAnalysis(str: string, options?: RuntimeOptions): Report
interface RuntimeOptions {
    module?: boolean;
    isMinified?: boolean;
}

The method take a first argument which is the code you want to analyse. It will return a Report Object:

interface Report {
    dependencies: ASTDeps;
    warnings: Warning<BaseWarning>[];
    idsLengthAvg: number;
    stringScore: number;
    isOneLineRequire: boolean;
}
generateWarning(kind: string, options?: WarningOptions): Warning< BaseWarning >

Generate a new Warning Object.

interface WarningOptions {
    location: Location;
    file?: string;
    value?: string;
}
rootLocation(): SourceLocation

Return a default SourceLocation with all row and column set to zero.

{ start: { line: 0, column: 0 }, end: { line: 0, column: 0 } }

License

MIT

js-x-ray's People

Contributors

fraxken avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.