Using Twisted Python library to replicate packets

Most of the programming I do is packet replication and automation. My workflow is to first analyze the handshakes and protocols in FireFox with the Tamper Data add-on. I use WireShark to nail down the interaction details. Finally, I recreate everything using the Twisted library in Python.

Here’s a boilerplate to quickly recreate packets using Twisted. It requests and saves a copy of Google.com. Simply replace with your favorite URL, or un-comment the two lines and comment the other getPage to send a POST request.

from twisted.web import client
from twisted.internet import reactor
 
agent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.12) Gecko/2009070611 Firefox/3.0.12 (.NET CLR 3.5.30729)"
headers = {'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language':'en-us,en;q=0.5', 'Accept-Charset':'ISO-8859-1,utf-8;q=0.7'}
headers_post = headers.copy()
headers_post['Content-Type'] = 'application/x-www-form-urlencoded'
 
def success(data):
    print "Success!"
    f = open('save.html','w')
    f.write(data)
    f.close()
    reactor.stop()
 
def failure(fail):
    print fail.getErrorMessage()
    reactor.stop()
 
def request():
    url = "http://google.com/"
    d = client.getPage(url, headers=headers, agent=agent)
    #post_data = ""
    #d = client.getPage(url, postdata=post_data, method="POST", headers=headers_post, agent=agent)
    d.addCallback(success).addErrback(failure)
 
request()
reactor.run()

To make it continually request the same page, change the success method to this:

def success(data):
    print "Success!"
    f = open('save.html','w')
    f.write(data)
    f.close()
    reactor.callLater(0.5, request)
Leave a comment

1 Comments.

Leave a Reply


[ Ctrl + Enter ]