# JAW | A Graph-based Security Analysis Framework For Client-side JavaScript

**URL:** <https://onehack.st/t/jaw-a-graph-based-security-analysis-framework-for-client-side-javascript/283217>\
**Category:** Tools & Scripts\
**Tags:** programming, hacking\
**Created:** [May 21, 2024, 7:26pm UTC](https://onehack.st/t/jaw-a-graph-based-security-analysis-framework-for-client-side-javascript/283217 "2024-05-21T19:26:01Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheStrength](https://onehack.st/user_avatar/onehack.st/thestrength/32/46753_2.png) [@TheStrength](https://onehack.st/u/TheStrength)\
**Post date:** [May 21, 2024, 7:26pm UTC](https://onehack.st/t/jaw-a-graph-based-security-analysis-framework-for-client-side-javascript/283217/1 "2024-05-21T19:26:02Z")

</div>

![image](https://onehack.st/uploads/default/original/3X/4/1/418bf9e437540a0a6ecd2ba0217e364a342fd2bf.png)

[Website](https://soheilkhodayari.github.io/JAW/) | [Docs](https://github.com/SoheilKhodayari/JAW/tree/master/docs) | [Setup](https://github.com/SoheilKhodayari/JAW/tree/master/docs/installation.md) | [Crawler](https://github.com/SoheilKhodayari/JAW/tree/master/crawler) | [Quick Start](https://github.com/SoheilKhodayari/JAW#quick-start)[Docker (Example)](https://github.com/SoheilKhodayari/JAW/blob/master/analyses/example/example_analysis.py)

# JAW

[![Node](https://camo.githubusercontent.com/2c0a7c9dec13f0e77255715c73e2a4701b6487db01c35596f804cfaa0da7aa47/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6e6f64652534306c61746573742d253345253344253230362e302e302d627269676874677265656e2e737667)](https://img.shields.io/badge/node%40latest-%3E%3D%206.0.0-brightgreen.svg) [![Platform](https://camo.githubusercontent.com/769eb2b853f2d580db1bd8cdc26d046a2eb5475cbd93efdd3e5ca00bf2ed802f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f706c6174666f726d2d77696e646f77732532302537432532306d61636f732532302537432532306c696e75782d6c69676874677265792e737667)](https://img.shields.io/badge/platform-windows%20%7C%20macos%20%7C%20linux-lightgrey.svg) [![Open Source Love](https://camo.githubusercontent.com/196e8b088932bdaf1b820ca7ab78e04307999f5bd44130bbb392053d8d07a9c8/68747470733a2f2f6261646765732e66726170736f66742e636f6d2f6f732f76312f6f70656e2d736f757263652e7376673f763d313033)](https://github.com/ellerbrock/open-source-badges/) [![Tweet](https://camo.githubusercontent.com/a00d957c85627f2862ae61b59289e25f875e9ae40a716efdf5c3e032d6b8f863/68747470733a2f2f696d672e736869656c64732e696f2f747769747465722f75726c2f687474702f736869656c64732e696f2e7376673f7374796c653d736f6369616c)](https://twitter.com/intent/tweet?text=A%20Static-Dynamic%20Analysis%20Framework%20for%20Client-side%20JavaScript&url=https://github.com/SoheilKhodayari/JAW)

An open-source, prototype implementation of property graphs for JavaScript based on the [esprima](https://github.com/jquery/esprima/tree/master/src) parser, and the [EsTree SpiderMonkey Spec](https://github.com/estree/estree). JAW can be used for analyzing the client-side of web applications and JavaScript-based programs.

This project is licensed under `GNU AFFERO GENERAL PUBLIC LICENSE V3.0`. See [here](https://github.com/SoheilKhodayari/JAW/blob/master/LICENSE) for more information.

JAW has a Github pages website available at [https://soheilkhodayari.github.io/JAW/](https://soheilkhodayari.github.io/JAW/).

**Release Notes:**

- Oct 2023, JAW-v3 (Sheriff): JAW updated to detect [client-side request hijacking](https://github.com/SoheilKhodayari/JAW#/) vulnerabilities.
- July 2022, JAW-v2 ([TheThing](https://github.com/SoheilKhodayari/TheThing)): JAW updated to its next major release with the ability to detect [DOM Clobbering](https://soheilkhodayari.github.io/DOMClobbering) vulnerabilities. See [`JAW-V2`](https://github.com/SoheilKhodayari/JAW/tree/JAW-v2) branch.
- Dec 2020, JAW-v1 : first prototype version. See [`JAW-V1`](https://github.com/SoheilKhodayari/JAW/tree/JAW-v1) branch.

# Content

### Overview of JAW

1. [Test Inputs](https://github.com/SoheilKhodayari/JAW#test-inputs)
2. [Data Collection](https://github.com/SoheilKhodayari/JAW#data-collection)
3. [HPG Construction](https://github.com/SoheilKhodayari/JAW#hpg-construction)
4. [Analysis and Outputs](https://github.com/SoheilKhodayari/JAW#analysis-and-outputs)

### Setup

1. [Installation](https://github.com/SoheilKhodayari/JAW#setup-1)

### Quick Start

1. [Running the Pipeline](https://github.com/SoheilKhodayari/JAW#running-the-pipeline)
2. [Quick Example](https://github.com/SoheilKhodayari/JAW#quick-example)
3. [Crawling and Data Collection](https://github.com/SoheilKhodayari/JAW#crawling-and-data-collection)

- [Playwright CLI with Foxhound](https://github.com/SoheilKhodayari/JAW#playwright-cli-with-foxhound)
- [Puppeteer-based Crawler](https://github.com/SoheilKhodayari/JAW#puppeteer-cli)
- [Selenium-based Crawler](https://github.com/SoheilKhodayari/JAW#selenium-cli)

1. [Graph Construction](https://github.com/SoheilKhodayari/JAW#graph-construction)

- [HPG Construction CLI](https://github.com/SoheilKhodayari/JAW#hpg-construction-cli)
- [HPG Import CLI](https://github.com/SoheilKhodayari/JAW#hpg-import-cli)

1. [Security Analysis](https://github.com/SoheilKhodayari/JAW#security-analysis)

- [Running Custom Graph traversals](https://github.com/SoheilKhodayari/JAW#running-custom-graph-traversals)
- [Vulnerability Detection](https://github.com/SoheilKhodayari/JAW#vulnerability-detection)

1. [Test Web Application](https://github.com/SoheilKhodayari/JAW#test-web-application)

### Further Information

1. [Detailed Documentation](https://github.com/SoheilKhodayari/JAW#detailed-documentation)
2. [Contribution and Code of Conduct](https://github.com/SoheilKhodayari/JAW#contribution-and-code-of-conduct)
3. [Academic Publication](https://github.com/SoheilKhodayari/JAW#academic-publication)

# Overview of JAW

The architecture of the JAW is shown below.

[![](https://github.com/SoheilKhodayari/JAW/raw/master/docs/assets/architecture_v3.png)](https://github.com/SoheilKhodayari/JAW/blob/master/docs/assets/architecture_v3.png)

## Test Inputs

JAW can be used in two distinct ways:

1. **Arbitrary JavaScript Analysis:** Utilize JAW for modeling and analyzing any JavaScript program by specifying the program’s file system `path`.
2. **Web Application Analysis:** Analyze a web application by providing a single seed URL.

## Data Collection

- JAW features several JavaScript-enabled web crawlers for collecting web resources at scale.

## HPG Construction

- Use the collected web resources to create a Hybrid Program Graph (HPG), which will be imported into a [Neo4j](https://neo4j.com/) database.
- Optionally, supply the HPG construction module with a mapping of semantic types to custom JavaScript language tokens, facilitating the categorization of JavaScript functions based on their purpose (e.g., HTTP request functions).

## Analysis and Outputs

- Query the constructed `Neo4j` graph database for various analyses. JAW offers utility traversals for data flow analysis, control flow analysis, reachability analysis, and pattern matching. These traversals can be used to develop custom security analyses.
- JAW also includes built-in traversals for detecting client-side CSRF, [DOM Clobbering](http://domclob.xyz/) and request hijacking vulnerabilities.
- The outputs will be stored in the same folder as that of input.

# Setup

The installation script relies on the following prerequisites:

- Latest version of `npm package manager` (node js)
- Any stable version of `python 3.x`
- Python `pip` package manager

Afterwards, install the necessary dependencies via:

$ ./install.sh

For `detailed` installation instructions, please see [here](https://github.com/SoheilKhodayari/JAW/blob/master/docs/installation.md).

# Quick Start

## Running the Pipeline

You can run an instance of the pipeline in a background screen via:

$ python3 -m run\_pipeline --conf=config.yaml

The CLI provides the following options:

```auto
$ python3 -m run_pipeline -h

usage: run_pipeline.py [-h] [--conf FILE] [--site SITE] [--list LIST] [--from FROM] [--to TO]

This script runs the tool pipeline.

optional arguments:
  -h, --help show this help message and exit
  --conf FILE, -C FILE pipeline configuration file. (default: config.yaml)
  --site SITE, -S SITE website to test; overrides config file (default: None)
  --list LIST, -L LIST site list to test; overrides config file (default: None)
  --from FROM, -F FROM the first entry to consider when a site list is provided; overrides config file (default: -1)
  --to TO, -T TO the last entry to consider when a site list is provided; overrides config file (default: -1)

```

**Input Config:** JAW expects a `.yaml` config file as input. See [config.yaml](https://github.com/SoheilKhodayari/JAW/blob/master/config.yaml) for an example.

**Hint.** The config file specifies different passes (e.g., crawling, static analysis, etc) which can be enabled or disabled for each vulnerability class. This allows running the tool building blocks individually, or in a different order (e.g., crawl all webapps first, then conduct security analysis).

## Quick Example

For running a [quick example](https://github.com/SoheilKhodayari/JAW/blob/master/analyses/example/example_analysis.py) demonstrating how to build a property graph and run Cypher queries over it, do:

$ python3 -m analyses.example.example\_analysis --input=$(pwd)/data/test\_program/test.js

## Crawling and Data Collection

This module collects the data (i.e., JavaScript code and state values of web pages) needed for testing. If you want to test a specific JavaScipt file that you already have on your file system, you can **skip** this step.

JAW has crawlers based on [Selenium](https://selenium-python.readthedocs.io/) (JAW-v1), [Puppeteer](https://github.com/puppeteer/puppeteer) (JAW-v2, v3) and [Playwright](https://playwright.dev/) (JAW-v3). For most up-to-date features, it is recommended to use the Puppeteer- or Playwright-based versions.

### Playwright CLI with Foxhound

This web crawler employs [foxhound](https://github.com/SAP/project-foxhound/), an instrumented version of Firefox, to perform dynamic taint tracking as it navigates through webpages. To start the crawler, do:

$ cd crawler $ node crawler-taint.js --seedurl=[https://google.com](https://google.com) --maxurls=100 --headless=true --foxhoundpath=

The `foxhoundpath` is by default set to the following directory: `crawler/foxhound/firefox` which contains a binary named `firefox`.

**Note:** you need a build of [foxhound](https://github.com/SAP/project-foxhound/) to use this version. An ubuntu build is included in the JAW-v3 [release](https://github.com/SoheilKhodayari/JAW/blob/master).

### Puppeteer CLI

To start the crawler, do:

$ cd crawler $ node crawler.js --seedurl=[https://google.com](https://google.com) --maxurls=100 --browser=chrome --headless=true

See [here](https://github.com/SoheilKhodayari/JAW/blob/master/docs/puppeteer-crawler.md) for more information.

### Selenium CLI

To start the crawler, do:

$ cd crawler/hpg\_crawler $ vim docker-compose.yaml # set the websites you want to crawl here and save $ docker-compose build $ docker-compose up -d

Please refer to the documentation of the `hpg_crawler` here for more information.

## Graph Construction

### HPG Construction CLI

To generate an HPG for a given (set of) JavaScript file(s), do:

```auto
$ node engine/cli.js --lang=js --graphid=graph1 --input=/in/file1.js --input=/in/file2.js --output=$(pwd)/data/out/ --mode=csv

optional arguments:
  --lang: language of the input program
  --graphid: an identifier for the generated HPG
  --input: path of the input program(s)
  --output: path of the output HPG, must be i
  --mode: determines the output format (csv or graphML)

```

## HPG Import CLI

To import an HPG inside a neo4j graph database (docker instance), do:

```auto
$ python3 -m hpg_neo4j.hpg_import --rpath=<path-to-the-folder-of-the-csv-files> --id=<xyz> --nodes=<nodes.csv> --edges=<rels.csv>
$ python3 -m hpg_neo4j.hpg_import -h

usage: hpg_import.py [-h] [--rpath P] [--id I] [--nodes N] [--edges E]

This script imports a CSV of a property graph into a neo4j docker database.

optional arguments:
  -h, --help show this help message and exit
  --rpath P relative path to the folder containing the graph CSV files inside the `data` directory
  --id I an identifier for the graph or docker container
  --nodes N the name of the nodes csv file (default: nodes.csv)
  --edges E the name of the relations csv file (default: rels.csv)

```

## HPG Construction and Import CLI (v1)

In order to create a hybrid property graph for the output of the `hpg_crawler` and import it inside a local neo4j instance, you can also do:

$ python3 -m engine.api --js=\<program.js\> --import= --hybrid= --reqs=\<requests.out\> --evts=\<events.out\> --cookies=\<cookies.pkl\> --html=\<html\_snapshot.html\>

**Specification of Parameters:**

- `<path>`: absolute path to the folder containing the program files for analysis (must be under the `engine/outputs` folder).
- `--js=<program.js>`: name of the JavaScript program for analysis (default: `js_program.js`).
- `--import=<bool>`: whether the constructed property graph should be imported to an active neo4j database (default: true).
- `--hybrid=bool`: whether the hybrid mode is enabled (default: `false`). This implies that the tester wants to enrich the property graph by inputing files for any of the HTML snapshot, fired events, HTTP requests and cookies, as collected by the JAW crawler.
- `--reqs=<requests.out>`: for hybrid mode only, name of the file containing the sequence of obsevered network requests, pass the string `false` to exclude (default: `request_logs_short.out`).
- `--evts=<events.out>`: for hybrid mode only, name of the file containing the sequence of fired events, pass the string `false` to exclude (default: `events.out`).
- `--cookies=<cookies.pkl>`: for hybrid mode only, name of the file containing the cookies, pass the string `false` to exclude (default: `cookies.pkl`).
- `--html=<html_snapshot.html>`: for hybrid mode only, name of the file containing the DOM tree snapshot, pass the string `false` to exclude (default: `html_rendered.html`).

For more information, you can use the help CLI provided with the graph construction API:

$ python3 -m engine.api -h

## Security Analysis

The constructed HPG can then be queried using [Cypher](https://neo4j.com/docs/cypher-manual/3.5/) or the [NeoModel ORM](https://neomodel.readthedocs.io/en/latest/properties.html).

### Running Custom Graph traversals

You should place and run your queries in `analyses/<ANALYSIS_NAME>`.

#### Option 1: Using the NeoModel ORM (Deprecated)

You can use the [NeoModel ORM](https://neomodel.readthedocs.io/en/latest/properties.html) to query the HPG. To write a query:

- (1) Check out the [HPG data model](https://github.com/SoheilKhodayari/JAW/blob/master/docs/hpg-nodes.md) and [syntax tree](https://github.com/SoheilKhodayari/JAW/blob/master/docs/syntax-tree.md).
- (2) Check out the [ORM model](https://github.com/SoheilKhodayari/JAW/blob/master/hpg_neo4j/orm.py) for HPGs
- (3) See the example query file provided; `example_query_orm.py` in the `analyses/example` folder.

$ python3 -m analyses.example.example\_query\_orm

For more information, please see [here](https://github.com/SoheilKhodayari/JAW/blob/master/docs/hpg-querying.md).

#### Option 2: Using Cypher Queries

You can use [Cypher](https://neo4j.com/docs/cypher-manual/3.5/) to write custom queries. For this:

- (1) Check out the [HPG data model](https://github.com/SoheilKhodayari/JAW/blob/master/docs/hpg-nodes.md) and [syntax tree](https://github.com/SoheilKhodayari/JAW/blob/master/docs/syntax-tree.md).
- (2) See the example query file provided; `example_query_cypher.py` in the `analyses/example` folder.

$ python3 -m analyses.example.example\_query\_cypher

For more information, please see [here](https://github.com/SoheilKhodayari/JAW/blob/master/docs/hpg-querying.md).

### Vulnerability Detection

This section describes how to configure and use JAW for vulnerability detection, and how to interpret the output. JAW contains, among others, self-contained queries for detecting client-side CSRF and DOM Clobbering

**Step 1.** enable the analysis component for the vulnerability class in the input [config.yaml](https://github.com/SoheilKhodayari/JAW/blob/master/config.yaml) file:

request\_hijacking: enabled: true # […] # domclobbering: enabled: false # […] cs\_csrf: enabled: false # […]

**Step 2.** Run an instance of the pipeline with:

$ python3 -m run\_pipeline --conf=config.yaml

**Hint.** You can run multiple instances of the pipeline under different `screen`s:

$ screen -dmS s1 bash -c ‘python3 -m run\_pipeline --conf=conf1.yaml; exec sh’ $ screen -dmS s2 bash -c ‘python3 -m run\_pipeline --conf=conf2.yaml; exec sh’ $ # […]

To generate parallel configuration files automatically, you may use the [`generate_config.py`](https://github.com/SoheilKhodayari/JAW/blob/master/input/generate_config.py) script.

#### How to Interpret the Output of the Analysis?

The outputs will be stored in a file called `sink.flows.out` in the same folder as that of the input. For Client-side CSRF, for example, for each HTTP request detected, JAW outputs an entry marking the set of semantic types (a.k.a, semantic tags or labels) associated with the elements constructing the request (i.e., the program slices). For example, an HTTP request marked with the semantic type `['WIN.LOC']` is forgeable through the `window.location` injection point. However, a request marked with `['NON-REACH']` is not forgeable.

An example output entry is shown below:

```auto
[*] Tags: ['WIN.LOC']
[*] NodeId: {'TopExpression': '86', 'CallExpression': '87', 'Argument': '94'}
[*] Location: 29
[*] Function: ajax
[*] Template: ajaxloc + "/bearer1234/"
[*] Top Expression: $.ajax({ xhrFields: { withCredentials: "true" }, url: ajaxloc + "/bearer1234/" })

1:['WIN.LOC'] variable=ajaxloc
	0 (loc:6)- var ajaxloc = window.location.href

```

This entry shows that on line 29, there is a `$.ajax` call expression, and this call expression triggers an `ajax` request with the url template value of `ajaxloc + "/bearer1234/`, where the parameter `ajaxloc` is a program slice reading its value at line 6 from `window.location.href`, thus forgeable through `['WIN.LOC']`.

## Test Web Application

In order to streamline the testing process for JAW and ensure that your setup is accurate, we provide a simple `node.js` web application which you can test JAW with.

First, install the dependencies via:

$ cd tests/test-webapp $ npm install

Then, run the application in a new screen:

$ screen -dmS jawwebapp bash -c ‘PORT=6789 npm run devstart; exec sh’

# Detailed Documentation.

For more information, visit our wiki page [here](https://github.com/SoheilKhodayari/JAW/blob/master/docs/README.md). Below is a table of contents for quick access.

### The Web Crawler of JAW

- [Web Crawlers](https://github.com/SoheilKhodayari/JAW/blob/master/docs/crawlers.md)

### Data Model of Hybrid Property Graphs (HPGs)

- [Property Graph Nodes](https://github.com/SoheilKhodayari/JAW/blob/master/docs/hpg-nodes.md)
- [Grammar and Syntax Tree](https://github.com/SoheilKhodayari/JAW/blob/master/docs/syntax-tree.md)
- [Property Graph Edges](https://github.com/SoheilKhodayari/JAW/blob/master/docs/hpg-edges.md)

### Graph Construction

- [Building a Property Graph](https://github.com/SoheilKhodayari/JAW/blob/master/docs/hpg-building.md)
- [Using JAW with Neo4j Docker Container](https://github.com/SoheilKhodayari/JAW/blob/master/docs/neo4j-docker.md)

### Graph Traversals

- [Running Queries Over Property Graphs](https://github.com/SoheilKhodayari/JAW/blob/master/docs/hpg-querying.md)

# Contribution and Code Of Conduct

Pull requests are always welcomed. This project is intended to be a safe, welcoming space, and contributors are expected to adhere to the contributor [code of conduct](https://github.com/SoheilKhodayari/JAW/blob/master/CODE_OF_CONDUCT.md).

## Academic Publication

If you use the JAW for academic research, we encourage you to cite the following [paper](https://soheilkhodayari.github.io/JAW/Paper.pdf):

```auto
@inproceedings{JAW,
  title = {JAW: Studying Client-side CSRF with Hybrid Property Graphs and Declarative Traversals},
  author= {Soheil Khodayari and Giancarlo Pellegrino},
  booktitle = {30th {USENIX} Security Symposium ({USENIX} Security 21)},
  year = {2021},
  address = {Vancouver, B.C.},
  publisher = {{USENIX} Association},
}   

```

## Acknowledgements

JAW has come a long way and we want to give our contributors a well-deserved shoutout here!

[@tmbrbr](https://github.com/tmbrbr), [@c01gide](https://cispa.de/en/people/c01gide), [@jndre](https://github.com/jndre), and Sepehr Mirzaei.

## GitHub:

> **[GitHub - SoheilKhodayari/JAW: JAW: A Graph-based Security Analysis Framework for...](https://github.com/SoheilKhodayari/JAW)**
>
> JAW: A Graph-based Security Analysis Framework for Client-side JavaScript
