From 5725d8b23956e4a20d8bb25f30b76ec4063539a8 Mon Sep 17 00:00:00 2001
From: Simone Margaritelli <evilsocket@gmail.com>
Date: Wed, 2 Oct 2019 19:41:50 +0200
Subject: [PATCH] new: gps coordinates are saved along with handshakes if gps
 is connected (related to #55)

---
 .../pwnagotchi/scripts/pwnagotchi/agent.py    |  8 +++-
 .../pwnagotchi/plugins/default/example.py     |  2 +-
 .../scripts/pwnagotchi/plugins/default/gps.py | 47 +++++++++++++++++++
 3 files changed, 54 insertions(+), 3 deletions(-)
 create mode 100644 sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/plugins/default/gps.py

diff --git a/sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/agent.py b/sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/agent.py
index bf9ecca..739d6ed 100644
--- a/sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/agent.py
+++ b/sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/agent.py
@@ -45,6 +45,9 @@ class Agent(Client, AsyncAdvertiser, AsyncTrainer):
             pass
         return False
 
+    def config(self):
+        return self._config
+
     def supported_channels(self):
         return self._supported_channels
 
@@ -336,6 +339,7 @@ class Agent(Client, AsyncAdvertiser, AsyncTrainer):
 
             try:
                 for h in [e for e in self.events() if e['tag'] == 'wifi.client.handshake']:
+                    filename = h['data']['file']
                     sta_mac = h['data']['station']
                     ap_mac = h['data']['ap']
                     key = "%s -> %s" % (sta_mac, ap_mac)
@@ -347,7 +351,7 @@ class Agent(Client, AsyncAdvertiser, AsyncTrainer):
                         if apsta is None:
                             core.log("!!! captured new handshake: %s !!!" % key)
                             self._last_pwnd = ap_mac
-                            plugins.on('handshake', self, ap_mac, sta_mac)
+                            plugins.on('handshake', self, filename, ap_mac, sta_mac)
                         else:
                             (ap, sta) = apsta
                             self._last_pwnd = ap['hostname'] if ap['hostname'] != '' and ap[
@@ -356,7 +360,7 @@ class Agent(Client, AsyncAdvertiser, AsyncTrainer):
                                 ap['channel'],
                                 sta['mac'], sta['vendor'],
                                 ap['hostname'], ap['mac'], ap['vendor']))
-                            plugins.on('handshake', self, ap, sta)
+                            plugins.on('handshake', self, filename, ap, sta)
 
             except Exception as e:
                 core.log("error: %s" % e)
diff --git a/sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/plugins/default/example.py b/sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/plugins/default/example.py
index 60efbd2..4a63275 100644
--- a/sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/plugins/default/example.py
+++ b/sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/plugins/default/example.py
@@ -143,7 +143,7 @@ def on_channel_hop(agent, channel):
 
 # called when a new handshake is captured, access_point and client_station are json objects
 # if the agent could match the BSSIDs to the current list, otherwise they are just the strings of the BSSIDs
-def on_handshake(agent, access_point, client_station):
+def on_handshake(agent, filename, access_point, client_station):
     pass
 
 
diff --git a/sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/plugins/default/gps.py b/sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/plugins/default/gps.py
new file mode 100644
index 0000000..c3c53a9
--- /dev/null
+++ b/sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/plugins/default/gps.py
@@ -0,0 +1,47 @@
+__author__ = 'evilsocket@gmail.com'
+__version__ = '1.0.0'
+__name__ = 'gps'
+__license__ = 'GPL3'
+__description__ = 'Save GPS coordinates whenever an handshake is captured.'
+__enabled__ = True  # set to false if you just don't use GPS
+
+import core
+import json
+import os
+
+device = '/dev/ttyUSB0'
+speed = 19200
+running = False
+
+
+def on_loaded():
+    core.log("GPS plugin loaded for %s" % device)
+
+
+def on_ready(agent):
+    global running
+
+    if os.path.exists(device):
+        core.log("enabling GPS bettercap's module for %s" % device)
+        try:
+            agent.run('gps off')
+        except:
+            pass
+
+        agent.run('set gps.device %s' % device)
+        agent.run('set gps.speed %d' % speed)
+        agent.run('gps on')
+        running = True
+    else:
+        core.log("no GPS detected")
+
+
+def on_handshake(agent, filename, access_point, client_station):
+    if running:
+        info = agent.session()
+        gps = info['gps']
+        gps_filename = filename.replace('.pcap', '.gps.json')
+
+        core.log("saving GPS to %s (%s)" % (gps_filename, gps))
+        with open(gps_filename, 'w+t') as fp:
+            json.dump(gps, fp)