# -*- coding: utf-8 -*-

import traceback
import json

############################################ CONFIG FILE ############################################


def reset_config (filename):
	try:
		out = '''{
	"addr": "127.0.0.1",
	"port": 80,

	"lbytes": "<comando_vuln_qui>",
	"rbytes": "\\r\\n",
	
	"protocol": [
		{"action":"recv", "length":1024},
		{"action":"send", "payload":"<comando_1_qui>\\r\\n"},
		{"action":"recv", "length":1024},
		{"action":"send", "payload":"<comando_2_qui>\\r\\n"},
		{"action":"recv", "length":1024},
		{"action":"exploit"}
	]
}
'''
		f = open(filename, "w")
		f.write(out)
		f.close ()

		return 0

	except Exception, e:
		#traceback.print_exc(e)
		return -3




def read_config (filename):
	try:
		f = open(filename, "r")
		config = json.loads(f.read())
		f.close ()

		if ( (not config.get("addr", False)) and (not config.get("addr", False) != "") ):
			print "[!] Valore addr mancante"; return -1

		if ( (not config.get("port", False)) and (not config.get("port", False) != "") ):
			print "[!] Valore port mancante"; return -1

		#if ( (not config.get("lbytes", False)) ):
		#	print "[!] Valore lbytes mancante"; return -1

		#if ( (not config.get("rbytes", False)) ):
		#	print "[!] Valore rbytes mancante"; return -1

		if ( (not config.get("protocol", False)) and (len(config.get("protocol", [])) < 1) ):
			print "[!] Array protocol mancante"; return -1

		return config

	except Exception, e:
		#traceback.print_exc(e)
		return -3



############################################ SESSION FILE ############################################


def reset_session ():
	return {
		
		'raw_fuzzing':{
			'buffer_apprx_length':None
		},

		'precise_fuzzing':{
			'eip_value': None,
			'buffer_length': None
		},

		'control_eip':{
			'control_eip_pass': None
		},

		'find_badchar':{
			'badchar': None
		},

		'space_check':{
			'space': None
		},

		'shellcode':{
			'shellcode': None
		},

		'find_return_address':{
			'candidate': None,
			'return_address': None
		}

	}



def read_session(filename):

	try:
		f = open(filename, "r")
		j = json.loads(f.read ())
		f.close ()

		return j
	
	except Exception, e:
		#traceback.print_exc(e)
		return -3



def save_session(filename, SESSION):

	try:
		out = json.dumps(SESSION, ensure_ascii=False, indent=4)
		f = open(filename, "w")
		f.write(out)
		f.close ()

		print "SAVED"

		return 0
	
	except Exception, e:
		#traceback.print_exc(e)
		return -3



#######################################################################################################
