Coder Social home page Coder Social logo

ebpf-test's Introduction

HTTP Filter

eBPF application that parses HTTP packets and extracts (and prints on screen) the URL contained in the GET/POST request.

eBPF HTTP Filter - Short Presentation

Usage Example

$ sudo python http-parse-complete.py 
GET /pipermail/iovisor-dev/ HTTP/1.1
HTTP/1.1 200 OK
GET /favicon.ico HTTP/1.1
HTTP/1.1 404 Not Found
GET /pipermail/iovisor-dev/2016-January/thread.html HTTP/1.1
HTTP/1.1 200 OK
GET /pipermail/iovisor-dev/2016-January/000046.html HTTP/1.1
HTTP/1.1 200 OK

Implementation overview

The implementation is split in two portions: the former that exploits eBPF code, the latter that performs some additional processing in user space (the python wrapper).

First part: eBPF filter

This component filters IP and TCP packets containing the "HTTP", "GET", "POST" strings in their payload and all subsequent packets belonging to the same session, having the same (ip.src,ip.dst,port.src,port.dst) tuple.

The program is loaded as PROG_TYPE_SOCKET_FILTER and attached to a socket, bind to eth0.

Matching packets are forwarded to user space, the others are dropped by the filter.

Second part: python code in user space

The Python script reads filtered raw packets from the socket, if necessary reassembles packets belonging to the same session, and prints on stdout the first line of the HTTP GET/POST request.

Simple vs. complete

Two versions of this code are available in this repository:

  • simple version: it does not handle URLs that span across multiple packets. For instance, if the URL is too long it shows only the portion contained in the first packet.
  • complete version: it is able to cope with URLs spanning across multiple packets; if such a situation is detected, the code reassembles packets belonging to the same session and prints the complete URL.

How to execute this sample

This sample can be executed by typing either one the two commands below:

$ sudo python http-parse-simple.py
$ sudo python http-parse-complete.py

ebpf-test's People

Contributors

fabiomignini avatar frisso avatar mbertrone avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ebpf-test's Issues

Unable to run code

Steps to reproduce:-

  • Clone the repo.
  • Run sudo http-parse-complete.py
 ➤ sudo python http-parse-complete.py
binding socket to 'eth0'
bpf: Failed to load program: Invalid argument

Traceback (most recent call last):
  File "http-parse-complete.py", line 122, in <module>
    function_http_filter = bpf.load_func("http_filter", BPF.SOCKET_FILTER)
  File "/usr/lib/python2.7/site-packages/bcc/__init__.py", line 379, in load_func
    (func_name, errstr))
Exception: Failed to load BPF program http_filter: Invalid argument

I'm only starting to enter the eBPF world, just did a basic check that http_filter indeed exists in the .c file.

Permission denied

sudo python3 http-parse-complete.py -i enp0s3 ->
Traceback (most recent call last):
File "/home/gil/Desktop/test/eBPF-docker/eBPF/http-parse-complete.py", line 122, in
function_http_filter = bpf.load_func("http_filter", BPF.SOCKET_FILTER)
File "/usr/lib/python3/dist-packages/bcc/init.py", line 411, in load_func
raise Exception("Failed to load BPF program %s: %s" %
Exception: Failed to load BPF program b'http_filter': Permission denied

Simple files do not work on Docker using eth0

As the title describes, running the simple files from a Docker. container does not work and results in the following error:

could not attach XDP program: create link: numerical result out of range

Is there a chance there is a fix you know that my help me so I could check for eth0 on a Docker container?

Replicate

This happened on my MacOS, after running go generate && go build I have called the executable with the eth0 parameter and the error occurred.

If I change the interface to something else, the code runs on the kernel and everything is good:

sudo ./ebpf lo works as expected:

Compiled /ebpf/bpf_bpfel.o
Stripped /ebpf/bpf_bpfel.o
Wrote /ebpf/bpf_bpfel.go
Compiled /ebpf/bpf_bpfeb.o
Stripped /ebpf/bpf_bpfeb.o
Wrote /ebpf/bpf_bpfeb.go
2024/03/11 08:34:20 The chosen interface is lo
2024/03/11 08:34:20 Attached XDP program to iface "lo" (index 1)
2024/03/11 08:34:20 Press Ctrl-C to exit and remove the program
2024/03/11 08:34:21 Map contents:
2024/03/11 08:34:22 Map contents:
2024/03/11 08:34:23 Map contents:
2024/03/11 08:34:24 Map contents:

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.