Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f5cbf5a

Browse files
author
Daniele Linguaglossa
committed
fixed file fuzzing routine
1 parent cf4e2a7 commit f5cbf5a

2 files changed

Lines changed: 35 additions & 21 deletions

File tree

pyjfuzz/core/errors/__init__.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ class PJFInvalidArgument(PJFBaseException):
5656
"""
5757
err_type = "INVALID ARGUMENT"
5858

59+
class PJFInvalidJSON(PJFBaseException):
60+
"""
61+
Invalid argument passed to PyJFuzz
62+
"""
63+
err_type = "INVALID JSON"
64+
5965
class PJFSocketError(PJFBaseException):
6066
"""
6167
Socket issue
@@ -90,4 +96,13 @@ class PJFSocketPortInUse(PJFSocketError):
9096
class PJFProcessExecutionError(PJFProcessError):
9197
"""
9298
Error during process execution
93-
"""
99+
"""
100+
101+
class PJFMalformedJSON(PJFInvalidJSON):
102+
"""
103+
Invalid argument passed to PyJFuzz
104+
"""
105+
err_type = "MALFORMED JSON"
106+
107+
def __init__(self):
108+
self.message = "Invalid JSON object"

pyjfuzz/core/pjf_worker.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
from pjf_factory import PJFFactory
2929
from pjf_process_monitor import PJFProcessMonitor
3030
from pjf_external_fuzzer import PJFExternalFuzzer
31+
from errors import PJFMalformedJSON
3132
from argparse import Namespace
32-
from ast import literal_eval
3333
import tempfile
34-
import json
34+
import json as json_eval
3535
import netifaces
3636
import time
3737
import sys
@@ -127,23 +127,22 @@ def start_process_monitor(self):
127127
raise PJFBaseException(e.message)
128128

129129
def start_file_fuzz(self):
130-
try:
131130
with open(self.config.json_file, "rb") as json_file:
132-
if not self.config.strong_fuzz:
133-
setattr(self, "json", literal_eval(json_file.read()))
134-
json = PJFFactory(self.config)
135-
else:
136-
try:
137-
setattr(self, "json", literal_eval(json_file.read()))
138-
except:
139-
json_file.seek(0)
140-
setattr(self, "json", json_file.read())
141-
json = PJFFactory(self.config)
142-
json_file.close()
143-
with open(self.config.json_file, "wb") as json_file:
144-
json_file.write(json.fuzzed)
145-
except Exception as e:
146-
raise PJFBaseException(e.message)
131+
j = json_file.read()
132+
json = None
133+
try:
134+
if not self.config.strong_fuzz:
135+
setattr(self.config, "json", json_eval.loads(j))
136+
json = PJFFactory(self.config)
137+
else:
138+
setattr(self.config, "json", json_eval.loads(j))
139+
json = PJFFactory(self.config)
140+
except:
141+
raise PJFMalformedJSON()
142+
json_file.close()
143+
if json:
144+
with open(self.config.json_file, "wb") as json_file:
145+
json_file.write(json.fuzzed)
147146

148147
def start_http_server(self):
149148
try:
@@ -160,7 +159,7 @@ def start_http_server(self):
160159
def fuzz_command_line(self):
161160
try:
162161
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
163-
temp_file.write(json.dumps(self.config.json))
162+
temp_file.write(json_eval.dumps(self.config.json))
164163
temp_file.close()
165164
setattr(self, "temp_file_name", temp_file.name)
166165
if self.config.debug:
@@ -174,7 +173,7 @@ def fuzz_command_line(self):
174173

175174
def fuzz_stdin(self):
176175
try:
177-
result = PJFExternalFuzzer(self.config).execute(json.dumps(self.config.json))
176+
result = PJFExternalFuzzer(self.config).execute(json_eval.dumps(self.config.json))
178177
if result:
179178
sys.stdout.write(result)
180179
else:

0 commit comments

Comments
 (0)