简体   繁体   English

我的嗅探器不起作用?

[英]My scapy sniffer does not work?

I have written a program that collects data from packets in iterations. 我编写了一个程序,该程序从迭代中收集数据包。 Until a couple of days ago it worked fine. 直到几天前,它运行良好。 EDIT: SOLVED. 编辑:已解决。 I saved IP as a constant and it overwrote IP. 我将IP保存为常量,并且覆盖了IP。

from scapy.all import * 
import requests
import socket



ROUND = 2
IP = 'localhost'
PORT = 80
SERVER_ADDRESS = (IP,PORT)








def get_packet_size(packet):
    return len(packet)


def find_port(packet,ip):

    if packet[IP].dst == ip:
        if TCP in packet:
            return packet[TCP].sport
        else:
            return packet[UDP].sport
    else:
        if UDP in packet:
            return packet[TCP].dport
        else:
            return packet[UDP].dport
def check_traffic(packet , ip):

    if packet[IP].dst == ip:
        return False
    else:
        return True
def find_country(packet, ip):
    request = "http://freegeoip.net/json/"+ip
    response = requests.get(request)
    real_response = response.text

    real_response = real_response.split(",")
    country_full = real_response[2]
    country_full = country_full.split(":")
    country = country_full[1]
    country = country[1:len(country) - 1]
    print(country)

    return str(country)


def find_ip(packet):
    name = socket.gethostname()
    ip_of_agent = socket.gethostbyname(name)

    if(packet[IP].dst != ip_of_agent):
        return packet[IP].dst
    else:
        return packet[IP].src

def work_on_packets(packets):
    packet_dic = {}
    #ip_dic = []
    i = 0 # num of packet in iteration.
    for packet in packets:

        print("\n")
        packet_ip = find_ip(packet)
        print(packet_ip)

        country = find_country(packet,packet_ip)
        print(country)

        is_coming_traffic = check_traffic(packet,packet_ip) # True if coming , False if outer traffic. 

        port = find_port(packet,packet_ip)
        print(port)

        packet_size = get_packet_size(packet)
        print(packet_size)

        packet_dic["Packet "+str(i)] = [packet_ip,country,is_coming_traffic,port,packet_size]
        i = i + 1

    #send_data(packet_dic)





def is_IP(packet):
    return ((UDP in packet or TCP in packet) and IP in packet)

def main():
    print("Starting sniff..")
    while(True):
        packets = sniff(lfilter = is_IP )
        work_on_packets(packets)



main()

But right now it just doesn't work. 但是现在它不起作用了。 The output is always like this,nothing more: 输出总是这样,仅此而已:

WARNING: No route found for IPv6 destination :: (no default route?). This affects only IPv6
Starting sniff..

What could be the problem behind it? 背后可能是什么问题? any help is great! 任何帮助都很棒!

It's just a warning telling you that you don;t have any default routes for IPV6 and is totally normal for systems that do not have IPV6. 这只是一个警告,告诉您您没有IPV6的任何默认路由,对于没有IPV6的系统是完全正常的。 This should not affect your program for IPV4 packets. 这不会影响您的IPV4数据包程序。

Here's how you can disable it 停用方法如下

import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM