AI prompts
base on A PoC code for JSON Smuggling technique to smuggle arbitrary files through JSON # jsmug - JSON Smuggler
`jsmug` is a simple PoC code for JSON smuggling technique. JSON smuggling technique takes advantage of insignificant bytes in a JSON document to smuggle arbitrary files.
<br/><br/>
## Usage
<br/>
### Compilation:
```bash
$ gcc jsmug.c -o ./jsmug
```
<br/>
### Encoding file:
```bash
$ ./jsmug encode <input_file_name> <output_file_name> <bytes_per_pair>
```
**Example:**
```bash
$ ./jsmug encode ./malicious-binary encoded-binary.json 20
```
In the below example, We encoded `naabu` binary in a JSON file named `sweet-document.json`.
![Encoding File](media/encoding-demo.png)
<br/><br/>
### Decoding file:
```bash
$ ./jsmug decode <encoded_file_name> <output_file_name>
```
**Example:**
```bash
$ ./jsmug decode ./encoded-binary.json decoded-binary
```
In the below example, We decoded the `sweet-document.json` JSON file generated earlier to retrieve original `naabu` binary named `decoded-binary`.
![Decoding File](media/decoding-demo.png)
<br/><br/>
## Background
<br/>
As per the [JSON RFC](https://datatracker.ietf.org/doc/html/rfc8259#section-2), Certain bytes known as insignificant bytes are allowed at certain positions in a JSON file. These insignificant bytes carry no meaning in a JSON document and hence are ignored by JSON parsing tools such as `jq`. There are 4 allowed insignificant bytes mentioned in the JSON RFC:
* 0x09 (Horizontal Tab)
* 0x0a (New Line)
* 0x0d (Carriage Return)
* 0x20 (Space)
Since these bytes are invisible to human eye and also ignored by JSON parsers, These 4 bytes can be used for encoding arbitrary data or file. Same as we use Base 2 system for representing something in binary format, We can use a Base 4 sytem to represent data using these 4 bytes.
```
+---+---+
-------------------- | A | B | ------------------------- Raw Bytes ▲
| +---+---+ | |
| | | ENCODING
| | | ▼
| | | ▼
| | | ▼
+---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+---+---+ | ▼
| 0 | 0 | 0 | 0 | 1 | 0 | 0 | 1 | | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 2 | Base4 Representation | ▼
+---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+---+---+ |
| | |
| | |
| | |
+----+----+----+----+----+----+----+----+ +----+----+----+----+----+----+----+----+ |
| \t | \t | \t | \t | \n | \t | \t | \n | | \t | \t | \t | \t | \n | \t | \t | \r | Custom Base4 Symbol Representation |
+----+----+----+----+----+----+----+----+ +----+----+----+----+----+----+----+----+ |
\ / |
\ / |
\ / |
\ / |
\ / |
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+ |
| \t | \t | \t | \t | \n | \t | \t | \n | \t | \t | \t | \t | \n | \t | \t | \r | Payload Bytes |
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+ |
| |
| |
| bytes_per_pair = 5; | ▲
| no_of_pairs = ceil(8 * 2 / 5 ) = 4 | ▲
| | ▲
| | ▲
+----------------------------------------------------------------+ | ▲
| {"data":[\t\t\t\t\n{\t\t\n\t\t"json"\t\t\n\t\t:\r"smuggled"}]} | JSON Bytes | DECODING
+----------------------------------------------------------------+ |
|=========| |=======| |========| |=| ▼
p1 p2 p3 p4
```
<br/>
The above ASCII diagram shows how raw bytes are represented in their Base4 representation. Then those Base4 bytes are mapped to their respective insignificant bytes. Based on the `bytes_per_pair` specified, The Base4 symbol bytes are divided in pairs and placed at certain locations in a JSON document. The ability to specify how many bytes should be together assits in bypassing any detection rules written specifically for this kind of action.
<br/><br/>
## Credits
<br/>
The idea of smuggling files/binaries/data using this technique was based on an article by [Grimminck](https://grimminck.medium.com/) named "[*JSON Smuggling: A far-fetched intrusion detection evasion technique*](https://grimminck.medium.com/json-smuggling-a-far-fetched-intrusion-detection-evasion-technique-51ed8f5ee05f)". In his article, He demonstrates an undisclosed PoC for this techniqe, I thought it would be nice to try to create one myself.
<br/><br/>
## Notes
<br/>
* While keeping a lower number for `bytes_per_pair` would be nice to evade IDS, Please make sure you are using a higher number of `bytes_per_pair` for files of bigger size as it might lead to size calculations exceeding `size_t` limit ultimately resulting in a segmentation fault.", Assign "at most 3 tags" to the expected json: {"id":"8686","tags":[]} "only from the tags list I provide: [{"id":77,"name":"3d"},{"id":89,"name":"agent"},{"id":17,"name":"ai"},{"id":54,"name":"algorithm"},{"id":24,"name":"api"},{"id":44,"name":"authentication"},{"id":3,"name":"aws"},{"id":27,"name":"backend"},{"id":60,"name":"benchmark"},{"id":72,"name":"best-practices"},{"id":39,"name":"bitcoin"},{"id":37,"name":"blockchain"},{"id":1,"name":"blog"},{"id":45,"name":"bundler"},{"id":58,"name":"cache"},{"id":21,"name":"chat"},{"id":49,"name":"cicd"},{"id":4,"name":"cli"},{"id":64,"name":"cloud-native"},{"id":48,"name":"cms"},{"id":61,"name":"compiler"},{"id":68,"name":"containerization"},{"id":92,"name":"crm"},{"id":34,"name":"data"},{"id":47,"name":"database"},{"id":8,"name":"declarative-gui "},{"id":9,"name":"deploy-tool"},{"id":53,"name":"desktop-app"},{"id":6,"name":"dev-exp-lib"},{"id":59,"name":"dev-tool"},{"id":13,"name":"ecommerce"},{"id":26,"name":"editor"},{"id":66,"name":"emulator"},{"id":62,"name":"filesystem"},{"id":80,"name":"finance"},{"id":15,"name":"firmware"},{"id":73,"name":"for-fun"},{"id":2,"name":"framework"},{"id":11,"name":"frontend"},{"id":22,"name":"game"},{"id":81,"name":"game-engine "},{"id":23,"name":"graphql"},{"id":84,"name":"gui"},{"id":91,"name":"http"},{"id":5,"name":"http-client"},{"id":51,"name":"iac"},{"id":30,"name":"ide"},{"id":78,"name":"iot"},{"id":40,"name":"json"},{"id":83,"name":"julian"},{"id":38,"name":"k8s"},{"id":31,"name":"language"},{"id":10,"name":"learning-resource"},{"id":33,"name":"lib"},{"id":41,"name":"linter"},{"id":28,"name":"lms"},{"id":16,"name":"logging"},{"id":76,"name":"low-code"},{"id":90,"name":"message-queue"},{"id":42,"name":"mobile-app"},{"id":18,"name":"monitoring"},{"id":36,"name":"networking"},{"id":7,"name":"node-version"},{"id":55,"name":"nosql"},{"id":57,"name":"observability"},{"id":46,"name":"orm"},{"id":52,"name":"os"},{"id":14,"name":"parser"},{"id":74,"name":"react"},{"id":82,"name":"real-time"},{"id":56,"name":"robot"},{"id":65,"name":"runtime"},{"id":32,"name":"sdk"},{"id":71,"name":"search"},{"id":63,"name":"secrets"},{"id":25,"name":"security"},{"id":85,"name":"server"},{"id":86,"name":"serverless"},{"id":70,"name":"storage"},{"id":75,"name":"system-design"},{"id":79,"name":"terminal"},{"id":29,"name":"testing"},{"id":12,"name":"ui"},{"id":50,"name":"ux"},{"id":88,"name":"video"},{"id":20,"name":"web-app"},{"id":35,"name":"web-server"},{"id":43,"name":"webassembly"},{"id":69,"name":"workflow"},{"id":87,"name":"yaml"}]" returns me the "expected json"