Search code examples
pythonhttpscapy

Using Scapy to fitler HTTP packets


I am trying to make a filter for packets that contain HTTP data, yet I don't have a clue on how to do so.

I.E. Is there a way to filter packets using Scapy that are only HTTP?


Solution

  • The other answers give you a solution that can only be so much accurate, as you can use HTTP in other ports than 80, and as for version 2.4.3 scapy team has released a new HTTP layer, so we don't have to rely on those assumptions anymore:

    >>> import scapy.all as S
    >>> S.load_layer("http")
    >>> HTTPRequest
    <class 'scapy.layers.http.HTTPRequest'>
    >>> def filter_get_requests(pkg):
            return pkg.haslayer(HTTPRequest) and pkg[HTTPRequest].Method==b'GET'
    
    >>> s = S.sniff(lfilter=filter_get_requests) 
    

    Then make a GET request to your favorite HTTP site and there you have it :) You can read the whole HTTP layer doc in here.