|
| 1 | +# Live Capture Library Example |
| 2 | + |
| 3 | +This is an example of using the Suricata library to capture live |
| 4 | +traffic from a network interface with custom packet handling and |
| 5 | +threading. |
| 6 | + |
| 7 | +## Building In Tree |
| 8 | + |
| 9 | +The Suricata build system has created a Makefile that should allow you |
| 10 | +to build this application in-tree on most supported platforms. To |
| 11 | +build simply run: |
| 12 | + |
| 13 | +``` |
| 14 | +make |
| 15 | +``` |
| 16 | + |
| 17 | +## Running |
| 18 | + |
| 19 | +``` |
| 20 | +./live -i eth0 -l . |
| 21 | +``` |
| 22 | + |
| 23 | +This example requires at least one `-i` option to specify the network |
| 24 | +interface to capture from. You can specify multiple interfaces to |
| 25 | +capture from multiple sources simultaneously - a separate worker thread |
| 26 | +will be created for each interface: |
| 27 | + |
| 28 | +``` |
| 29 | +./live -i eth0 -i eth1 -l . |
| 30 | +``` |
| 31 | + |
| 32 | +Any additional arguments are passed directly to Suricata as command |
| 33 | +line arguments. |
| 34 | + |
| 35 | +**Note:** Live packet capture typically requires root privileges or |
| 36 | +appropriate capabilities (e.g., `CAP_NET_RAW` on Linux). |
| 37 | + |
| 38 | +Example with common options: |
| 39 | +``` |
| 40 | +sudo ./live -i eth0 -- -l . -S rules.rules |
| 41 | +``` |
| 42 | + |
| 43 | +Example capturing from multiple interfaces: |
| 44 | +``` |
| 45 | +sudo ./live -i eth0 -i wlan0 -- -l . -S rules.rules |
| 46 | +``` |
| 47 | + |
| 48 | +To apply a BPF filter (e.g. only TCP traffic) pass it after Suricata options: |
| 49 | +``` |
| 50 | +sudo ./live -i eth0 -- -l . -S rules.rules tcp |
| 51 | +``` |
| 52 | +Multiple interface example with BPF: |
| 53 | +``` |
| 54 | +sudo ./live -i eth0 -i wlan0 -- -l . -S rules.rules tcp port 80 |
| 55 | +``` |
| 56 | +Note: Only specify `-i` interfaces before `--`. Options after `--` are passed |
| 57 | +to Suricata (e.g., `-l`, `-S`). BPF terms at the end are combined into a filter |
| 58 | +string. |
| 59 | + |
| 60 | +Shutdown: each worker thread may call EngineStop when its capture ends; the |
| 61 | +main loop waits for this signal, performs SuricataShutdown concurrently with |
| 62 | +per-thread SCTmThreadsSlotPacketLoopFinish, then joins all worker threads |
| 63 | +before GlobalsDestroy. |
| 64 | + |
| 65 | +The example supports up to 16 interfaces simultaneously. |
| 66 | + |
| 67 | +## Building Out of Tree |
| 68 | + |
| 69 | +A Makefile.example has also been generated to use as an example on how |
| 70 | +to build against the library in a standalone application. |
| 71 | + |
| 72 | +First build and install the Suricata library including: |
| 73 | + |
| 74 | +``` |
| 75 | +make install-library |
| 76 | +make install-headers |
| 77 | +``` |
| 78 | + |
| 79 | +Then run: |
| 80 | + |
| 81 | +``` |
| 82 | +make -f Makefile.example |
| 83 | +``` |
| 84 | + |
| 85 | +If you installed to a non-standard location, you need to ensure that |
| 86 | +`libsuricata-config` is in your path, for example: |
| 87 | + |
| 88 | +``` |
| 89 | +PATH=/opt/suricata/bin:$PATH make -f Makefile.example |
| 90 | +``` |
0 commit comments