Skip to content

Instantly share code, notes, and snippets.

@dcode
Last active June 30, 2023 03:00
Show Gist options
  • Save dcode/9051a4aa7338b6c946d0a2de189912ec to your computer and use it in GitHub Desktop.
Save dcode/9051a4aa7338b6c946d0a2de189912ec to your computer and use it in GitHub Desktop.
This is a hack that I put together to pull PCAP from multiple instances of stenographer, each with a different configuration file. It adds `mergecap` as a dependency, which is used to produce the final PCAP, which is then filtered through `tcpdump` as before.
# Copyright 2014 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# stenographer - full packet to disk capture
#
# stenographer is a simple, fast method of writing live packets to disk,
# then requesting those packets after-the-fact for post-hoc analysis.
[Unit]
Description=packet capture to disk
After=syslog.target network.target
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/true
ExecReload=/bin/true
WorkingDirectory=/etc/stenographer
[Install]
WantedBy=multi-user.target
# Copyright 2014 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# stenographer - full packet to disk capture
#
# stenographer is a simple, fast method of writing live packets to disk,
# then requesting those packets after-the-fact for post-hoc analysis.
[Unit]
Description=packet capture to disk
After=network.target
PartOf=stenographer.service
ReloadPropagatedFrom=stenographer.service
[Service]
User=stenographer
Group=stenographer
SyslogIdentifier=stenographer
LimitFSIZE=4294967296
LimitNOFILE=1000000
ExecStart=/usr/bin/stenographer -config /etc/stenographer/config.%i
ExecStopPost=/bin/pkill -9 stenotype
[Install]
WantedBy=multi-user.target
#!/bin/bash
# Copyright 2014 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
if [ "$#" -lt 1 ]; then
cat >&2 <<EOF
$0 provides a simple method for reading logs out of stenographer.
Its first argument is the query to send to stenographer, all other arguments
are passed to TCPDump.
Examples:
# Print all packets for source IP 1.1.1.1 without DNS resolution (-n).
$0 'host 1.1.1.1' -n src host 1.1.1.1
# Print all PSH packets between 1.1.1.1 and 2.2.2.2:
$0 'host 1.1.1.1 and host 2.2.2.2' -n 'tcp[tcpflags] & tcp-push != 0'
# Write all packets between 1.1.1.1 and 2.2.2.2 to disk.
'host 1.1.1.1 and host 2.2.2.2' -w /tmp/out.pcap
See README.md for more details on the Stenographer query language.
Set the STENOGRAPHER_CONFIG environmental variable to point to your stenographer
config if it's in a nonstandard place (defaults to /etc/stenographer/config).
$0 arguments are given before the filter. These include:
--limit-bytes X : Stop output once we've exceeded X bytes
--limit-packets X : Stop output once we've exceeded X packets
For example:
# Print first 6 packets or 2K bytes, whichever comes first,
# from source IP 1.1.1.1
$0 --limit-packets 6 --limit-bytes 2048 'host 1.1.1.1'
EOF
exit 1
fi
HEADERS=""
while true; do
case "$1" in
--limit-packets)
HEADERS="$HEADERS --header Steno-Limit-Packets:$2"
shift 2
;;
--limit-bytes)
HEADERS="$HEADERS --header Steno-Limit-Bytes:$2"
shift 2
;;
*)
STENOQUERY="$1"
shift
break
;;
esac
done
TCPDUMP=$(PATH=$PATH:/usr/local/sbin:/usr/sbin:/sbin which tcpdump)
STENOCURL=$(PATH=$(dirname "$0"):$PATH which stenocurl)
STENOGRAPHER_CONFIG_BASE=/etc/stenographer/config.
INTERFACES=$(ls ${STENOGRAPHER_CONFIG_BASE}* | awk -F. '{ print $2 }')
OUT=$(mktemp -d /tmp/stenoread.XXXXXXXXXX) || { echo "Failed to create temp file"; exit 1; }
echo "Running stenographer query '$STENOQUERY', piping to 'tcpdump $@'" >&2
for IFACE in ${INTERFACES[@]}
do
STENOGRAPHER_CONFIG="${STENOGRAPHER_CONFIG_BASE}${IFACE}"
"$STENOCURL" /query \
-d "$STENOQUERY" \
--silent \
--max-time 890 \
--show-error $HEADERS |
"$TCPDUMP" -r /dev/stdin -s 0 -w ${OUT}/${IFACE}.pcap &>/dev/null
done
mergecap -w - ${OUT}/*.pcap | tcpdump -r /dev/stdin -s 0 "$@"
rm -rf ${OUT}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment