Skip to content

Commit 595103b

Browse files
authored
Merge pull request #444 from anarkiwi/bfaster
reduce size of ncapture container, use editcap to trim/validate pcap before tshark reads it.
2 parents 8825f4c + 006ab2a commit 595103b

File tree

6 files changed

+55
-51
lines changed

6 files changed

+55
-51
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ jobs:
2020
find . -name requirements.txt -type f -exec pip3 install -r {} \; && \
2121
export PATH=/home/runner/.local/bin:$PATH && \
2222
make test && \
23-
coverage report && coverage xml && \
24-
cd network_tap/ncapture && sudo ./test_ncapture.sh
23+
coverage report && coverage xml
2524
- name: Upload coverage to Codecov
2625
uses: codecov/[email protected]
2726
if: github.repository == 'iqtlabs/network-tools'

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
test:
2-
PYTHONPATH=network_tools_lib python3 -m pytest -l -s -v --cov=. --cov-report term-missing
2+
RESULT_PATH=/dev/null PYTHONPATH=network_tools_lib python3 -m pytest -l -s -v --cov=. --cov-report term-missing
33
# TODO: complete pytype coverage for pcap_stats
44
PYTHONPATH=network_tools_lib pytype -k --exclude=pcap_stats .
5+
./network_tap/ncapture/test_ncapture.sh

network_tap/ncapture/Dockerfile

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,40 @@
1-
FROM alpine:3.14
1+
FROM alpine:3.14 as checkout
22
LABEL maintainer="Charlie Lewis <[email protected]>"
33

4-
ENV BUILDDEPS="autoconf automake bison build-base flex gcc git libtool libpcap-dev libpcap linux-headers musl-dev python3-dev yaml-dev"
5-
ENV PYTHONPATH=/tmp/network_tools_lib
6-
WORKDIR /tmp
7-
8-
# TODO: libwdcap currently requires openssl 1.0.2
9-
RUN apk add --update $BUILDDEPS \
10-
bash \
11-
coreutils \
12-
python3 \
13-
py3-pip \
14-
&& rm -rf /var/cache/apk/* \
15-
&& mkdir /src \
16-
&& cd /src \
17-
&& git clone https://github.com/wanduow/wandio.git -b 4.2.3-1 \
4+
RUN apk add --update git
5+
WORKDIR /src
6+
RUN git clone https://github.com/wanduow/wandio.git -b 4.2.3-1 \
187
&& git clone https://github.com/LibtraceTeam/libtrace.git -b 4.0.16-1 \
198
&& git clone https://github.com/openssl/openssl -b OpenSSL_1_0_2s \
20-
&& git clone https://github.com/wanduow/libwdcap.git \
21-
&& cd /src/wandio \
22-
&& ./bootstrap.sh \
23-
&& ./configure \
24-
&& make && make install \
25-
&& cd /src/libtrace \
26-
&& ./bootstrap.sh \
27-
&& ./configure \
28-
&& make && make install \
29-
&& cd /src/openssl \
30-
&& ./config --prefix=/usr/local --openssldir=/usr/local/openssl \
31-
&& make && make install \
32-
&& cd /src/libwdcap \
33-
&& ./bootstrap.sh \
34-
&& ./configure --disable-shared \
35-
&& make && make install \
36-
&& cd examples \
37-
&& g++ -fpermissive -o tracecapd tracecapd.c -L/usr/local/lib -Wl,-Bstatic -ltrace -lwdcap -Wl,-Bdynamic -lpcap -lssl -lcrypto -lwandio -lyaml \
38-
&& cp tracecapd /usr/local/bin \
39-
&& rm -rf /src \
40-
&& apk del $BUILDDEPS \
41-
&& apk add \
42-
libstdc++ \
43-
libgcc \
44-
libpcap \
45-
yaml
9+
&& git clone https://github.com/wanduow/libwdcap.git
10+
11+
FROM alpine:3.14
12+
COPY --from=checkout /src /src
13+
WORKDIR /src
4614

15+
# TODO: libwdcap currently requires openssl 1.0.2
16+
RUN apk add --update autoconf automake bison build-base flex gcc libtool libpcap-dev libpcap linux-headers musl-dev yaml-dev
17+
18+
WORKDIR /src/openssl
19+
RUN ./config --prefix=/usr/local --openssldir=/usr/local/openssl && MAKEFLAGS=--quiet make -j "$(nproc)" && make install_sw
20+
WORKDIR /src/wandio
21+
RUN ./bootstrap.sh && ./configure && make && make install
22+
WORKDIR /src/libtrace
23+
RUN ./bootstrap.sh && ./configure && make && make install
24+
WORKDIR /src/libwdcap
25+
RUN ./bootstrap.sh && ./configure --disable-shared && make && make install
26+
WORKDIR /src/libwdcap/examples
27+
RUN g++ -fpermissive -o tracecapd tracecapd.c -L/usr/local/lib -Wl,-Bstatic -ltrace -lwdcap -Wl,-Bdynamic -lpcap -lssl -lcrypto -lwandio -lyaml && cp tracecapd /usr/local/bin
28+
29+
WORKDIR /tmp
4730
VOLUME /tmp
31+
4832
COPY network_tap/ncapture/ /tmp
4933
COPY network_tools_lib /tmp/network_tools_lib
5034

35+
RUN apk add --update bash coreutils python3 py3-pip
5136
RUN pip3 install --no-cache-dir -r requirements.txt
37+
RUN ldd /usr/local/bin/tracecapd
5238

39+
ENV PYTHONPATH=/tmp/network_tools_lib
5340
CMD ["/tmp/run.sh"]

network_tap/ncapture/test_ncapture.sh

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
#!/bin/bash
22

33
# smoke test for ncapture worker
4-
# requires tcpdump and tshark to be installed.
54

65
URI=lo
76
IP=127.0.0.1
87
SIZE=1000
98
MAXCAPLEN=50
109

11-
TMPDIR=$(mktemp -d)
10+
sudo apt-get update && sudo apt-get install tcpdump tshark
11+
docker build -f network_tap/ncapture/Dockerfile . -t iqtlabs/ncapture || exit 1
1212

13-
docker build -f Dockerfile . -t iqtlabs/ncapture
13+
TMPDIR=$(mktemp -d)
1414
echo starting ncapture
1515
docker run --privileged --net=host --cap-add=NET_ADMIN -v $TMPDIR:/files -t iqtlabs/ncapture /tmp/run.sh $URI 15 test 1 "host $IP and icmp" "" -d 12 -s 4 -a none -c none -o /files/ || exit 1 &
1616
echo waiting for pcap
1717
PINGS=0
1818
while [ "$(find $TMPDIR -prune -empty)" ] ; do
1919
((++PINGS))
20-
ping -q -n -i 0.1 -s $SIZE -c 10 $IP > /dev/null
20+
# need sudo for low interval
21+
sudo ping -q -n -i 0.1 -s $SIZE -c 10 $IP > /dev/null
2122
echo -n .$PINGS
2223
if [ "$PINGS" -gt "60" ] ; then
2324
echo timed out waiting for pcap

p0f/app.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ def parse_eth(packet):
4242

4343
def run_tshark(path):
4444
addresses = set()
45-
with pyshark.FileCapture(path, include_raw=False, keep_packets=False, debug=True,
46-
custom_parameters=['-o', 'tcp.desegment_tcp_streams:false',
47-
'-n', '-j', 'eth ip ipv6']) as cap: # disable DNS, eth/IP only.
45+
with pyshark.FileCapture(
46+
path, include_raw=False, keep_packets=False, debug=True,
47+
custom_parameters=[
48+
'-o', 'tcp.desegment_tcp_streams:false', '-n'], # disable DNS
49+
tshark_path=os.path.join(os.path.dirname(__file__), 'tsharkwrapper.sh')) as cap:
4850
for packet in cap:
4951
src_eth_address, dst_eth_address = parse_eth(packet)
5052
src_address, dst_address = parse_ip(packet)

p0f/tsharkwrapper.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
# tshark does not respect -s when reading from a capture, so provide
3+
# a wrapper to cut down the capture to IP/header only, which also
4+
# protects tshark's parser.
5+
# bash-ism to retrieve last item in arg list (called by pyshark, is the pcap name)
6+
pcap="${@: -1}"
7+
if [[ ! -f "$pcap" ]] ; then
8+
echo last arg must exist and be input pcap.
9+
exit 1
10+
fi
11+
# bash-ism to drop last arg (pcap name)
12+
set -- "${@:1:$#-1}"
13+
# pass remaining pyshark args to tshark, which will end with "-r -".
14+
editcap -F pcap $pcap -s 128 - | tshark $* \-

0 commit comments

Comments
 (0)