eCHO
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Help
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Episode 14: netkat with @Itsuugo [YouTube](https://youtu.be/yabzjJMdI08) 16 July 2021 hosted by [Duffie Cooley](https://twitter.com/mauilion) ## Headlines * [Detecting Kernel Hooking using eBPF](https://blog.tofile.dev/2021/07/07/ebpf-hooks.html) * [Facebook Enforcing encryption at scale](https://engineering.fb.com/2021/07/12/security/enforcing-encryption/) * [Halving the TezEdge node’s memory usage with an eBPF-based memory profiler](https://medium.com/tezedge/halving-the-tezedge-nodes-memory-usage-with-an-ebpf-based-memory-profiler-2bfd32f94f69) More context: search for tezedge in https://ebpf.io/blog/ebpf-updates-2020-12 * eBPF Summit on [Twitter](https://twitter.com/ebpfsummit) and [LinkedIn](https://linkedin.com/company/ebpf-summit) ## netkat with [Antonio Ojea](https://twitter.com/itsuugo) ##### Twitter: @itsuugo Github: @aojea netkat is a netcat clone that uses raw sockets to avoid iptables and/or other OS filtering mechanisms. ### Motivation Kubernetes environments have a LOT of iptables, hence the necessity to bypass iptables sometimes, for testing, debugging, troubleshooting, ... ![](https://i.imgur.com/ZwEMrbQ.jpg =400x) ### How it works ``` sudo ./bin/netkat Usage: nk [options] [hostname] [port] -debug Debug -interface string Specify interface to use. Default interface with the route to the specified [hostname] -listen Bind and listen for incoming connections -source-port int Specify source port to use -udp Use UDP instead of default TCP ``` netkat works as the original netcat, but using raw sockets, so it is not affected by the netfilter hooks. A RAW socket receives a copy of all packets, however, we don't need all of them, just the ones used by the netkat connection. On the socket we filter the packets that we don't WANT On ingress we filter the packets that we WANT, so the host doesn't close our connection Netkat can obtain the tuple of the connection in advance, so it can filter using BFP and the socket SO_ATTACH_FILTER option, and the traffic control eBPF filtering capability. The packets received on the socket, are raw packets (with ethernet headers), but since they are bypassing the kernel TCP/IP stack, something need to reassemble the packets and obtain a data stream. This is achieved using an userspace TCP/IP stack https://pkg.go.dev/gvisor.dev/gvisor/pkg/tcpip/stack At high level, the code does: - obtain the connection details: source and destination IP and port, and host interface - parametrize the eBPF code with the connection details, bpf2go generate the eBPF code - inject the BPF code to the raw socket and to the ingress interface - As result, we have a new version of netcat that bypass iptables (needs CAP_NET_RAW) It has a bonus, with the -d flag it has a sniffer XD ``` sudo ./netkat -d -l 127.0.0.1 80 2021/07/16 01:58:08 Creating raw socket 2021/07/16 01:58:08 Adding ebpf ingress filter on interface lo 2021/07/16 01:58:08 filter {LinkIndex: 1, Handle: 0:1, Parent: ffff:fff2, Priority: 0, Protocol: 3} 2021/07/16 01:58:08 Creating user TCP/IP stack 2021/07/16 01:58:08 Listening on 127.0.0.1:80 I0716 01:58:31.206864 22926 sniffer.go:418] recv tcp 127.0.0.1:44644 -> 127.0.0.1:80 len:0 id:a5ee flags: S seqnum: 1321663292 ack: 0 win: 65495 xsum:0xfe30 options: {MSS:65495 WS:7 TS:true TSVal:2715804214 TSEcr:0 SACKPermitted:true} I0716 01:58:31.206937 22926 sniffer.go:418] recv tcp 127.0.0.1:44644 -> 127.0.0.1:80 len:0 id:a5ee flags: S seqnum: 1321663292 ack: 0 win: 65495 xsum:0xfe30 options: {MSS:65495 WS:7 TS:true TSVal:2715804214 TSEcr:0 SACKPermitted:true} I0716 01:58:32.239938 22926 sniffer.go:418] recv tcp 127.0.0.1:44644 -> 127.0.0.1:80 len:0 id:a5ef flags: S seqnum: 1321663292 ack: 0 win: 65495 xsum:0xfe30 options: {MSS:65495 WS:7 TS:true TSVal:2715805247 TSEcr:0 SACKPermitted:true} I0716 01:58:32.240044 22926 sniffer.go:418] recv tcp 127.0.0.1:44644 -> 127.0.0.1:80 len:0 id:a5ef flags: S seqnum: 1321663292 ack: 0 win: 65495 xsum:0xfe30 options: {MSS:65495 WS:7 TS:true TSVal:2715805247 TSEcr:0 SACKPermitted:true} I0716 01:58:34.287962 22926 sniffer.go:418] recv tcp 127.0.0.1:44644 -> 127.0.0.1:80 len:0 id:a5f0 flags: S seqnum: 1321663292 ack: 0 win: 65495 xsum:0xfe30 options: {MSS:65495 WS:7 TS:true TSVal:2715807295 TSEcr:0 SACKPermitted:true} I0716 01:58:34.288096 22926 sniffer.go:418] recv tcp 127.0.0.1:44644 -> 127.0.0.1:80 len:0 id:a5f0 flags: S seqnum: 1321663292 ack: 0 win: 65495 xsum:0xfe30 options: {MSS:65495 WS:7 TS:true TSVal:2715807295 TSEcr:0 SACKPermitted:true} ^C2021/07/16 01:58:35 Exiting: received signal 2021/07/16 01:58:35 Done ``` ![](https://i.imgur.com/jLOApyQ.jpg) ### Tests!! I really like that Antonio wrote tests for this let's check them out. ### Demo: Kubernetes LoadBalancer Services and ExternalTrafficPolicy ![](https://i.imgur.com/S9hMwEh.jpg) 1. Create a multinode cluster with [KIND](https://kind.sigs.k8s.io/) ``` cat <<EOF | kind create cluster --config=- kind: Cluster apiVersion: kind.x-k8s.io/v1alpha4 nodes: - role: control-plane - role: worker - role: worker EOF ``` 2. Run a fake loadbalancer controller ```sh go get -v github.com/aojea/networking-controllers cd $GOPATH/github.com/aojea/networking-controllers cd cmd/loadbalancer go build . kind get kubeconfig > kind.conf ./loadbalancer --kubeconfig kind.conf --iprange "10.111.111.0/24" I0716 02:58:43.535494 15454 controller.go:80] Starting controller fake-loadbalancer-controller I0716 02:58:43.535567 15454 controller.go:84] Waiting for informer caches to sync I0716 02:58:43.535589 15454 shared_informer.go:240] Waiting for caches to sync for fake-loadbalancer-controller I0716 02:58:43.636251 15454 shared_informer.go:247] Caches are synced for fake-loadbalancer-controller I0716 02:58:43.636267 15454 controller.go:90] Starting workers I0716 02:58:43.636285 15454 controller.go:149] Processing sync for service ``` 3. Create a deployment with one pod and expose it with a Loadbalancer ```sh kubectl apply -f https://gist.githubusercontent.com/aojea/369ad6a5d4cbb6b0fbcdd3dd909d9887/raw/0ecd0564c1db4c7103de07accce99da1c7bf91c3/loadbalancer.yaml ``` 4. Obtain the LoadBalancer assigned IP ```sh kubectl get service lb-service -o jsonpath='{.status.loadBalancer.ingress[0].ip}' 10.111.111.129 ``` The loadbalancer has `externalTrafficPolicy: Local` so it should only be able to work if the traffic is directed to the nodes that contains pods belonging to that Service. ```sh kubectl get pods -l app=MyApp -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES test-deployment-6f45696bd5-xlp96 1/1 Running 0 10m 10.244.2.2 kind-worker2 <none> <none> ``` 5. Emulate the external loadbalancer installing a route in the host to the loadbalancer IP through the node with the pod Get the nodes IPs ```sh kubectl get nodes -o wide NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME kind-control-plane Ready control-plane,master 34m v1.21.2 172.18.0.4 <none> Ubuntu 21.04 4.18.0-301.1.el8.x86_64 containerd://1.5.2 kind-worker Ready <none> 33m v1.21.2 172.18.0.3 <none> Ubuntu 21.04 4.18.0-301.1.el8.x86_64 containerd://1.5.2 kind-worker2 Ready <none> 33m v1.21.2 172.18.0.2 <none> Ubuntu 21.04 4.18.0-301.1.el8.x86_64 containerd://1.5.2 ``` > example: pod is in kind-worker2 with ip 172.18.0.2 and loadbalancer IP 10.111.111.129 ```sh sudo ip route add 10.111.111.129 via 172.18.0.2 ``` 6. Check it preserves the source ip ... ```sh echo -e "GET /clientip HTTP/1.1\nhost:myhost\n" | nc 10.111.111.129 80 HTTP/1.1 200 OK Date: Thu, 15 Jul 2021 23:30:35 GMT Content-Length: 16 Content-Type: text/plain; charset=utf-8 172.18.0.1:34028 ``` 7. and that it doesn't work if we target a node without pods backing that service ``` sudo ip route del 10.111.111.129 via 172.18.0.2 sudo ip route add 10.111.111.129 via 172.18.0.3 echo -e "GET /clientip HTTP/1.1\nhost:myhost\n" | nc -v -w 3 10.111.111.129 80 Ncat: Version 7.70 ( https://nmap.org/ncat ) Ncat: Connection timed out. ``` 8. However, if we use `externalTrafficPolicy: Cluster` it works ... but the source IP is not preserved ```sh kubectl patch service lb-service -p '{"spec":{"externalTrafficPolicy":"Cluster"}}' service/lb-service patched $ echo -e "GET /clientip HTTP/1.1\nhost:myhost\n" | nc -v -w 3 10.111.111.129 80 Ncat: Version 7.70 ( https://nmap.org/ncat ) Ncat: Connected to 10.111.111.129:80. HTTP/1.1 200 OK Date: Thu, 15 Jul 2021 23:34:22 GMT Content-Length: 16 Content-Type: text/plain; charset=utf-8 172.18.0.3:14569 Ncat: 36 bytes sent, 133 bytes received in 0.01 seconds. ``` 9. Set `externalTrafficPolicy: Local` again ```sh kubectl patch service lb-service -p '{"spec":{"externalTrafficPolicy":"Local"}}' service/lb-service patched $ echo -e "GET /clientip HTTP/1.1\nhost:myhost\n" | nc -v -w 3 10.111.111.129 80 Ncat: Version 7.70 ( https://nmap.org/ncat ) Ncat: Connection timed out. ``` 10. Test the service from within node without the pod ```sh echo -e "GET /clientip HTTP/1.1\nhost:myhost\n" | nc -v -w 3 10.111.111.129 80 Connection to 10.111.111.129 80 port [tcp/http] succeeded! HTTP/1.1 200 OK Date: Thu, 15 Jul 2021 23:36:50 GMT Content-Length: 16 Content-Type: text/plain; charset=utf-8 ``` We think it shoudn't work because we are in a node without pods, however, since the traffic comes from a node within the cluster the traffic is considered internal and externalTrafficPolicy doesn't apply. The reason is that kube-proxy installs some iptables rules that capture the traffic from within the node. This causes a problem to test LoadBalancers, because it requires traffic to be sent from an external node, something that is not easy: Kubernetes cluster use to be isolated, expose apiserver endpoint e2e should not assume direct connectivity from the e2e binary ### Bypassing iptables with netkat 1. Run netkat in a pod (using hostnetwork) we are going to simulate an external connecting from inside a node ```sh kubectl apply -f https://gist.githubusercontent.com/aojea/952c82a58da625fbd9b8aca35f0e63f1/raw/0c5c625e037eb6431947c93152a3182c47004aa3/netkat.yaml pod/netkat created ``` 2. Check where is running ```sh $ kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES netkat 1/1 Running 0 9s 172.18.0.3 kind-worker <none> <none> test-deployment-6f45696bd5-xlp96 1/1 Running 0 63m 10.244.2.2 kind-worker2 <none> <none> ``` 3. Login to the container ```sh kubectl exec -it netkat ash kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl kubectl exec [POD] -- [COMMAND] instead. / # ip route add 10.111.111.15 via 172.18.0.2 echo -e "GET /clientip HTTP/1.1\nhost:myhost\n" | ./netkat 10.111.111.15 80 2021/07/16 01:06:54 routes {Ifindex: 18 Dst: 10.111.111.15/32 Src: 172.18.0.3 Gw: 172.18.0.2 Flags: [] Table: 254} 2021/07/16 01:06:54 Creating raw socket 2021/07/16 01:06:54 Adding ebpf ingress filter on interface eth0 2021/07/16 01:06:54 filter {LinkIndex: 18, Handle: 0:1, Parent: ffff:fff2, Priority: 0, Protocol: 3} 2021/07/16 01:06:54 Creating user TCP/IP stack 2021/07/16 01:06:54 Dialing ... 2021/07/16 01:06:54 Connection established 2021/07/16 01:06:54 Connection error: <nil> HTTP/1.1 200 OK Date: Fri, 16 Jul 2021 01:06:54 GMT Content-Length: 16 Content-Type: text/plain; charset=utf-8 172.18.0.3:26913 ``` we can see it preserves the source ip 4. Check that if we target a different node it doesn't work as expected because the service has externalTrafficPolicy: local # and it considers the connection as external ```sh / # ip route del 10.111.111.15 via 172.18.0.3 / # ip route add 10.111.111.15 via 172.18.0.4 / # echo -e "GET /clientip HTTP/1.1\nhost:myhost\n" | ./netkat 10.111.111.15 80 2021/07/16 01:11:38 routes {Ifindex: 18 Dst: 10.111.111.15/32 Src: 172.18.0.2 Gw: 172.18.0.4 Flags: [] Table: 254} 2021/07/16 01:11:38 Creating raw socket 2021/07/16 01:11:38 Adding ebpf ingress filter on interface eth0 2021/07/16 01:11:38 filter {LinkIndex: 18, Handle: 0:1, Parent: ffff:fff2, Priority: 0, Protocol: 3} 2021/07/16 01:11:38 Creating user TCP/IP stack 2021/07/16 01:11:38 Dialing ... 2021/07/16 01:11:43 Dialing error: context deadline exceeded 2021/07/16 01:11:43 Can't connect to server: context deadline exceeded ``` ### References: 1. https://en.wikipedia.org/wiki/Netcat 2. https://github.com/aojea/netkat 3. https://man7.org/linux/man-pages/man7/raw.7.html 4. https://man7.org/linux/man-pages/man7/packet.7.html 5. https://man7.org/linux/man-pages/man8/tc-bpf.8.html 6. https://pkg.go.dev/github.com/cilium/ebpf/cmd/bpf2go 7. https://docs.cilium.io/en/stable/bpf/ 8. https://developers.redhat.com/blog/2021/04/01/get-started-with-xdp 9. https://blog.cloudflare.com/tag/ebpf/ 10. https://www.asykim.com/blog/deep-dive-into-kubernetes-external-traffic-policies

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully