Advertisement
Yonka2019

Magshimshark.py

May 24th, 2021
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.59 KB | None | 0 0
  1. from scapy.all import *
  2. from scapy.layers.dns import DNS, DNSRR
  3. from scapy.layers.inet import IP, TCP
  4. import re
  5.  
  6. EMAIL_SEPARATOR = r"%40"  # in groopy.co.il the '@' changed to '%40' (someone@gmail.com ---> someone%40gmail.com)
  7. WEATHER_FORECAST_IP = "34.218.16.79"
  8. EXIT_CODE = "5"
  9. URL_GET_PATTERN = r"GET (.+?) "
  10. WEATHER_FORECAST_PATTERN = r"^200:ANSWER:date=(.+?)&city=(.+?)&temp=(.+?)&text=(.+)$"
  11. EMAIL_PATTERN = r"[a-zA-Z0-9_.+-]+" + EMAIL_SEPARATOR + r"[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+"
  12.  
  13.  
  14. def main():
  15.     TYPES = {"1": dns_selected, "2": weather_forecast_selected, "3": http_get_selected, "4": email_scan_selected}
  16.     user_input = ""
  17.     while user_input != EXIT_CODE:
  18.         print_menu()
  19.         try:
  20.             user_input = input("> ")
  21.             TYPES[user_input]()
  22.         except:
  23.             continue
  24.     print("Goodbye.")
  25.  
  26.  
  27. # region DNS
  28. def dns_selected():
  29.     print("Sniffing: DNS\n")
  30.     sniff(lfilter=is_dns_answer, prn=print_dns)
  31.  
  32.  
  33. def is_dns_answer(inc_packet):
  34.     my_ip = get_if_addr(conf.iface)
  35.     return DNS in inc_packet and IP in inc_packet and inc_packet[IP].src != my_ip and DNSRR in inc_packet
  36.  
  37.  
  38. def print_dns(inc_packet):
  39.     ip = inc_packet[DNS][DNSRR].rdata
  40.     domain = inc_packet[DNS][DNSRR].rrname
  41.  
  42.     if isinstance(ip, bytes):
  43.         ip = ip.decode()
  44.     if isinstance(domain, bytes):
  45.         domain = domain.decode()
  46.  
  47.     print("########\n"
  48.           f"- ip: {ip}\n"
  49.           f"- domain: {domain}")
  50.  
  51.  
  52. # endregion
  53. # region HTTP
  54. def http_get_selected():
  55.     print("Sniffing: HTTP[GET] URL\n")
  56.     sniff(lfilter=is_http_get, prn=print_http)
  57.  
  58.  
  59. def is_http_get(inc_packet):
  60.     return inc_packet.haslayer(TCP) and inc_packet.getlayer(TCP).dport == 80 and inc_packet.haslayer(Raw) \
  61.            and b'GET' in bytes(inc_packet[Raw])
  62.  
  63.  
  64. def print_http(inc_packet):
  65.     get_url = re.findall(URL_GET_PATTERN, inc_packet.getlayer(Raw).load.decode())[0]
  66.     print(f"New HTTP[GET] URL: \"{get_url}\"")
  67.  
  68.  
  69. # endregion
  70. # region EMAIL
  71. def email_scan_selected():
  72.     print("Sniffing: Email scanning\n")
  73.     sniff(lfilter=is_http_contains_email, prn=print_email_in_http)
  74.  
  75.  
  76. def is_http_contains_email(inc_packet):
  77.     try:
  78.         return inc_packet.haslayer(TCP) and inc_packet.getlayer(TCP).dport == 80 and inc_packet.haslayer(Raw) and (
  79.                 re.search(EMAIL_PATTERN, inc_packet[Raw].load.decode()) is not None)
  80.     except:
  81.         return False
  82.  
  83.  
  84. def print_email_in_http(inc_packet):
  85.     email_scanned = re.findall(EMAIL_PATTERN, inc_packet.getlayer(Raw).load.decode())[0]
  86.     print(f"New Email scanned: {email_scanned}")
  87.  
  88.  
  89. # endregion
  90. # region WEATHER
  91. def weather_forecast_selected():
  92.     print("Sniffing: Weather forecast\n")
  93.     sniff(lfilter=is_weather_forecast_answer, prn=print_weather_forecast)
  94.  
  95.  
  96. def is_weather_forecast_answer(inc_packet):
  97.     return inc_packet.haslayer(IP) and inc_packet[IP].src == WEATHER_FORECAST_IP and inc_packet.haslayer(Raw) \
  98.            and b'200:ANSWER' in bytes(inc_packet[Raw])
  99.  
  100.  
  101. def print_weather_forecast(inc_packet):
  102.     forecast = re.findall(WEATHER_FORECAST_PATTERN, inc_packet.getlayer(Raw).load.decode())[0]
  103.     print("########\n"
  104.           f"Date: {forecast[0]}\n"
  105.           f"City: {forecast[1]}\n"
  106.           f"Temperature: {forecast[2]}\n"
  107.           f"Status: {forecast[3]}")
  108.  
  109.  
  110. # endregion
  111.  
  112.  
  113. def print_menu():
  114.     print("""
  115. - [MENU] Select by the digit [MENU] -
  116. 1. Print addresses that returned from DNS server
  117. 2. Print weather client response
  118. 3. Print HTTP[GET] URL
  119. 4. Print email scan
  120. 5. Exit
  121. - [MENU] Select by the digit [MENU] -
  122.    """)
  123.  
  124.  
  125. if __name__ == '__main__':
  126.     main()
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement