Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from scapy.all import *
- import datetime
- WIN_SIZE = 65535
- history_table=open("history_table.txt","r+")
- def arp_req(IPadr):
- """ Returns MAC address of given IP - CHECKED """
- packet = ARP(pdst = IPadr)
- reply = sr1(packet).hwsrc
- return reply
- def dns_req(URL):
- """ Returns IP from URL - CHECKED """
- packet = IP(dst="8.8.8.8")/UDP()/DNS(rd = 1, qd = DNSQR(qname = URL))
- try:
- reply = sr1(packet, verbose=0)[DNS].an[DNSRR].rdata
- except:
- print "IP does not exist."
- return None
- return reply
- def ret_from_cache(URL):
- """ Returns IP from URL from the DNS table if exists, else returns 0 """
- DNS_Table = open("DNS_Table.txt", "w+")
- DNS_Table_Text = DNS_Table.read()
- # If found
- if URL in DNS_Table_Text:
- tableLines = DNS_Table_Text.split("\n")
- for line in tableLines:
- if URL in line:
- currvaluelist = line.split(",")
- DNS_Table.close()
- return currvaluelist[1]
- # If not found
- else:
- DNS_Table.close()
- return False
- def dns_flush():
- """ Clears DNS Table file - UNCHECKED but most likely working """
- DNS_Table = open("DNS_Table.txt", "a+")
- DNS_Table.seek(0)
- DNS_Table.truncate()
- return 0
- def update_history(url):
- """ Updates the history table file by adding the URL and time of visiting """
- time_now=datetime.datetime.now()
- history_table.write(str(url) + "," + str(time_now) + "\n")
- return 0
- def clear_history():
- """ Removes all data from history table """
- history_table.seek(0)
- history_table.truncate()
- return 0
- def remove_from_history(url):
- history_table.seek(0)
- lines=history_table.readlines()
- history_table.seek(0)
- for x in lines:
- if url not in x:
- history_table.write(x)
- history_table.truncate()
- history_table.seek(0)
- return 0
- def find_ip(URL):
- IPaddr = ret_from_cache(URL) # Looking for ip in table.
- if IPaddr is False: # If IP wasn't found in the table.
- IPaddr = dns_req(URL)
- if IPaddr == None: # If failed to get IP
- return None
- update_cache(URL, IPaddr) # Updating cache file
- return IPaddr
- else: # If IP was found in the table
- return IPaddr
- def menu():
- """ Prints menu and returns user's selection """
- choice=0
- print "Select one of the options:\n1. History\n2. Visit a site\n3. Exit\n"
- while True:
- try:
- choice = input("Selection:")
- except:
- print "Invalid choice!"
- continue
- if choice in range(1, 4):
- break
- else:
- print "Invalid input. Try again.\n"
- return choice
- def history_menu():
- """ Prints menu and returns selection value """
- print " Menu"
- print "1. Show History"
- print "2. Clear history"
- print "3. Remove a specific record"
- while 1:
- try:
- selection = input("Selection: ")
- except:
- print "Invalid input. Try again.\n"
- continue
- if selection in range(1,4):
- return selection
- else:
- print ("Invalid input. Try again.\n")
- def make_get(URL):
- """ Create an HTTP GET message to the URL site, send it and save the data to an html file """
- ''' Getting IP '''
- IPaddr = find_ip(URL)
- print IPaddr
- if IPaddr is None:
- return None
- update_history(URL)
- ''' Three way handshake '''
- # Syn
- sport = random.randint(10000, 16000)
- seq = random.randint(1000, 200000)
- syn_pack = IP(dst=IPaddr)/TCP(dport=80, seq=seq, flags='S', sport=sport)
- # Syn Ack
- synack_pack = sr1(syn_pack, verbose=0, timeout=20)
- recv_seq = synack_pack[TCP].seq
- current_seq = seq+1
- current_ack = recv_seq+1
- print "Syn sent"
- # Ack
- ack_pack = IP(dst=IPaddr) / TCP(dport=80, seq=current_seq, ack=current_ack, flags='A', sport=sport)
- synack_pack = send(ack_pack, verbose=0)
- print "Syn ack sent"
- ''' HTTP GET Packet '''
- httpget = 'GET / HTTP/1.1\r\nHost: ' + URL + '\r\nConnection: keep-alive\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\nAccept-Encoding: \r\nAccept-Language: en-US,en;q=0.8\r\n\r\n'
- send_packet = IP(dst=IPaddr) / TCP(dport=80,window=WIN_SIZE, seq=current_seq, ack=current_ack, flags='AP', sport=sport) / Raw(httpget)
- ''' Output '''
- out = open("output.html","w")
- fin = False
- current_seq = current_seq + len(httpget)
- # Loop will recieve packets until it gets a FINISH flag, meaning the communication is done.
- while not fin:
- get_ans = sr(send_packet, verbose=0, timeout=1, multi=1) # Sending packet
- all_ans = get_ans[0]
- current_rcvd_len = 0
- print all_ans.show()
- # loop through all answers and check which has a Raw part
- for ans in all_ans:
- packet = ans[1]
- ''' Check if FIN flag was sent '''
- if TCP in packet:
- flags = packet.sprintf("%TCP.flags%") # Stores the TCP flags in a string
- if 'F' in flags: # Server has sent FIN flag
- print "Got FIN."
- fin = True
- ''' Writing raw data '''
- if Raw in packet: # If packet contains raw.
- raw_data = str(packet[Raw]) # Raw string data in raw_data.
- current_rcvd_len = len(packet[Raw])
- print "Got raw, len: " + str(current_rcvd_len)
- # Writing after headers
- end_of_http_headers = raw_data.find("\r\n\r\n") # Find the end of the packet's header.
- if end_of_http_headers > 1:
- start_offset = end_of_http_headers
- out.write(raw_data[start_offset:]) # Writing the data after the headers
- ''' Ack to the server '''
- current_rcvd_seq = packet[TCP].seq # Current seq
- ack_to_send = current_rcvd_seq + current_rcvd_len # The ack that will be sent is the current length of the data plus the ack sent
- send_packet = IP(dst=IPaddr) / TCP(dport=80, window=WIN_SIZE, seq=current_seq, ack=ack_to_send, flags='A', sport=sport)
- out.close()
- def update_cache(URL, IPnew):
- """ Adds URL to the DNS_Table """
- time_now=datetime.datetime.now()
- DNS_Table = open("DNS_Table.txt", "a+")
- print str(URL) + "," + str(IPnew) + "," + str(time_now.hour)+":"+str(time_now.minute) + "\n" # Debug
- DNS_Table.write(str(URL) + "," + str(IPnew) + "," + str(time_now.hour)+":"+str(time_now.minute) + "\n")
- DNS_Table.close()
- return 0
- def main():
- selection = 0
- history_selection = 0
- while selection != 3:
- selection = menu()
- if selection == 1: # History
- history_selection = history_menu()
- if history_selection == 1: # Read history
- history_table.seek(0)
- print history_table.read()
- elif history_selection == 2: # Clear history
- clear_history()
- elif history_selection == 3: # Remove a specific record
- remove_from_history(str(raw_input("Enter URL to remove: ")))
- else:
- print "Invalid selection!"
- elif selection == 2: # Visit a site
- make_get(raw_input("Enter URL to visit: "))
- print "Goodbye!"
- history_table.close()
- return 0
- main() # Calling main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement