misc: small fix or general refactoring i did not bother commenting

This commit is contained in:
Simone Margaritelli 2019-10-07 15:32:44 +02:00
parent 60f0f6c29e
commit 90b0e10e81
No known key found for this signature in database
GPG Key ID: 82E42E7F3B34C97E

View File

@ -69,48 +69,50 @@ def get_api_token(log, keys):
def parse_packet(packet, info): def parse_packet(packet, info):
from scapy.all import Dot11Elt, Dot11Beacon, Dot11, Dot11ProbeResp, Dot11AssoReq, Dot11ReassoReq from scapy.all import Dot11Elt, Dot11Beacon, Dot11, Dot11ProbeResp, Dot11AssoReq, Dot11ReassoReq
if packet.haslayer(Dot11Beacon): if packet.haslayer(Dot11Beacon):
if packet.haslayer(Dot11Beacon) \ if packet.haslayer(Dot11ProbeResp) or packet.haslayer(Dot11AssoReq) or packet.haslayer(Dot11ReassoReq):
or packet.haslayer(Dot11ProbeResp) \ if hasattr(packet[Dot11], 'addr3'):
or packet.haslayer(Dot11AssoReq) \
or packet.haslayer(Dot11ReassoReq):
if 'bssid' not in info and hasattr(packet[Dot11], 'addr3'):
info['bssid'] = packet[Dot11].addr3 info['bssid'] = packet[Dot11].addr3
if 'essid' not in info and hasattr(packet[Dot11Elt], 'info'): if hasattr(packet[Dot11Elt], 'info'):
info['essid'] = packet[Dot11Elt].info.decode('utf-8') info['essid'] = packet[Dot11Elt].info.decode('utf-8')
return info return info
def parse_pcap(filename): def parse_pcap(filename):
logging.info("api: parsing %s ..." % filename) logging.info("api: parsing %s ..." % filename)
essid = bssid = None net_id = os.path.basename(filename).replace('.pcap', '')
if '_' in net_id:
# /root/handshakes/ESSID_BSSID.pcap
essid, bssid = net_id.split('_')
else:
# /root/handshakes/BSSID.pcap
essid, bssid = '', net_id
it = iter(bssid)
bssid = ':'.join([a + b for a, b in zip(it, it)])
info = {
'essid': essid,
'bssid': bssid
}
try: try:
from scapy.all import rdpcap from scapy.all import rdpcap
info = {}
for pkt in rdpcap(filename): for pkt in rdpcap(filename):
info = parse_packet(pkt, info) info = parse_packet(pkt, info)
if 'essid' in info and info['essid'] is not None and 'bssid' in info and info['bssid'] is not None:
break
bssid = info['bssid'] if 'bssid' in info else None
essid = info['essid'] if 'essid' in info else None
except Exception as e: except Exception as e:
bssid = None
logging.error("api: %s" % e) logging.error("api: %s" % e)
return essid, bssid return info['essid'], info['bssid']
def api_report_ap(token, essid, bssid): def api_report_ap(log, keys, token, essid, bssid):
while True:
logging.info("api: reporting %s (%s)" % (essid, bssid)) logging.info("api: reporting %s (%s)" % (essid, bssid))
try: try:
api_address = 'https://api.pwnagotchi.ai/api/v1/unit/report/ap' api_address = 'https://api.pwnagotchi.ai/api/v1/unit/report/ap'
headers = {'Authorization': 'access_token %s' % token} headers = {'Authorization': 'access_token %s' % token}
@ -120,13 +122,18 @@ def api_report_ap(token, essid, bssid):
} }
r = requests.post(api_address, headers=headers, json=report) r = requests.post(api_address, headers=headers, json=report)
if r.status_code != 200: if r.status_code != 200:
if r.status_code == 401:
logging.warning("token expired")
token = get_api_token(log, keys)
continue
else:
raise Exception("(status %d) %s" % (r.status_code, r.text)) raise Exception("(status %d) %s" % (r.status_code, r.text))
else:
return True
except Exception as e: except Exception as e:
logging.error("api: %s" % e) logging.error("api: %s" % e)
return False return False
return True
def on_internet_available(ui, keys, config, log): def on_internet_available(ui, keys, config, log):
global REPORT global REPORT
@ -149,7 +156,7 @@ def on_internet_available(ui, keys, config, log):
if net_id not in reported: if net_id not in reported:
essid, bssid = parse_pcap(pcap_file) essid, bssid = parse_pcap(pcap_file)
if bssid: if bssid:
if api_report_ap(token, essid, bssid): if api_report_ap(log, keys, token, essid, bssid):
reported.append(net_id) reported.append(net_id)
REPORT.update(data={'reported': reported}) REPORT.update(data={'reported': reported})
else: else: