Skip to content

Instantly share code, notes, and snippets.

@direvius
Created February 25, 2014 11:57
Show Gist options
  • Save direvius/9207582 to your computer and use it in GitHub Desktop.
Save direvius/9207582 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import base64
import pickle
import urllib
import sys
class Stpd(object):
'''
STPD ammo formatter
'''
def __init__(self, ammo_factory):
self.af = ammo_factory
def __iter__(self):
return ("%s None\n%s\n\n" % (len(missile), missile) for missile in self.af)
class HttpAmmo(object):
def __init__(self, uri, headers=[], method='GET', http_ver='1.1', body=''):
self.method = method
self.uri = uri
self.proto = 'HTTP/%s' % http_ver
self.headers = headers
self.body = body
if self.body is not '':
self.headers.append('Content-length: %s' % len(body))
def to_s(self):
if self.headers:
headers = '\r\n'.join(self.headers) + '\r\n'
else:
headers = ''
return "%s %s %s\r\n%s\r\n%s" % (self.method, self.uri, self.proto, headers, self.body)
class FcgiLogReader(object):
'''Read missiles from ammo file'''
def __init__(self, fcgi_file):
self.fcgi_file = fcgi_file
def __iter__(self):
line = self.fcgi_file.readline()
while line:
yield HttpAmmo(
'/advq/search',
["Connection: close", "Host: example.com"],
method='POST',
body=self.__decode_fcgi_log_line(line),
).to_s()
line = self.fcgi_file.readline()
def __decode_fcgi_log_line(self, line):
return urllib.urlencode(pickle.loads(base64.b64decode(line.split("\t")[4])), True)
afr = FcgiLogReader(sys.stdin)
with open('ammo', 'w') as ammo_file:
for missile in Stpd(afr):
ammo_file.write(missile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment