2828from pjf_factory import PJFFactory
2929from pjf_process_monitor import PJFProcessMonitor
3030from pjf_external_fuzzer import PJFExternalFuzzer
31+ from errors import PJFMalformedJSON
3132from argparse import Namespace
32- from ast import literal_eval
3333import tempfile
34- import json
34+ import json as json_eval
3535import netifaces
3636import time
3737import 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