Advertisement
Yonka2019

FinalProject.py

Jun 6th, 2021 (edited)
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.14 KB | None | 0 0
  1. import hashlib
  2. from functools import partial
  3.  
  4. from scapy.layers.inet import IP, UDP
  5. from scapy.layers.l2 import Ether
  6. from scapy.packet import Raw
  7. from scapy.sendrecv import sniff, sendp
  8. import re
  9. import datetime
  10.  
  11. KEY_PATTERN = r"0(\d\d)(.+)?$"
  12. LOCATION_DATA_PATTERN = r"^location data \d{1,2}\/10: (.+)$"
  13. ALIEN_IP = "54.71.128.194"
  14. LOCATIONS_NUMBER = 10
  15. ASCII_a = 97
  16. LETTERS = 26
  17. AIRPORT = "nevada25.84"
  18.  
  19.  
  20. def main():
  21.     print("Sniffing...")
  22.     full_location = []  # Building full location list
  23.     sniff(lfilter=is_alien_packet, prn=partial(print_decrypted_alien_packet, full_location))
  24.  
  25.  
  26. def is_alien_packet(packet):
  27.     """
  28.    Checks if the given packet one of the "ALIENS" packets
  29.    @param packet: the packet to check
  30.    @return: true/false
  31.    """
  32.     return IP in packet and (packet[IP].src == ALIEN_IP or packet[IP].dst == ALIEN_IP)
  33.  
  34.  
  35. def print_decrypted_alien_packet(full_location, packet):
  36.     """
  37.    Prints the data of the given packet
  38.    @param full_location: building full location list
  39.    @param packet: the packet which contains the data to print
  40.    """
  41.     data = packet[Raw].load.decode()
  42.     decrypted = decrypt(data)
  43.  
  44.     if "location data" in decrypted:
  45.         location_data = re.search(LOCATION_DATA_PATTERN, decrypted).group(1)
  46.         full_location.append(location_data)  # Builds the full location, everytime assigns the list by 10 chars
  47.  
  48.         if len(full_location) == LOCATIONS_NUMBER:
  49.             full_location_md5 = hashlib.md5(''.join(map(str, full_location)).encode()).hexdigest()
  50.             # Hashes MD5 the 100 char location buffer and assigns into the packet data
  51.             ready = f"FLY000location_md5={full_location_md5},airport={AIRPORT},time=15:52,lane=earth.jup," \
  52.                     f"vehicle=2554,fly"
  53.  
  54.             pkt = Ether() / IP(dst=ALIEN_IP) / UDP(dport=packet[UDP].sport, sport=packet[UDP].dport) / Raw(load=ready)
  55.             # Builds the new packet with all parameters
  56.             sendp(pkt, verbose=0)  # Sends the packet (without output)
  57.  
  58.     if packet[IP].src == ALIEN_IP:  # ALIEN -> PC
  59.         print(f"|{datetime.datetime.now().strftime('%H:%M:%S')}| << {decrypted}")
  60.     else:
  61.         print(f"|{datetime.datetime.now().strftime('%H:%M:%S')}| >> {decrypted}")
  62.  
  63.  
  64. def decrypt(data):
  65.     """
  66.    Finds automatically the key and decrypts the given data
  67.    @param data: DATA (full data key+data)
  68.    @return: the DECRYPTED str
  69.    """
  70.     matches = re.search(KEY_PATTERN, data)
  71.     key = int(matches.group(1))  # Getting key from the data
  72.     input_str = xstr(matches.group(2)).lower()  # Data (without the key) and lower()
  73.  
  74.     builder = ""
  75.  
  76.     for num, ch in enumerate(input_str):
  77.         if not ch.isalpha() or num % 2 != 0:
  78.             builder += ch
  79.         else:
  80.             builder += str(chr((ord(ch) - ASCII_a - key) % LETTERS + ASCII_a))
  81.         num += 1
  82.  
  83.     return builder
  84.  
  85.  
  86. def xstr(s):
  87.     """
  88.    if str equals to None it replaces it with blank str (to avoid issues)
  89.    @param s: str
  90.    @return: new str (if None ->> '' else [nothing changed])
  91.    """
  92.     return '' if s is None else str(s)
  93.  
  94.  
  95. if __name__ == '__main__':
  96.     main()
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement