Skip to content

Commit 33817e6

Browse files
nabilschearyonghong-song
authored andcommitted
add DNS correlation to connect tracking
1 parent 48946d2 commit 33817e6

8 files changed

+283
-31
lines changed

Dockerfile.tests

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ RUN apt-get update && apt-get install -y \
5050
libtinfo5 \
5151
libtinfo-dev
5252

53-
RUN pip3 install pyroute2 netaddr
54-
RUN pip install pyroute2 netaddr
53+
RUN pip3 install pyroute2 netaddr dnslib cachetools
54+
RUN pip install pyroute2 netaddr dnslib cachetools
5555

5656
# FIXME this is faster than building from source, but it seems there is a bug
5757
# in probing libruby.so rather than ruby binary

Dockerfile.ubuntu

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ COPY --from=builder /root/bcc/*.deb /root/bcc/
2323

2424
RUN \
2525
apt-get update -y && \
26-
DEBIAN_FRONTEND=noninteractive apt-get install -y python python3 binutils libelf1 kmod && \
26+
DEBIAN_FRONTEND=noninteractive apt-get install -y python python3 binutils libelf1 kmod python-dnslib python-cachetools python3-dnslib python3-cachetools && \
2727
dpkg -i /root/bcc/*.deb

INSTALL.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ apt-get -t jessie-backports install linux-base linux-image-4.9.0-0.bpo.2-amd64 l
325325
apt-get install debhelper cmake libllvm3.8 llvm-3.8-dev libclang-3.8-dev \
326326
libelf-dev bison flex libedit-dev clang-format-3.8 python python-netaddr \
327327
python-pyroute2 luajit libluajit-5.1-dev arping iperf netperf ethtool \
328-
devscripts zlib1g-dev libfl-dev
328+
devscripts zlib1g-dev libfl-dev python-dnslib python-cachetools
329329
```
330330

331331
#### Sudo
@@ -419,7 +419,7 @@ popd
419419
```
420420
sudo dnf install -y bison cmake ethtool flex git iperf libstdc++-static \
421421
python-netaddr python-pip gcc gcc-c++ make zlib-devel \
422-
elfutils-libelf-devel
422+
elfutils-libelf-devel python-cachetools
423423
sudo dnf install -y luajit luajit-devel # for Lua support
424424
sudo dnf install -y \
425425
http://repo.iovisor.org/yum/extra/mageia/cauldron/x86_64/netperf-2.7.0-1.mga6.x86_64.rpm
@@ -548,7 +548,7 @@ Tested on Amazon Linux AMI release 2018.03 (kernel 4.14.47-56.37.amzn1.x86_64)
548548
# enable epel to get iperf, luajit, luajit-devel, cmake3 (cmake3 is required to support c++11)
549549
sudo yum-config-manager --enable epel
550550
551-
sudo yum install -y bison cmake3 ethtool flex git iperf libstdc++-static python-netaddr gcc gcc-c++ make zlib-devel elfutils-libelf-devel
551+
sudo yum install -y bison cmake3 ethtool flex git iperf libstdc++-static python-netaddr python-cachetools gcc gcc-c++ make zlib-devel elfutils-libelf-devel
552552
sudo yum install -y luajit luajit-devel
553553
sudo yum install -y http://repo.iovisor.org/yum/extra/mageia/cauldron/x86_64/netperf-2.7.0-1.mga6.x86_64.rpm
554554
sudo pip install pyroute2
@@ -590,7 +590,7 @@ sudo /usr/share/bcc/tools/execsnoop
590590
# enable epel to get iperf, luajit, luajit-devel, cmake3 (cmake3 is required to support c++11)
591591
sudo yum-config-manager --enable epel
592592
593-
sudo yum install -y bison cmake3 ethtool flex git iperf libstdc++-static python-netaddr gcc gcc-c++ make zlib-devel elfutils-libelf-devel
593+
sudo yum install -y bison cmake3 ethtool flex git iperf libstdc++-static python-netaddr python-cachetools gcc gcc-c++ make zlib-devel elfutils-libelf-devel
594594
sudo yum install -y luajit luajit-devel
595595
sudo yum install -y http://repo.iovisor.org/yum/extra/mageia/cauldron/x86_64/netperf-2.7.0-1.mga6.x86_64.rpm
596596
sudo pip install pyroute2

man/man8/tcpconnect.8

+49-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
.SH NAME
33
tcpconnect \- Trace TCP active connections (connect()). Uses Linux eBPF/bcc.
44
.SH SYNOPSIS
5-
.B tcpconnect [\-h] [\-c] [\-t] [\-p PID] [-P PORT] [-u UID] [-U] [\-\-cgroupmap MAPPATH] [\-\-mntnsmap MAPPATH]
5+
.B tcpconnect [\-h] [\-c] [\-t] [\-p PID] [-P PORT] [-u UID] [-U] [\-\-cgroupmap MAPPATH] [\-\-mntnsmap MAPPATH] [\-d]
66
.SH DESCRIPTION
77
This tool traces active TCP connections (eg, via a connect() syscall;
88
accept() are passive connections). This can be useful for general
@@ -14,9 +14,18 @@ This works by tracing the kernel tcp_v4_connect() and tcp_v6_connect() functions
1414
using dynamic tracing, and will need updating to match any changes to these
1515
functions.
1616

17+
When provided with the \-d or \-\-dns option, this tool will also correlate
18+
connect calls with the most recent DNS query that matches the IP connected.
19+
This feature works by tracing the kernel udp_recvmsg() function to collect DNS
20+
responses.
21+
1722
Since this uses BPF, only the root user can use this tool.
1823
.SH REQUIREMENTS
1924
CONFIG_BPF and bcc.
25+
26+
If using the \-d or \-\-dns option, you must have the
27+
dnslib and cachetools python packages installed. You can install them with pip3 or with
28+
apt on Ubuntu 18.04+ using the python3\-dnslib and python3\-cachetools packages.
2029
.SH OPTIONS
2130
.TP
2231
\-h
@@ -45,6 +54,26 @@ Trace cgroups in this BPF map only (filtered in-kernel).
4554
.TP
4655
\--mntnsmap MAPPATH
4756
Trace mount namespaces in this BPF map only (filtered in-kernel).
57+
.TP
58+
\-d
59+
Shows the most recent DNS query for the IP address in the connect call.
60+
This is likely related to the TCP connection details in the other columns, but is not guaranteed.
61+
This
62+
feature works by tracing the udp_recvmsg kernel function and tracking DNS
63+
responses received by the server. It only supports UDP DNS packets up to 512 bytes
64+
in length. The python code keeps a cache of 10k DNS responses in memory
65+
for up 24 hours.
66+
67+
If the time difference in milliseconds
68+
between when the system received a DNS response and when a
69+
connect syscall was traced using an IP in that DNS response is greater than 100ms,
70+
this tool will report this delta after the query.
71+
These deltas should be relatively short for most applications. A
72+
long delay between the response and connect could be either anomalous activity
73+
or indicate a misattribution between the DNS name requested and the IP that
74+
the connect syscall is using.
75+
76+
The \-d option may not be used with the count feature (option \-c)
4877
.SH EXAMPLES
4978
.TP
5079
Trace all active TCP connections:
@@ -55,6 +84,10 @@ Trace all TCP connects, and include timestamps:
5584
#
5685
.B tcpconnect \-t
5786
.TP
87+
Trace all TCP connects, and include most recent matching DNS query for each connected IP
88+
#
89+
.B tcpconnect \-d
90+
.TP
5891
Trace PID 181 only:
5992
#
6093
.B tcpconnect \-p 181
@@ -110,12 +143,27 @@ Destination port
110143
.TP
111144
CONNECTS
112145
Accumulated active connections since start.
146+
.TP
147+
QUERY
148+
Shows the most recent DNS query for the IP address in the connect call.
149+
This is likely related to the TCP connection details in the other columns, but is not guaranteed.
113150
.SH OVERHEAD
114151
This traces the kernel tcp_v[46]_connect functions and prints output for each
115152
event. As the rate of this is generally expected to be low (< 1000/s), the
116153
overhead is also expected to be negligible. If you have an application that
117154
is calling a high rate of connect()s, such as a proxy server, then test and
118155
understand this overhead before use.
156+
157+
If you are using the \-d option to track DNS requests, this tool will trace the
158+
udp_recvmsg function and generate an event for any packets from UDP port 53.
159+
This event contains up to 512 bytes of the UDP packet payload.
160+
Typical applications do not extensively use UDP, so the performance overhead of
161+
tracing udp_recvmsg is
162+
expected to be negligible, However, if you have an application that receives
163+
many UDP packets, then you should test and understand the overhead of tracing
164+
every received UDP message. Furthermore, performance overhead of running
165+
this tool on a DNS server is expected to be higher than average because all
166+
DNS response packets will be copied to userspace.
119167
.SH SOURCE
120168
This is from bcc.
121169
.IP

scripts/bpf_demo.ks.erb

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ kexec-tools
3535
cmake
3636
libstdc++-static
3737
python-netaddr
38+
python-cachetools
3839
python-futures
3940
%end
4041

scripts/build-rpm.sh

+11
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ function cleanup() {
1010
}
1111
trap cleanup EXIT
1212

13+
# install python dependencies for test
14+
if [ -f os-release ]; then
15+
. os-release
16+
fi
17+
if [[ $VERSION_ID -lt 30 ]]; then
18+
PKGS="python3-cachetools python-cachetools"
19+
else
20+
PKGS="python3-cachetools"
21+
fi
22+
sudo dnf install -y $PKGS
23+
1324
mkdir $TMP/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
1425

1526
llvmver=3.7.1

0 commit comments

Comments
 (0)