Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Kernel Recipes 2019 - Suricata and XDP

215 views

Published on

Suricata is a network threat detection engine using network packets capture to reconstruct the traffic till the application layer and find threats on the network using rules that define behavior to detect. This task is really CPU intensive and discarding non interesting traffic is a solution to enable a scaling of Suricata to 40gbps and other.

This talk will present the latest evolution of Suricata that knows uses eBPF and XDP to bypass traffic. Suricata 5.0 is supporting the hardware XDP to provide ypass with network card such as Netronome. It also takes advantage of pinned maps to get persistance of the bypassed flows. This talk will cover the different usage of XDP and eBPF in Suricata and shows how it impact performance and usability. If development time permit, the talk will also cover AF_XDP and the impact on this new capture method on Suricata.
Eric Leblond

Published in: Software
  • Be the first to comment

  • Be the first to like this

Kernel Recipes 2019 - Suricata and XDP

  1. 1. Suricata and XDP É. Leblond Stamus Networks September 27, 2019 É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 1 / 38
  2. 2. About me Eric Leblond a.k.a Regit Network security expert Netfilter core team Suricata developer: In charge of packet acquisition Co-founder of Stamus Networks, a company providing Suricata based appliances. @Regiteric on Twitter (#sorry) Legendometer É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 2 / 38
  3. 3. About me Eric Leblond a.k.a Regit Network security expert Netfilter core team Suricata developer: In charge of packet acquisition Co-founder of Stamus Networks, a company providing Suricata based appliances. @Regiteric on Twitter (#sorry) Legendometer (No log scale) É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 2 / 38
  4. 4. What about Kernel Recipes logo ? É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 3 / 38
  5. 5. What about Kernel Recipes logo ? É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 3 / 38
  6. 6. What about Kernel Recipes logo ? É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 3 / 38
  7. 7. What about Kernel Recipes logo ? É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 3 / 38
  8. 8. What is Suricata ? IDS and IPS engine Get it here: http://www.suricata-ids.org Open Source (GPLv2) Initially publicly funded, now funded by consortium members Run by Open Information Security Foundation (OISF) More information about OISF at http://www.openinfosecfoundation.org/ É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 4 / 38
  9. 9. Suricata Ecosystem (example) É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 5 / 38
  10. 10. Suricata key points Suricata Protocol analysis Port inde- pendant detection Metadata extraction Protocols HTTP SMTP TLS DNS SMB NFS Intrusion detection Signatures Protocol keywords Multi step attacks Lua for advance analysis ArchitectureOutput JSON Redis Syslog Prelude IDSpcap afpacket netmap IPS Netfilter afpacket ipfw É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 6 / 38
  11. 11. Suricata application layer analysis Suricata analysis Network interface gets copy of traffic Aggregated RX and TX of sniffed interface Reconstruct flow stream as target host Decode application layer Extract file (optional) É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 7 / 38
  12. 12. Suricata EVE JSON event É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 8 / 38
  13. 13. Packet loss drama (1/2) Suricata as a passive sniffer Work on traffic duplication No influence retransmission No influence on bandwitdth throttling Need to minimize packet loss Accuracy of reconstruction drop when packet are lost Packets drop means Missed IDS alerts Missed file extraction É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 9 / 38
  14. 14. Packet loss drama (2/2) Some numbers 10% missed alerts with 3% packets loss 50% failed file extraction with 5.5% packets loss É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 10 / 38
  15. 15. Suricata load balancing É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 11 / 38
  16. 16. 1 Bypass support in Suricata 2 XDP bypass 3 XDP in hardware mode 4 Other usages of XDP 5 AF_XDP 6 Challenge of XDP for a generic software É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 11 / 38
  17. 17. The big flow problem: elephant flow É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 12 / 38
  18. 18. The big flow problem: elephant flow É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 12 / 38
  19. 19. The big flow problem: elephant flow É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 12 / 38
  20. 20. The big flow problem Ring buffer overrun Limited sized ring buffer Overrun cause packets loss that cause streaming malfunction Ring size increase Work around Use memory Fail for non burst Dequeue at N Queue at speed N+M É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 13 / 38
  21. 21. Introducing bypass Stop packet handling as soon as possible Tag flow as bypassed Maintain table of bypassed flows Discard packet if part of a bypassed flow Bypass method Local bypass: Suricata discard packet after decoding Capture bypass: capture method maintain flow table and discard packets of bypassed flows É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 14 / 38
  22. 22. Bypassing big flow: local bypass É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 15 / 38
  23. 23. Bypassing big flow: capture bypass É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 16 / 38
  24. 24. Bypassing big flow: capture bypass É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 16 / 38
  25. 25. Stream depth bypass Attacks characteristic In most cases attack is done at start of TCP session Generation of requests prior to attack is not common Multiple requests are often not even possible on same TCP session Stream reassembly depth Reassembly is done till stream.reassembly.depth bytes. Stream is not analyzed once limit is reached Activating stream depth bypass Set stream.bypass to yes in YAML É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 17 / 38
  26. 26. Selective bypass Ignore some traffic Ignore intensive traffic like Netflix Can be done independently of stream depth Can be done using generic or custom signatures É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 18 / 38
  27. 27. Selective bypass Ignore some traffic Ignore intensive traffic like Netflix Can be done independently of stream depth Can be done using generic or custom signatures The bypass keyword A new bypass signature keyword Trigger bypass when signature match Example of signature pass http any any −> any any ( content : " suricata . io " ; http_host ; bypass ; sid :6666; rev : 1 ; ) É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 18 / 38
  28. 28. Bypass: a long running story Suricata 3.2.1 (Feb. 2017) Suricata bypass API NFQ implementation Suricata 4.1 (Nov. 2018) Pfring HW bypass for Accolade NIC (Alfredo Cardigliano) AF_PACKET eBPF socket filtering bypass AF_PACKET XDP bypass Suricata 5.0 (Oct. 2019) Netronome hardware bypass Tunnel decapsulation Pattern based bypass for TLS É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 19 / 38
  29. 29. 1 Bypass support in Suricata 2 XDP bypass 3 XDP in hardware mode 4 Other usages of XDP 5 AF_XDP 6 Challenge of XDP for a generic software É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 19 / 38
  30. 30. Implementation Libbpf based Suricata loads and install the eBPF filter Set up the maps and pinned them if asked Principle Flow table maps in eBPF eBPF filter drop packet belonging to the flow in the flow table Suricata maintains the flow table maps É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 20 / 38
  31. 31. Flow table maintenance (Suricata 4.1) eBPF update Flow table Pass packet if not in a bypassed flow Update the last seen timestamp and do accounting Flow table dump 1 Suricata iterate on Flow table 2 Check entry with expired timeout 3 Remove them for the Flow table É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 21 / 38
  32. 32. Polling limitation This is slow 2 syscall per item Up to 30 seconds to dump a 300000 entries table And we need big table Accounting dead flow Long bypassed flow get accounted at expiration Wrong performance stats Estimating bypass efficiency with flow data fails É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 22 / 38
  33. 33. 1 Bypass support in Suricata 2 XDP bypass 3 XDP in hardware mode 4 Other usages of XDP 5 AF_XDP 6 Challenge of XDP for a generic software É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 22 / 38
  34. 34. XDP in hardware mode with Netronome cards Netronome card can run XDP eBPF code eBPF bytecode is loaded by the card Maps are available A true offloading Usage A specific function call in libbpf at eBPF installation That’s all. If hardware support the code É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 23 / 38
  35. 35. Hardware constraints Costly time function Netronome NIC CPUs get time via kernel Costly to get it to update last seen We need an algorithm update Some minor constraints Limited key+value size: fixed by compressing some fields in the keys No per-cpu maps Some XDP features can’t be offloaded Some make no sense in hardware (CPU redirect) Some are in the roadmap Fixed by #ifdef in the code É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 24 / 38
  36. 36. Flow key compression s t r u c t flowv4_keys { __u32 src ; __u32 dst ; union { __u32 ports ; __u16 port16 [ 2 ] ; } ; − __u32 ip_proto ; − __u16 vlan_id [ 2 ] ; + __u8 ip_proto : 1 ; + __u16 vlan0 :15; + __u16 vlan1 ; } ; É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 25 / 38
  37. 37. Flow key compression s t r u c t flowv4_keys { __u32 src ; __u32 dst ; union { __u32 ports ; __u16 port16 [ 2 ] ; } ; − __u32 ip_proto ; − __u16 vlan_id [ 2 ] ; + __u8 ip_proto : 1 ; + __u16 vlan0 :15; + __u16 vlan1 ; } ; u32 for ip_proto was like É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 25 / 38
  38. 38. Flow timeout logic update Algorithm update Keep Flow in Suricata internal flow table Fetch eBPF flow entries when flow timeout Increase timeout if traffic has been seen Update bypassed counters Benefit Work on Netronome card Avoid stressing system with a full dump Intermediate accounting for flow Exact per-flow accounting of bypassed traffic É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 26 / 38
  39. 39. Netronome RSS load balancing Programmable Receive Side Scaling RSS distributes packets on multiple queues to share load Netronome supports RSS RSS load balancing can be done in eBPF code É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 27 / 38
  40. 40. Netronome RSS load balancing Programmable Receive Side Scaling RSS distributes packets on multiple queues to share load Netronome supports RSS RSS load balancing can be done in eBPF code Code is #KISS /∗ IP−pairs + protocol (UDP/TCP/ICMP) h i t same CPU ∗/ __u32 xdp_hash = tuple . src + tuple . dst ; xdp_hash = SuperFastHash ( ( char ∗) &xdp_hash , 4 , INITVAL + iph−>protocol ) ; ctx −>rx_queue_index = xdp_hash % RSS_QUEUE_NUMBERS; É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 27 / 38
  41. 41. 1 Bypass support in Suricata 2 XDP bypass 3 XDP in hardware mode 4 Other usages of XDP 5 AF_XDP 6 Challenge of XDP for a generic software É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 27 / 38
  42. 42. Improving Suricata restart Feeling like the falling whale in H2G2 Flow taken in the middle can’t be properly analyzed Suricata restart reset the in kernel Flow table Big trouble at restart and bypassed flow striking hard É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 28 / 38
  43. 43. Improving Suricata restart Feeling like the falling whale in H2G2 Flow taken in the middle can’t be properly analyzed Suricata restart reset the in kernel Flow table Big trouble at restart and bypassed flow striking hard Pinned maps for flow table Keep maps between Suricata run Previously bypassed flows are not seen again Suricata is not overwhelmed at restart Flow restoration Bypassed flows kept in the map need to timeout Need to restore the flow from the eBPF map to Suricata É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 28 / 38
  44. 44. Tunnel Decapsulation Minify the elephant Fow reconstruction implies all packets of a flow on a single thread IP transport tunnel reach one single thread Let’s use bpf_xdp_adjust_head nh_off += 4; proto = grhdr−>proto ; /∗ parse GRE protocol to get o f f s e t to s t a r t of inner data ∗/ /∗ . . . some parsing skipped ∗/ i f ( grhdr−>flags & GRE_CSUM) nh_off += 4; i f ( data + nh_off > data_end ) /∗ pass in case of error ∗/ return XDP_PASS; i f ( bpf_xdp_adjust_head ( ctx , 0 + nh_off ) ) /∗ move head of data to inner data ∗/ return XDP_PASS; /∗ pass in case of error ∗/ /∗ continue treatment , data s t a r t i s now inner data of GRE tunnel ∗/ É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 29 / 38
  45. 45. TLS bypass improvement Suricata TLS bypass Can do TLS handshake analysis but nothing to be done on encrypted traffic Suricat triggers bypass when TLS session switch to encrypted Issue due to ring buffer All packets of short living sessions are in ring buffer Bypass is not efficient XDP pattern based bypass i f ( app_data [ 0 ] == 0x17 /∗ TLS 1.2 ∗/ && app_data [ 1 ] == 0x3 && app_data [ 2 ] == 0x3 ) { /∗ and encrypted packet ∗/ tls_count = bpf_map_lookup_elem(& tls_bypass_count , &key1 ) ; i f ( tls_count ) tls_count ++; return XDP_DROP; } É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 30 / 38
  46. 46. 1 Bypass support in Suricata 2 XDP bypass 3 XDP in hardware mode 4 Other usages of XDP 5 AF_XDP 6 Challenge of XDP for a generic software É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 30 / 38
  47. 47. AF_XDP a new raw packet capture method Principle eBPF filter send packet to a shared buffer packet reach userspace Before skb creation In a efficient hole compliant buffer structure Implementation New capture method in Suricata (like AF_PACKET or NFQUEUE) Code using libbpf É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 31 / 38
  48. 48. I’m API with my life Libbpf XSK API High level API helps a lot Setup the complex data structure Start without even an eBPF file Low level API also available Know your hardware issue Bind to a queue Scalibility depends of hardware No CPU based load balancing But do we need that ? É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 32 / 38
  49. 49. Give me some time Initial implementation libbpf is easy to use Suricata part was the most complex Where is my timestamp ? No hardware timestamp available Mandatory in Suricata case We are getting copy of packets Case of splitted RX TX can’t be fixed É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 33 / 38
  50. 50. 1 Bypass support in Suricata 2 XDP bypass 3 XDP in hardware mode 4 Other usages of XDP 5 AF_XDP 6 Challenge of XDP for a generic software É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 33 / 38
  51. 51. Easier now libbpf No de facto standard for eBPF handling in 2015 Had to patch libbpf to get it working libbpf is now available in distribution Kernel side stabilization Less breakage when changing version É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 34 / 38
  52. 52. Packet decoding No commodity decoding Suricata needs to handle all networks case Decoding in eBPF for a lot of common protocols Examples exist but are too simple Would love a decoding library Reusable blocks For main IP layers and layer 2 protocols É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 35 / 38
  53. 53. Dealing with distributions Distributing libbpf Available in Debian (sid with backport) Available Fedora 30 Available in Mageia Shipping eBPF files What if we need to tune feature Possible solutions Use #ifdef and build eBPF file on prod system Need to have compiler on production system Security implication Use (pinned) maps to setup the XDP filter Need some tooling Or code in Suricata É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 36 / 38
  54. 54. Conclusion Suricata and XDP It was a long journey XDP toolkit has improved over time Features and performance are there AF_XDP is promising More information Stamus Networks: https://www.stamus-networks.com/ Suricata and XDP whitepaper: https://tinyurl.com/y6nqhalu Suricata code: https://github.com/oisf/suricata Libbpf code: https://github.com/libbpf/libbpf É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 37 / 38
  55. 55. Questions ? Thanks for their help Alexei Starovoitov Daniel Borkmann Jesper Dangaard Brouer And Netronome Team David Beckett Jakub Kicinski Jiong Wang Contact me Mail: eleblond@stamus- networks.com Twitter: @regiteric More information Suricata: https://www.suricata-ids.org/ Stamus Networks: https://www.stamus-networks.com/ Suricata and XDP whitepaper: https://tinyurl.com/y6nqhalu Suricata code: https://github.com/oisf/suricata Libbpf code: https://github.com/libbpf/libbpf É. Leblond (Stamus Networks) Suricata and XDP September 27, 2019 38 / 38

×