Skip to content

Instantly share code, notes, and snippets.

@defparam
Created March 10, 2021 15:11
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save defparam/b60dbbc9b8c6c57681124a56bd7747b7 to your computer and use it in GitHub Desktop.
Save defparam/b60dbbc9b8c6c57681124a56bd7747b7 to your computer and use it in GitHub Desktop.
Turbo Intruder JSON Fuzzing Example using JSONAccessor
# JSON Fuzz Proof of Concept using JSONAccessor
# Author: Evan Custodio (@defparam)
#
# MIT License
# Copyright 2021 Evan Custodio
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import json
from copy import deepcopy
import random
def queueRequests(target, wordlists):
engine = RequestEngine(endpoint=target.endpoint)
# Take the request and parse it into a headers + body (assuming JSON body)
headers, body = JSONAccessor.parse_into_parts(target.req)
# Create a JSONAccessor Object
JA = JSONAccessor(body)
# Iterate through each value/index in the JSON
for JSON_Value, JSON_Index in JA:
JA.initialize() # Call initialize to reset the internal JSON structure
# If the value type is a string, append a quote mark
if type(JSON_Value) == str:
JA[JSON_Index] = JSON_Value + "'"
# If the value type is an int, change it to -1
if type(JSON_Value) == int:
JA[JSON_Index] = -1
# Build an HTTP request with the mutated JSON providing the headers
test_request = JA.request(headers)
# Send out the test
engine.queue(test_request)
def handleResponse(req, interesting):
table.add(req)
# The JSONAccessor Class
#
# This class is the main JSON Parser which parses input JSON and provides
# the ability to access and modify a JSON structure as a flat list or iterator
class JSONAccessor:
def __init__(self, content):
self._d = json.loads(content)
self._d_initial = deepcopy(self._d)
self._i = 0
self._ind = 0
self._getval = None
self._size = self._countdict(self._d)
@staticmethod
def parse_into_parts(request):
tokens = request.split('\r\n\r\n')
headers = tokens[0]
body = ('\r\n\r\n').join(tokens[1:])
return (headers, body,)
def request(self, headers):
return headers + "\r\n\r\n" + str(self)
def initialize(self):
self._d = deepcopy(self._d_initial)
def _iterlist(self,l,ind,set=None):
for x in range(len(l)):
item = l[x]
if type(item) == dict:
self._iterdict(item,ind,set)
elif type(item) == list:
self._iterlist(item,ind,set)
else:
if self._i == ind:
self._getval = item
if set != None:
l[x] = set
self._i += 1
if self._getval != None:
return
def _iterdict(self,d,ind,set=None):
for key in d.keys():
item = d[key]
if type(item) == dict:
self._iterdict(item,ind,set)
elif type(item) == list:
self._iterlist(item,ind,set)
else:
if self._i == ind:
self._getval = item
if set != None:
d[key] = set
self._i += 1
if self._getval != None:
return
def _countlist(self,l):
for item in l:
if type(item) == dict:
self._countdict(item)
elif type(item) == list:
self._countlist(item)
else:
self._i += 1
def _countdict(self,d):
for key in d.keys():
item = d[key]
if type(item) == dict:
self._countdict(item)
elif type(item) == list:
self._countlist(item)
else:
self._i += 1
return self._i
def __iter__(self):
self._ind = 0
return self
def next(self):
if self._ind == len(self):
raise StopIteration
else:
ret = self[self._ind], self._ind
self._ind += 1
return ret
def __len__(self):
return self._size
def __repr__(self):
return str(self._d)
def __setitem__(self, key, value):
self._i = 0
self._getval = None
self._iterdict(self._d, key, value)
def __getitem__(self, key):
self._i = 0
self._getval = None
self._iterdict(self._d, key)
return self._getval
def __str__(self):
return json.dumps(self._d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment