def SiteRun(self, site): newtemplatefilename = "temp_siterun" + str(random.randint( 0, 65535)) + ".spt" #this is SPIKE Proxy specific code right here myRandR = requestandresponse.RequestAndResponse( spkproxy.header(), spkproxy.body(), spkproxy.header(), spkproxy.body()) myRandR.clientheader.URL = "/" myRandR.clientbody.body = [] myRandR.clientheader.setSiteTuple(site) myRandR.serverheader = None myRandR.serverbody = None #now write this as a pickle to our file #this is not SPIKE Proxy specific openfile = open(newtemplatefilename, "wb") binary = 1 cPickle.dump(myRandR, openfile, binary) openfile.close() #mark what test we are doing so we match correctly self.dositetest = 1 results = self.doTemplateRun(newtemplatefilename) #now delete our temporary file os.unlink(newtemplatefilename) return results
def constructRequest(myheader,mybody=None): #for null value if (mybody==None): mybody=spkproxy.body() #debug if 0: return "GET / HTTP/1.1\r\nHost: www.immunitysec.com\r\nContent-Length: 0\r\n\r\n" request=myheader.verb+" "+myheader.getProxyHeader()+myheader.URL #if we have arguments if myheader.useRawArguments: if len(myheader.allURLargs) > 0: request+="?"+myheader.allURLargs else: if len(myheader.URLargsDict) > 0: request+="?" request+=joinargs(myheader.URLargsDict,orderlist=myheader.orderlist) request+=" "+myheader.version+"\r\n" #ok, the first line is done! #do the rest of the headers that need order #I dunno if any except Host really need ordering, but I do it #to erase any chance of lame bugs later on #plus, python makes it quite easy needOrdered=["Host","User-Agent","Accept","Accept-Language","Accept-Encoding","Accept-Charset","Keep-Alive","Connection","Pragma","Cache-Control"] for avalue in needOrdered: request+=myheader.grabHeader(avalue) #now work on the header pairs we haven't already done for akey in myheader.headerValuesDict.keys(): if akey not in needOrdered: request+=myheader.grabHeader(akey) #ok, headers are all done except for content-length #Content-Length: 0 should always be valid, but it's #not working for some reason on get requests! if mybody.mysize!=0 or myheader.verb!="GET": if not myheader.surpressContentLength(): request+="Content-Length: "+str(len(mybody.data))+"\r\n" #ok, all headers are done, finish with blank line request+="\r\n" #ok, now add body request+="".join(mybody.data) #done! return request
def constructRequest(myheader, mybody=None): #for null value if (mybody == None): mybody = spkproxy.body() #debug if 0: return "GET / HTTP/1.1\r\nHost: www.immunitysec.com\r\nContent-Length: 0\r\n\r\n" request = myheader.verb + " " + myheader.getProxyHeader() + myheader.URL #if we have arguments if myheader.useRawArguments: if len(myheader.allURLargs) > 0: request += "?" + myheader.allURLargs else: if len(myheader.URLargsDict) > 0: request += "?" request += joinargs(myheader.URLargsDict, orderlist=myheader.orderlist) request += " " + myheader.version + "\r\n" #ok, the first line is done! #do the rest of the headers that need order #I dunno if any except Host really need ordering, but I do it #to erase any chance of lame bugs later on #plus, python makes it quite easy needOrdered = [ "Host", "User-Agent", "Accept", "Accept-Language", "Accept-Encoding", "Accept-Charset", "Keep-Alive", "Connection", "Pragma", "Cache-Control" ] for avalue in needOrdered: request += myheader.grabHeader(avalue) #now work on the header pairs we haven't already done for akey in myheader.headerValuesDict.keys(): if akey not in needOrdered: request += myheader.grabHeader(akey) #ok, headers are all done except for content-length #Content-Length: 0 should always be valid, but it's #not working for some reason on get requests! if mybody.mysize != 0 or myheader.verb != "GET": if not myheader.surpressContentLength(): request += "Content-Length: " + str(len(mybody.data)) + "\r\n" #ok, all headers are done, finish with blank line request += "\r\n" #ok, now add body request += "".join(mybody.data) #done! return request
def SiteRun(self,site): newtemplatefilename="temp_siterun"+str(random.randint(0,65535))+".spt" #this is SPIKE Proxy specific code right here myRandR=requestandresponse.RequestAndResponse(spkproxy.header(),spkproxy.body(),spkproxy.header(),spkproxy.body()) myRandR.clientheader.URL="/" myRandR.clientbody.body=[] myRandR.clientheader.setSiteTuple(site) myRandR.serverheader=None myRandR.serverbody=None #now write this as a pickle to our file #this is not SPIKE Proxy specific openfile=open(newtemplatefilename,"wb") binary=1 cPickle.dump(myRandR,openfile,binary) openfile.close() #mark what test we are doing so we match correctly self.dositetest=1 results=self.doTemplateRun(newtemplatefilename) #now delete our temporary file os.unlink(newtemplatefilename) return results
#!/usr/bin/python #testpickle.py - tests the pickle routines for a RequestAndResponse #global imports import os import dircache import pickle #my imports from spkproxy import header, body from requestandresponse import RequestAndResponse import daveutil clientheader = header() clientbody = body() serverheader = header() serverbody = body() filename = "testpickle.pickle" obj = RequestAndResponse(clientheader, clientbody, serverheader, serverbody) openfile = open(filename, "w") print "openfile=" + str(openfile) + " object: " + str(obj) pickle.dump(obj, openfile) openfile.close() print "Done!"
dorequest=0 doresponse=0 target="" for o,a in opts: if o in ["-f"]: target=a if o in ["-r"]: dorequest=1 if o in ["-R"]: doresponse=1 if target=="": usage() clientheader=header() clientbody=body() serverheader=header() serverbody=body() filename=target fd=open(filename) if fd==None: print "Bad Filename: %s"%filename sys.exit(1) obj=cPickle.load(fd) fd.close() if dorequest: print daveutil.constructRequest(obj.clientheader,obj.clientbody) if doresponse: print daveutil.constructResponse(obj.serverheader,obj.serverbody)