You need to use the sprintf function of the package instead of printing the package itself. You also need to split the string returned from it and combine it together with the newline characters, otherwise it spits everything out on one line:
#!/usr/bin/python from scapy.all import * def http_header(packet): http_packet=str(packet) if http_packet.find('GET'): return GET_print(packet) def GET_print(packet1): ret = "***************************************GET PACKET****************************************************\n" ret += "\n".join(packet1.sprintf("{Raw:%Raw.load%}\n").split(r"\r\n")) ret += "*****************************************************************************************************\n" return ret sniff(iface='eth0', prn=http_header, filter="tcp port 80")
I also added a filter for TCP port 80, but this can be removed if you need to.
Output Example:
***************************************GET PACKET**************************************************** 'GET /projects/scapy/doc/usage.html HTTP/1.1 Host: www.secdev.org Connection: keep-alive Cache-Control: max-age=0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36 Referer: https://www.google.co.uk/ Accept-Encoding: gzip, deflate, sdch Accept-Language: en-GB,en;q=0.8,en-US;q=0.6 If-None-Match: "28c84-48498d5654df67640-gzip" If-Modified-Since: Mon, 19 Apr 2010 15:44:17 GMT ' *****************************************************************************************************
Pierre points out that you can completely eliminate the http_header function by using the lfilter argument to sniff() . I let the code be a little more concise at the same time:
#!/usr/bin/python from scapy.all import * stars = lambda n: "*" * n def GET_print(packet): return "\n".join(( stars(40) + "GET PACKET" + stars(40), "\n".join(packet.sprintf("{Raw:%Raw.load%}").split(r"\r\n")), stars(90))) sniff( iface='eth0', prn=GET_print, lfilter=lambda p: "GET" in str(p), filter="tcp port 80")
source share