Advertisement
Guest User

Untitled

a guest
Jul 18th, 2015
500
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.53 KB | None | 0 0
  1. import socket,sys
  2. import time
  3. from struct import *
  4. from collections import OrderedDict
  5. import os
  6. import optparse
  7. #define ETH_P_ALL    0x0003
  8.  
  9. parser = optparse.OptionParser("usage: %prog -t <sniff etmek ucun vaxt>")
  10. parser.add_option('-t','--time',dest='usertime',type='int', help='Sebekeni sniff etmek ucun vaxt')
  11. (options,args) = parser.parse_args()
  12.  
  13. if(options.usertime == None):
  14.     print parser.usage
  15.     sys.exit(0)
  16.  
  17.  
  18. timeforsniff=options.usertime
  19.  
  20.  
  21. def eth_addr (a) :
  22.     b = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x" % (ord(a[0]) , ord(a[1]) , ord(a[2]), ord(a[3]), ord(a[4]) , ord(a[5]))
  23.     return b
  24.  
  25. def time_diff(outside,vaxt=5):
  26.     netice = (time.time()-int(outside))/60
  27.     if(netice>=vaxt):
  28.         return True
  29.  
  30. if(os.name=='nt'):
  31.     print "[*]Windows OS desteklemir"
  32.     sys.exit()
  33.  
  34. try:
  35.     s = socket.socket( socket.AF_PACKET , socket.SOCK_RAW , socket.ntohs(0x0003))
  36. except socket.error , msg:
  37.     print '[*]Socket yaranmadi! Error Code : ' + str(msg[0]) + ' Error Message ' + msg[1]
  38.     sys.exit()
  39. except AttributeError:
  40.     print "[*]Windows OS de AF_PACKET islemir."
  41.     sys.exit()
  42.  
  43. now = time.time()
  44. protocol_numb = {"1":"ICMP","6":"TCP","17":"UDP"}
  45. #butun_paketler = {"ICMP":{},"TCP":{},"UDP":{}}
  46. butun_paketler = OrderedDict([("ICMP",OrderedDict([])),("TCP",OrderedDict([])),("UDP",OrderedDict([]))])
  47.  
  48. print "-"*55
  49. print "PunCap Packet Collector v1"
  50. print "http://anti-armenia.org \n(c) 2015"
  51. print "-"*55
  52. print "Paketler capture olunur..."
  53. while True:
  54.     if(time_diff(now,timeforsniff)):
  55.         break
  56.      
  57.      
  58.     packet = s.recvfrom(65565)
  59.  
  60.     packet = packet[0]
  61.     eth_length = 14
  62.  
  63.     eth_header = packet[:eth_length]
  64.     eth = unpack('!6s6sH' , eth_header)
  65.     eth_protocol = socket.ntohs(eth[2])
  66.     dest_mac = eth_addr(packet[0:6])
  67.     source_mac = eth_addr(packet[6:12])
  68.  
  69.     if eth_protocol == 8 :
  70.         ip_header = packet[eth_length:20+eth_length]
  71.      
  72.         iph = unpack('!BBHHHBBH4s4s' , ip_header)
  73.  
  74.         version_ihl = iph[0]
  75.         version = version_ihl >> 4
  76.         ihl = version_ihl & 0xF
  77.  
  78.         iph_length = ihl * 4
  79.         protocol = iph[6]
  80.         if(str(iph[6]) not in protocol_numb.keys()):
  81.             protocol_name = str(iph[6])
  82.         else:
  83.             protocol_name = protocol_numb[str(iph[6])]
  84.         s_addr = socket.inet_ntoa(iph[8]);
  85.         d_addr = socket.inet_ntoa(iph[9]);
  86.         timestamp = time.time();
  87.         elave=None
  88.      
  89.      
  90.         #TCP protocol
  91.         if protocol == 6 :
  92.             t = iph_length + eth_length
  93.             tcp_header = packet[t:t+20]
  94.             tcph = unpack('!HHLLBBHHH' , tcp_header)
  95.             source_port = tcph[0]
  96.             dest_port = tcph[1];
  97.             if(str(s_addr) not in butun_paketler['TCP']):
  98.                 elave = OrderedDict([("Source_Mac",str(source_mac)),("Dest_Mac",str(dest_mac)),("Source_Port",str(source_port)),("Dest_Port",str(dest_port)),("Source_IP",str(s_addr)),("Dest_IP",str(d_addr)),("TimeStamp",str(int(timestamp)))])
  99.                 #butun_paketler["TCP"][str(s_addr)] = elave;
  100.                 butun_paketler["TCP"][str(s_addr)] = OrderedDict([]);
  101.                 butun_paketler["TCP"][str(s_addr)]["0"] = elave
  102.             else:
  103.                 all_key = butun_paketler['TCP'][str(s_addr)].keys();
  104.                 last = all_key[len(butun_paketler['TCP'][str(s_addr)].keys())-1]
  105.                 reqem = int(last)+1
  106.                 elave = OrderedDict([("Source_Mac",str(source_mac)),("Dest_Mac",str(dest_mac)),("Source_Port",str(source_port)),("Dest_Port",str(dest_port)),("Source_IP",str(s_addr)),("Dest_IP",str(d_addr)),("TimeStamp",str(int(timestamp)))])
  107.                 butun_paketler["TCP"][str(s_addr)][str(reqem)] = elave;
  108.                  
  109.  
  110.         #ICMP Packets
  111.         elif protocol == 1 :
  112.             u = iph_length + eth_length
  113.             icmph_length = 4
  114.             icmp_header = packet[u:u+4]
  115.          
  116.             icmph = unpack('!BBH' , icmp_header)
  117.             icmp_type = icmph[0]
  118.             code = icmph[1]
  119.          
  120.             if(str(s_addr) not in butun_paketler['ICMP']):
  121.                 elave = OrderedDict([("Source_Mac",str(source_mac)),("Dest_Mac",str(dest_mac)),("ICMP_TYPE",str(icmp_type)),("ICMP_CODE",str(code)),("Source_IP",str(s_addr)),("Dest_IP",str(d_addr)),("TimeStamp",str(int(timestamp)))])
  122.                
  123.                 #butun_paketler["ICMP"][str(s_addr)] = elave;
  124.                 butun_paketler["ICMP"][str(s_addr)] = OrderedDict([]);
  125.                 butun_paketler["ICMP"][str(s_addr)]["0"] = elave
  126.             else:
  127.                 all_key = butun_paketler['ICMP'][str(s_addr)].keys();
  128.                 last = all_key[len(butun_paketler['ICMP'][str(s_addr)].keys())-1]
  129.                 reqem = int(last)+1
  130.                 elave = OrderedDict([("Source_Mac",str(source_mac)),("Dest_Mac",str(dest_mac)),("ICMP_TYPE",str(icmp_type)),("ICMP_CODE",str(code)),("Source_IP",str(s_addr)),("Dest_IP",str(d_addr)),("TimeStamp",str(int(timestamp)))])
  131.                 butun_paketler["ICMP"][str(s_addr)][str(reqem)] = elave;
  132.  
  133.         #UDP packets
  134.      
  135.      
  136.         elif protocol == 17 :
  137.             u = iph_length + eth_length
  138.             udph_length = 8
  139.             udp_header = packet[u:u+8]
  140.             udph = unpack('!HHHH' , udp_header)
  141.          
  142.             source_port = udph[0]
  143.             dest_port = udph[1]
  144.          
  145.             if(str(s_addr) not in butun_paketler['UDP']):
  146.                 elave = OrderedDict([("Source_Mac",str(source_mac)),("Dest_Mac",str(dest_mac)),("Source_Port",str(source_port)),("Dest_Port",str(dest_port)),("Source_IP",str(s_addr)),("Dest_IP",str(d_addr)),("TimeStamp",str(int(timestamp)))])
  147.                 butun_paketler["UDP"][str(s_addr)] = OrderedDict([]);
  148.                 butun_paketler["UDP"][str(s_addr)]["0"] = elave              
  149.             else:
  150.                 all_key = butun_paketler['UDP'][str(s_addr)].keys();
  151.                 last = all_key[len(butun_paketler['UDP'][str(s_addr)].keys())-1]
  152.                 reqem = int(last)+1
  153.                 elave = OrderedDict([("Source_Mac",str(source_mac)),("Dest_Mac",str(dest_mac)),("Source_Port",str(source_port)),("Dest_Port",str(dest_port)),("Source_IP",str(s_addr)),("Dest_IP",str(d_addr)),("TimeStamp",str(int(timestamp)))])
  154.                 butun_paketler["UDP"][str(s_addr)][str(reqem)] = elave;
  155.          
  156.  
  157.         else :
  158.             pass
  159.  
  160.  
  161.  
  162. icmp= 0
  163. tcp = 0
  164. udp = 0
  165.  
  166. ip_vs_paket = OrderedDict([])
  167. packet_target = []
  168. temp = []
  169.  
  170. for protocol_types in butun_paketler:
  171.     for ip_addreses in butun_paketler[protocol_types]:
  172.         for packet_accept_order in butun_paketler[protocol_types][ip_addreses]:
  173.             if(protocol_types=="ICMP"):
  174.                 icmp=icmp+1
  175.             elif(protocol_types=="TCP"):
  176.                 tcp = tcp+1
  177.             elif(protocol_types=="UDP"):
  178.                 udp=udp+1
  179.  
  180.  
  181. cemi = tcp+udp+icmp
  182.  
  183. for protocol_types in butun_paketler:
  184.     for ip_addreses in butun_paketler[protocol_types]:
  185.         if(ip_addreses not in ip_vs_paket):
  186.             ip_vs_paket[ip_addreses] = OrderedDict([])
  187.             ip_vs_paket[ip_addreses][protocol_types] = len(butun_paketler[protocol_types][ip_addreses])
  188.         else:
  189.             ip_vs_paket[ip_addreses][protocol_types] = len(butun_paketler[protocol_types][ip_addreses])
  190.  
  191.        
  192.        
  193. for protocol_types in butun_paketler:
  194.     for ip_addreses in butun_paketler[protocol_types]:
  195.         for packet_accept_order in butun_paketler[protocol_types][ip_addreses]:
  196.             raw_packet_info = butun_paketler[protocol_types][ip_addreses][packet_accept_order]
  197.             s_addr = raw_packet_info['Source_IP']
  198.             d_addr = raw_packet_info['Dest_IP']
  199.             s_mac =  raw_packet_info['Source_Mac']
  200.             d_mac =  raw_packet_info['Dest_Mac']
  201.             timestamp = raw_packet_info['TimeStamp']
  202.            
  203.             if(protocol_types=="TCP"):
  204.                 s_port = raw_packet_info['Source_Port']
  205.                 d_port = raw_packet_info['Dest_Port']
  206.                 packet_target.append(s_addr+":"+s_port+"->"+d_addr+":"+d_port+"_TCP")
  207.            
  208.             elif(protocol_types=="UDP"):
  209.                 s_port = raw_packet_info['Source_Port']
  210.                 d_port = raw_packet_info['Dest_Port']
  211.                 packet_target.append(s_addr+":"+s_port+"->"+d_addr+":"+d_port+"_UDP")
  212.                
  213.             elif(protocol_types=="ICMP"):
  214.                 icmp_type = raw_packet_info['ICMP_TYPE']
  215.                 icmp_code = raw_packet_info['ICMP_CODE']
  216.                 packet_target.append(s_addr+"->"+d_addr+"_ICMP")
  217.            
  218. #print butun_paketler
  219.  
  220. print "-"*70
  221. print "Umumi Statistika:"
  222. print "-"*50
  223. print "Cemi Paket:",cemi
  224. print "TCP paketlerin sayi:",tcp
  225. print "UDP paketlerin sayi:",udp
  226. print "ICMP paketlerin sayi:",icmp
  227. print "-"*50
  228. print "Her IP gonderdiyi paket:"
  229. print "-"*50
  230. for ip in ip_vs_paket:
  231.     data = ip_vs_paket[ip]
  232.     if("TCP" not in data.keys()):
  233.         tcp_spec = 0
  234.     else:
  235.         tcp_spec = data["TCP"]
  236.    
  237.     if("UDP" not in data.keys()):
  238.         udp_spec = 0
  239.     else:
  240.         udp_spec = data["UDP"]
  241.     if("ICMP" not in data.keys()):
  242.         icmp_spec= 0
  243.     else:
  244.         icmp_spec= data["ICMP"]
  245.     print ip,"--","TCP=",tcp_spec,",UDP=",udp_spec,",ICMP=",icmp_spec
  246. print "-"*50
  247. print "IPlerin gonderdiyi paketlerin miqdari:"
  248. print "-"*50
  249. for data in packet_target:
  250.     if(data not in temp):
  251.         temp.append(data)
  252.         sayi = packet_target.count(data)
  253.         bol = data.split("_")
  254.         print bol[0]+" Sayi:"+str(sayi)+" "+bol[1]+" Protokolu"
  255.        
  256. print "-"*50
  257. print "-"*70
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement