ongrep

A cleaned up fork of ngrep for OpenBSD
git clone git://git.sgregoratto.me/ongrep
Log | Files | Refs | README | LICENSE

commit f2a1f8b09194a8ab87787c1fdb60b968f5957c05
parent 277ea6e45e6b65dcf81cbcb23177f347ace64ead
Author: Jordan Ritter <jpr5@darkridge.com>
Date:   Wed, 21 May 2014 16:33:54 -0700

Add tcpkill/dsniff (-K), a downstream patch from Debian

Diffstat:
MLICENSE.txt | 31+++++++++++++++++++++++++++++++
Mconfigure | 37+++++++++++++++++++++++++++----------
Mconfigure.in | 27+++++++++++++++++++--------
Mngrep.8 | 4++++
Mngrep.c | 30++++++++++++++++++++++++++++--
Atcpkill.c | 88+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atcpkill.h | 7+++++++
7 files changed, 204 insertions(+), 20 deletions(-)

diff --git a/LICENSE.txt b/LICENSE.txt @@ -36,3 +36,34 @@ BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + + +Portions were copied from tcpkill (part of dsniff), which has the +following copyright: + + Copyright (c) 1999, 2000 Dug Song <dugsong@monkey.org> + All rights reserved, all wrongs reversed. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of author may not be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, + INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY + AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/configure b/configure @@ -664,6 +664,7 @@ with_dropprivs_user enable_dropprivs enable_ipv6 enable_pcre +enable_tcpkill with_pcap_includes ' ac_precious_vars='build_alias @@ -1291,6 +1292,7 @@ Optional Features: --disable-dropprivs disable privilege dropping logic --enable-ipv6 enable IPv6 (and ICMPv6) support --enable-pcre use PCRE instead of GNU regex + --enable-tcpkill enable connection killing support Optional Packages: --with-PACKAGE[=ARG] use PACKAGE [ARG=yes] @@ -3424,18 +3426,22 @@ fi - use_regex_impl="$withval" - test $withval = yes && use_regex_impl="int" - if test $use_regex_impl != int -a $use_regex_impl != sys; then - echo "!!! error: unknown regex library ($withval)" - exit ; - fi -], +# Check whether --enable-tcpkill was given. +if test "${enable_tcpkill+set}" = set; then : + enableval=$enable_tcpkill; +# AC_CHECK_LIB(net, libnet_init_packet,,echo !!! error: tcpkill feature enabled but no libnet found; exit) + USE_TCPKILL="1" + EXTRA_OBJS="$EXTRA_OBJS tcpkill.o" + EXTRA_DEFINES="$EXTRA_DEFINES $(libnet-config --defines)" + EXTRA_LIBS="$EXTRA_LIBS $(libnet-config --libs)" + +else + + USE_TCPKILL="0" + +fi - dnl Default when the parameter wasn't specified. - use_regex_impl="int" -) @@ -3847,6 +3853,11 @@ cat >>confdefs.h <<_ACEOF _ACEOF +cat >>confdefs.h <<_ACEOF +#define USE_TCPKILL $USE_TCPKILL +_ACEOF + + cat >>confdefs.h <<_ACEOF #define USE_DROPPRIVS $USE_DROPPRIVS @@ -4111,6 +4122,12 @@ else echo "CONFIG: using GNU regex library" fi +if test "$USE_TCPKILL" = "1"; then + echo "CONFIG: tcpkill feature enabled" +else + echo "CONFIG: tcpkill feature disabled" +fi + echo diff --git a/configure.in b/configure.in @@ -138,17 +138,21 @@ AC_ARG_ENABLE(pcre, AC_SUBST(REGEX_DIR) AC_SUBST(REGEX_OBJS) - use_regex_impl="$withval" - test $withval = yes && use_regex_impl="int" +dnl +dnl tcpkill/libnet support (from Debian patch) +dnl - if test $use_regex_impl != int -a $use_regex_impl != sys; then - echo "!!! error: unknown regex library ($withval)" - exit ; - fi +AC_ARG_ENABLE(tcpkill, +[ --enable-tcpkill enable connection killing support], +[ +# AC_CHECK_LIB(net, libnet_init_packet,,echo !!! error: tcpkill feature enabled but no libnet found; exit) + USE_TCPKILL="1" + EXTRA_OBJS="$EXTRA_OBJS tcpkill.o" + EXTRA_DEFINES="$EXTRA_DEFINES $(libnet-config --defines)" + EXTRA_LIBS="$EXTRA_LIBS $(libnet-config --libs)" ], [ - dnl Default when the parameter wasn't specified. - use_regex_impl="int" + USE_TCPKILL="0" ]) @@ -399,6 +403,7 @@ AC_DEFINE_UNQUOTED(USE_PCAP_RESTART, $USE_PCAP_RESTART, [wheth AC_DEFINE_UNQUOTED(USE_PCRE, $USE_PCRE, [whether to use PCRE (default GNU Regex)]) AC_DEFINE_UNQUOTED(USE_IPv6, $USE_IPv6, [whether to use IPv6 (default off)]) +AC_DEFINE_UNQUOTED(USE_TCPKILL, $USE_TCPKILL, [whether to enable tcpkill functionality (default off)]) AC_DEFINE_UNQUOTED(USE_DROPPRIVS, $USE_DROPPRIVS, [whether to use privileges dropping (default yes)]) AC_DEFINE_UNQUOTED(DROPPRIVS_USER, "$DROPPRIVS_USER", [pseudo-user for running ngrep (default "nobody")]) @@ -474,6 +479,12 @@ else echo "CONFIG: using GNU regex library" fi +if test "$USE_TCPKILL" = "1"; then + echo "CONFIG: tcpkill feature enabled" +else + echo "CONFIG: tcpkill feature disabled" +fi + dnl dnl And we're done. ALL YOUR BASE. Don't forget. dnl diff --git a/ngrep.8 b/ngrep.8 @@ -142,6 +142,10 @@ command-line. Specify an alternate character to signify non-printable characters when displayed. The default is ``.''. +.IP "-K num" +Kill matching TCP connections (like tcpkill). The numeric argument +controls how many RST segments are sent. + .IP "-W normal|byline|single|none" Specify an alternate manner for displaying packets, when not in hexadecimal mode. The ``byline'' mode honors embedded linefeeds, diff --git a/ngrep.c b/ngrep.c @@ -110,6 +110,9 @@ static char rcsver[] = "$Revision$"; uint16_t snaplen = 65535, limitlen = 65535, promisc = 1, to = 100; uint16_t match_after = 0, keep_matching = 0, matches = 0, max_matches = 0; +#if USE_TCPKILL +uint16_t tcpkill_active = 0; +#endif uint8_t re_match_word = 0, re_ignore_case = 0, re_multiline_match = 1; uint8_t show_empty = 0, show_hex = 0, show_proto = 0, quiet = 0; @@ -209,7 +212,7 @@ int main(int argc, char **argv) { } #endif - while ((c = getopt(argc, argv, "LNhXViwqpevxlDtTRMCs:n:c:d:A:I:O:S:P:F:W:")) != EOF) { + while ((c = getopt(argc, argv, "LNhXViwqpevxlDtTRMK:Cs:n:c:d:A:I:O:S:P:F:W:")) != EOF) { switch (c) { case 'W': { if (!strcasecmp(optarg, "normal")) @@ -332,6 +335,11 @@ int main(int argc, char **argv) { case 'N': show_proto++; break; +#if USE_TCPKILL + case 'K': + tcpkill_active = atoi(optarg); + break; +#endif case 'h': usage(0); default: @@ -347,6 +355,11 @@ int main(int argc, char **argv) { if (argv[optind]) match_data = argv[optind++]; +#if USE_TCPKILL + if (tcpkill_active) + tcpkill_init(); +#endif + if (read_file) { if (!(pd = pcap_open_offline(read_file, pc_err))) { @@ -916,6 +929,11 @@ void dump_packet(struct pcap_pkthdr *h, u_char *p, uint8_t proto, unsigned char if (pd_dump) pcap_dump((u_char*)pd_dump, h, p); + +#if USE_TCPKILL + if (tcpkill_active) + tcpkill_kill(h, p, link_offset, tcpkill_active); +#endif } int8_t re_match_func(unsigned char *data, uint32_t len, uint16_t *mindex, uint16_t *msize) { @@ -1340,7 +1358,12 @@ void usage(int8_t e) { #endif "hNXViwqpevxlDtTRM> <-IO pcap_dump> <-n num> <-d dev> <-A num>\n" " <-s snaplen> <-S limitlen> <-W normal|byline|single|none> <-c cols>\n" - " <-P char> <-F file> <match expression> <bpf filter>\n" + " <-P char> <-F file>" +#if USE_TCPKILL + " <-K count>" +#endif + "\n" + " <match expression> <bpf filter>\n" " -h is help/usage\n" " -V is version information\n" " -q is be quiet (don't print packet reception hash marks)\n" @@ -1375,6 +1398,9 @@ void usage(int8_t e) { #else " -d is use specified device instead of the pcap default\n" #endif +#if USE_TCPKILL + " -K is send N packets to kill observed connections\n" +#endif ""); exit(e); diff --git a/tcpkill.c b/tcpkill.c @@ -0,0 +1,88 @@ +/* + * tcpkill.c + * + * Kill TCP connections already in progress. + * + * Copyright (c) 2000 Dug Song <dugsong@monkey.org> + * + * $Id: tcpkill.c,v 1.17 2001/03/17 08:10:43 dugsong Exp $ + */ + +#include <sys/types.h> + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> +#include <libnet.h> +#include <pcap.h> + +#include "tcpkill.h" + +libnet_t *l; + +void +tcpkill_kill(const struct pcap_pkthdr *pcap, const u_char *pkt, + unsigned pcap_off, unsigned kill_count) +{ + struct libnet_ipv4_hdr *ip; + struct libnet_tcp_hdr *tcp; + u_char ctext[64]; + u_int32_t seq, win; + int i, len; + + pkt += pcap_off; + len = pcap->caplen - pcap_off; + + ip = (struct libnet_ipv4_hdr *)pkt; + if (ip->ip_p != IPPROTO_TCP) + return; + + tcp = (struct libnet_tcp_hdr *)(pkt + (ip->ip_hl << 2)); + if (tcp->th_flags & (TH_SYN|TH_FIN|TH_RST)) + return; + + seq = ntohl(tcp->th_ack); + win = ntohs(tcp->th_win); + + snprintf(ctext, sizeof(ctext), "%s:%d > %s:%d:", + libnet_addr2name4(ip->ip_src.s_addr, LIBNET_DONT_RESOLVE), + ntohs(tcp->th_sport), + libnet_addr2name4(ip->ip_dst.s_addr, LIBNET_DONT_RESOLVE), + ntohs(tcp->th_dport)); + + for (i = 0; i < kill_count; i++) { + seq += (i * win); + + libnet_clear_packet(l); + + libnet_build_tcp(ntohs(tcp->th_dport), ntohs(tcp->th_sport), + seq, 0, TH_RST, 0, 0, 0, LIBNET_TCP_H, + NULL, 0, l, 0); + + libnet_build_ipv4(LIBNET_IPV4_H + LIBNET_TCP_H, 0, + libnet_get_prand(LIBNET_PRu16), 0, 64, + IPPROTO_TCP, 0, ip->ip_dst.s_addr, + ip->ip_src.s_addr, NULL, 0, l, 0); + + if (libnet_write(l) < 0) + warn("write"); + + fprintf(stderr, "%s R %lu:%lu(0) win 0\n", ctext, seq, seq); + } +} + +void +tcpkill_init(void) +{ + char *intf, ebuf[PCAP_ERRBUF_SIZE]; + char libnet_ebuf[LIBNET_ERRBUF_SIZE]; + + if ((intf = pcap_lookupdev(ebuf)) == NULL) + errx(1, "%s", ebuf); + + if ((l = libnet_init(LIBNET_RAW4, intf, libnet_ebuf)) == NULL) + errx(1, "couldn't initialize sending"); + + libnet_seed_prand(l); +} diff --git a/tcpkill.h b/tcpkill.h @@ -0,0 +1,7 @@ +#ifndef TCPKILL_H +#define TCPKILL_H + +void tcpkill_init(void); +void tcpkill_kill(const struct pcap_pkthdr *pcap, const u_char *pkt, unsigned pcap_off, unsigned kill_count); + +#endif