View | Details | Raw Unified | Return to bug 267232
Collapse All | Expand All

(-)b/security/knock/Makefile (-5 / +3 lines)
Lines 1-14 Link Here
1
PORTNAME=	knock
1
PORTNAME=	knock
2
PORTVERSION=	0.5
2
PORTVERSION=	0.8
3
PORTREVISION=	1
4
PORTEPOCH=	1
5
CATEGORIES=	security
3
CATEGORIES=	security
6
MASTER_SITES=	http://www.zeroflux.org/proj/knock/files/ \
4
MASTER_SITES=	https://github.com/jvinet/${PORTNAME}/releases/download/v${PORTVERSION}/ \
7
		LOCAL/sbz
5
		LOCAL/sbz
8
6
9
MAINTAINER=	sbz@FreeBSD.org
7
MAINTAINER=	sbz@FreeBSD.org
10
COMMENT=	Flexible port-knocking server and client
8
COMMENT=	Flexible port-knocking server and client
11
WWW=		http://www.zeroflux.org/projects/knock
9
WWW=		https://github.com/jvinet/knock
12
10
13
LICENSE=	GPLv2
11
LICENSE=	GPLv2
14
LICENSE_FILE=	${WRKSRC}/COPYING
12
LICENSE_FILE=	${WRKSRC}/COPYING
(-)b/security/knock/distinfo (-2 / +3 lines)
Lines 1-2 Link Here
1
SHA256 (knock-0.5.tar.gz) = c4c141166a10c45b139e5e656d0c98bbb99567abb35163863fc1df1a025cde15
1
TIMESTAMP = 1690093412
2
SIZE (knock-0.5.tar.gz) = 83990
2
SHA256 (knock-0.8.tar.gz) = 698d8c965624ea2ecb1e3df4524ed05afe387f6d20ded1e8a231209ad48169c7
3
SIZE (knock-0.8.tar.gz) = 377107
(-)b/security/knock/files/patch-Makefile.in (-20 / +11 lines)
Lines 1-20 Link Here
1
--- ./Makefile.in.orig	2005-06-27 07:11:34.000000000 +0200
1
--- Makefile.in.orig	2023-07-23 19:53:46 UTC
2
+++ ./Makefile.in	2011-08-17 13:19:00.000000000 +0200
2
+++ Makefile.in
3
@@ -15,7 +15,7 @@
3
@@ -864,7 +864,7 @@ distcleancheck: distclean
4
 #
4
 	       exit 1; } >&2
5
 #  You should have received a copy of the GNU General Public License
5
 check-am: all-am
6
 #  along with this program; if not, write to the Free Software
6
 check: check-am
7
-#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 
7
-all-am: Makefile $(PROGRAMS) $(SCRIPTS) $(MANS) $(DATA) config.h
8
+#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
8
+all-am: Makefile $(PROGRAMS) $(SCRIPTS) $(DATA) config.h
9
 #  USA.
9
 installdirs:
10
 #
10
 	for dir in "$(DESTDIR)$(bindir)" "$(DESTDIR)$(sbindir)" "$(DESTDIR)$(sbindir)" "$(DESTDIR)$(man1dir)" "$(DESTDIR)$(docdir)" "$(DESTDIR)$(sysconfdir)"; do \
11
 
11
 	  test -z "$$dir" || $(MKDIR_P) "$$dir"; \
12
@@ -48,7 +48,7 @@
13
 			 $(SRCDIR)knock.c \
14
 			 $(SRCDIR)list.c
15
 
16
-all: knockd knock man
17
+all: knockd knock
18
 
19
 knockd: $(OBJDIR)knockd.o $(OBJDIR)list.o
20
 	$(CXX) $(OBJDIR)knockd.o $(OBJDIR)list.o -o $@ $(LDFLAGS) -lpcap
(-)a/security/knock/files/patch-config.h.in (-89 lines)
Removed Link Here
1
--- ./config.h.in.orig	2004-04-19 09:22:07.000000000 +0200
2
+++ ./config.h.in	2011-08-17 13:19:00.000000000 +0200
3
@@ -3,12 +3,24 @@
4
 /* Define to 1 if you have the <arpa/inet.h> header file. */
5
 #undef HAVE_ARPA_INET_H
6
 
7
+/* Define to 1 if you have the `bzero' function. */
8
+#undef HAVE_BZERO
9
+
10
 /* Define to 1 if you don't have `vprintf' but do have `_doprnt.' */
11
 #undef HAVE_DOPRNT
12
 
13
+/* Define to 1 if you have the <fcntl.h> header file. */
14
+#undef HAVE_FCNTL_H
15
+
16
 /* Define to 1 if you have the `fork' function. */
17
 #undef HAVE_FORK
18
 
19
+/* Define to 1 if you have the `gethostbyaddr' function. */
20
+#undef HAVE_GETHOSTBYADDR
21
+
22
+/* Define to 1 if you have the `gethostbyname' function. */
23
+#undef HAVE_GETHOSTBYNAME
24
+
25
 /* Define to 1 if you have the `inet_ntoa' function. */
26
 #undef HAVE_INET_NTOA
27
 
28
@@ -28,14 +40,21 @@
29
 /* Define to 1 if you have the <memory.h> header file. */
30
 #undef HAVE_MEMORY_H
31
 
32
+/* Define to 1 if you have the `memset' function. */
33
+#undef HAVE_MEMSET
34
+
35
 /* Define to 1 if you have the <netdb.h> header file. */
36
 #undef HAVE_NETDB_H
37
 
38
 /* Define to 1 if you have the <netinet/in.h> header file. */
39
 #undef HAVE_NETINET_IN_H
40
 
41
-/* Define to 1 if you have the <pcap.h> header file. */
42
-#undef HAVE_PCAP_H
43
+/* Define to 1 if your system has a GNU libc compatible `realloc' function,
44
+   and to 0 otherwise. */
45
+#undef HAVE_REALLOC
46
+
47
+/* Define to 1 if you have the `socket' function. */
48
+#undef HAVE_SOCKET
49
 
50
 /* Define to 1 if you have the <stdint.h> header file. */
51
 #undef HAVE_STDINT_H
52
@@ -43,12 +62,15 @@
53
 /* Define to 1 if you have the <stdlib.h> header file. */
54
 #undef HAVE_STDLIB_H
55
 
56
-/* Define to 1 if you have the `strcasecmp' function. */
57
-#undef HAVE_STRCASECMP
58
+/* Define to 1 if you have the `strchr' function. */
59
+#undef HAVE_STRCHR
60
 
61
 /* Define to 1 if you have the `strdup' function. */
62
 #undef HAVE_STRDUP
63
 
64
+/* Define to 1 if you have the `strerror' function. */
65
+#undef HAVE_STRERROR
66
+
67
 /* Define to 1 if you have the <strings.h> header file. */
68
 #undef HAVE_STRINGS_H
69
 
70
@@ -61,6 +83,9 @@
71
 /* Define to 1 if you have the <syslog.h> header file. */
72
 #undef HAVE_SYSLOG_H
73
 
74
+/* Define to 1 if you have the <sys/ioctl.h> header file. */
75
+#undef HAVE_SYS_IOCTL_H
76
+
77
 /* Define to 1 if you have the <sys/socket.h> header file. */
78
 #undef HAVE_SYS_SOCKET_H
79
 
80
@@ -124,6 +149,9 @@
81
 /* Define to `int' if <sys/types.h> does not define. */
82
 #undef pid_t
83
 
84
+/* Define to rpl_realloc if the replacement function should be used. */
85
+#undef realloc
86
+
87
 /* Define to `unsigned' if <sys/types.h> does not define. */
88
 #undef size_t
89
 
(-)a/security/knock/files/patch-doc__knock.1.in (-15 lines)
Removed Link Here
1
--- ./doc/knock.1.in.orig	2005-06-27 07:10:30.000000000 +0200
2
+++ ./doc/knock.1.in	2011-08-17 13:28:24.000000000 +0200
3
@@ -15,6 +15,12 @@
4
 If you want each port to use a different protocol (TCP or UDP), then you
5
 can specify the protocol on a per-port basis.  See the example below.
6
 .TP
7
+.B "\-d <t>, \-\-delay <t>"
8
+Wait <t> milliseconds between each port hit.  This can be used in situations
9
+where a router mistakes your stream of SYN packets as a port scan and blocks
10
+them.  If the packet rate is slowed with --delay, then the router should let
11
+the packets through.
12
+.TP
13
 .B "\-v, \-\-verbose"
14
 Output verbose status messages.
15
 .TP
(-)a/security/knock/files/patch-doc__knockd.1.in (-58 lines)
Removed Link Here
1
--- ./doc/knockd.1.in.orig	2005-06-29 20:45:17.000000000 +0200
2
+++ ./doc/knockd.1.in	2011-08-17 13:29:55.000000000 +0200
3
@@ -60,13 +60,13 @@
4
 	sequence    = 7000,8000,9000
5
 	seq_timeout = 10
6
 	tcpflags    = syn
7
-	command     = /usr/sbin/iptables -A INPUT -s %IP% -j ACCEPT
8
+	command     = /usr/sbin/iptables -A INPUT -s %IP% --dport 22 -j ACCEPT
9
 
10
 [closeSSH]
11
 	sequence    = 9000,8000,7000
12
 	seq_timeout = 10
13
 	tcpflags    = syn
14
-	command     = /usr/sbin/iptables -D INPUT -s %IP% -j ACCEPT
15
+	command     = /usr/sbin/iptables -D INPUT -s %IP% --dport 22 -j ACCEPT
16
 
17
 .fi
18
 .RE
19
@@ -115,7 +115,7 @@
20
 	start_command      = /usr/sbin/iptables -A INPUT -s %IP% -p tcp --dport 25 -j ACCEPT
21
 	cmd_timeout        = 5
22
 	stop_command       = /usr/sbin/iptables -D INPUT -s %IP% -p tcp --dport 25 -j ACCEPT
23
-	
24
+
25
 .fi
26
 .RE
27
 .SH CONFIGURATION: GLOBAL DIRECTIVES
28
@@ -132,7 +132,7 @@
29
 .TP
30
 .B "Interface = <interface_name>"
31
 Network interface to listen on. Only its name has to be given, not the path to
32
-the device (eg, "eth0" and not "/dev/eth0"). Default: eth0.
33
+the device (eg, "fxp0" and not "/dev/fxp0"). Default: fxp0.
34
 .SH CONFIGURATION: KNOCK/EVENT DIRECTIVES
35
 .TP
36
 .B "Sequence = <port1>[:<tcp|udp>][,<port2>[:<tcp|udp>] ...]"
37
@@ -158,7 +158,7 @@
38
 \fBNote\fP: Do not edit the file while knockd is running!
39
 .TP
40
 .B "Seq_Timeout = <timeout>"
41
-Time to wait for a sequence to complete in seconds. If the time elapses
42
+Time to wait (in seconds) for a sequence to complete in seconds. If the time elapses
43
 before the knock is complete, it is discarded.
44
 .TP
45
 .B "TCPFlags = fin|syn|rst|psh|ack|urg"
46
@@ -183,10 +183,10 @@
47
 directive is optional, only required if \fBStop_Command\fP is used.
48
 .TP
49
 .B "Stop_Command = <command>"
50
-Specify the command to be executed when \fBCmd_Timeout\fP seconds have passed 
51
+Specify the command to be executed when \fBCmd_Timeout\fP seconds have passed
52
 since \fBStart_Command\fP has been executed.  All instances of \fB%IP%\fP will
53
 be replaced with the knocker's IP address.  This directive is optional.
54
-.SH SECURITY NOTES 
55
+.SH SECURITY NOTES
56
 Using the \fB-l\fP or \fB--lookup\fP commandline option to resolve DNS names
57
 for log entries may be a security risk!  An attacker may find out the first port
58
 of a sequence if he can monitor the DNS traffic of the host running knockd.
(-)b/security/knock/files/patch-knockd.conf (-3 / +4 lines)
Lines 1-6 Link Here
1
--- ./knockd.conf.orig	2004-05-07 00:56:03.000000000 +0200
1
--- knockd.conf.orig	2023-07-23 19:53:02 UTC
2
+++ ./knockd.conf	2011-08-17 13:19:00.000000000 +0200
2
+++ knockd.conf
3
@@ -1,15 +1,16 @@
3
@@ -1,16 +1,17 @@
4
 [options]
4
 [options]
5
 	logfile = /var/log/knockd.log
5
 	logfile = /var/log/knockd.log
6
+	interface = fxp0
6
+	interface = fxp0
Lines 19-21 Link Here
19
+	command     = /sbin/ipfw -q delete pass proto tcp src-ip %IP% dst-port 22
19
+	command     = /sbin/ipfw -q delete pass proto tcp src-ip %IP% dst-port 22
20
 	tcpflags    = syn
20
 	tcpflags    = syn
21
 
21
 
22
 [openHTTPS]
(-)a/security/knock/files/patch-src__knock.c (-132 lines)
Removed Link Here
1
--- ./src/knock.c.orig	2005-06-27 07:11:34.000000000 +0200
2
+++ ./src/knock.c	2011-08-17 13:21:28.000000000 +0200
3
@@ -1,8 +1,8 @@
4
 /*
5
  *  knock.c
6
- * 
7
+ *
8
  *  Copyright (c) 2004-2005 by Judd Vinet <jvinet@zeroflux.org>
9
- * 
10
+ *
11
  *  This program is free software; you can redistribute it and/or modify
12
  *  it under the terms of the GNU General Public License as published by
13
  *  the Free Software Foundation; either version 2 of the License, or
14
@@ -15,7 +15,7 @@
15
  *
16
  *  You should have received a copy of the GNU General Public License
17
  *  along with this program; if not, write to the Free Software
18
- *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 
19
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20
  *  USA.
21
  */
22
 
23
@@ -23,16 +23,20 @@
24
 #include <stdlib.h>
25
 #include <stdarg.h>
26
 #include <netdb.h>
27
-#include <sys/socket.h>
28
 #include <sys/types.h>
29
+#include <sys/socket.h>
30
+#if defined(__FreeBSD__) || defined(__APPLE__)
31
+#include <netinet/in.h>
32
+#endif
33
 #include <arpa/inet.h>
34
 #include <unistd.h>
35
 #include <string.h>
36
+#include <netinet/in.h>
37
 #include <resolv.h>
38
 #include <getopt.h>
39
 #include <fcntl.h>
40
 
41
-static char version[] = "0.5";
42
+static char version[] = "#VERSION#";
43
 
44
 #define PROTO_TCP 1
45
 #define PROTO_UDP 2
46
@@ -44,6 +48,7 @@
47
 
48
 int o_verbose = 0;
49
 int o_udp     = 0;
50
+int o_delay   = 0;
51
 
52
 int main(int argc, char** argv)
53
 {
54
@@ -55,12 +60,13 @@
55
 	{
56
 		{"verbose",   no_argument,       0, 'v'},
57
 		{"udp",       no_argument,       0, 'u'},
58
+		{"delay",     required_argument, 0, 'd'},
59
 		{"help",      no_argument,       0, 'h'},
60
 		{"version",   no_argument,       0, 'V'},
61
 		{0, 0, 0, 0}
62
 	};
63
 
64
-	while((opt = getopt_long(argc, argv, "vuhV", opts, &optidx))) {
65
+	while((opt = getopt_long(argc, argv, "vud:hV", opts, &optidx))) {
66
 		if(opt < 0) {
67
 			break;
68
 		}
69
@@ -68,6 +74,7 @@
70
 			case 0:   break;
71
 			case 'v': o_verbose = 1; break;
72
 			case 'u': o_udp = 1; break;
73
+			case 'd': o_delay = (int)atoi(optarg); break;
74
 			case 'V': ver();
75
 			case 'h': /* fallthrough */
76
 			default: usage();
77
@@ -77,6 +84,11 @@
78
 		usage();
79
 	}
80
 
81
+	if(o_delay < 0) {
82
+		fprintf(stderr, "error: delay cannot be negative\n");
83
+		exit(1);
84
+	}
85
+
86
 	host = gethostbyname(argv[optind++]);
87
 	if(host == NULL) {
88
 		fprintf(stderr, "Cannot resolve hostname\n");
89
@@ -98,16 +110,16 @@
90
 		} else {
91
 			port = atoi(arg);
92
 		}
93
-		
94
+
95
 		if(o_udp || proto == PROTO_UDP) {
96
-			sd = socket(PF_INET, SOCK_DGRAM, 0); 
97
+			sd = socket(PF_INET, SOCK_DGRAM, 0);
98
 			if(sd == -1) {
99
 				fprintf(stderr, "Cannot open socket\n");
100
 				exit(1);
101
 			}
102
 		} else {
103
 			int flags;
104
-			sd = socket(PF_INET, SOCK_STREAM, 0); 
105
+			sd = socket(PF_INET, SOCK_STREAM, 0);
106
 			if(sd == -1) {
107
 				fprintf(stderr, "Cannot open socket\n");
108
 				exit(1);
109
@@ -121,13 +133,13 @@
110
 		addr.sin_port = htons(port);
111
 		if(o_udp || proto == PROTO_UDP) {
112
 			vprint("hitting udp %s:%u\n", inet_ntoa(addr.sin_addr), port);
113
-			connect(sd, (struct sockaddr*)&addr, sizeof(struct sockaddr));
114
-			send(sd, NULL, 0, MSG_DONTWAIT);
115
+			sendto(sd, "", 1, 0, (struct sockaddr*)&addr, sizeof(addr));
116
 		} else {
117
 			vprint("hitting tcp %s:%u\n", inet_ntoa(addr.sin_addr), port);
118
 			connect(sd, (struct sockaddr*)&addr, sizeof(struct sockaddr));
119
 		}
120
 		close(sd);
121
+		usleep(1000*o_delay);
122
 	}
123
 
124
 	return(0);
125
@@ -148,6 +160,7 @@
126
 	printf("usage: knock [options] <host> <port[:proto]> [port[:proto]] ...\n");
127
 	printf("options:\n");
128
 	printf("  -u, --udp            make all ports hits use UDP (default is TCP)\n");
129
+	printf("  -d, --delay <t>      wait <t> milliseconds between port hits\n");
130
 	printf("  -v, --verbose        be verbose\n");
131
 	printf("  -V, --version        display version\n");
132
 	printf("  -h, --help           this help\n");
(-)a/security/knock/files/patch-src__knockd.c (-520 lines)
Removed Link Here
1
--- ./src/knockd.c.orig	2005-06-27 07:11:34.000000000 +0200
2
+++ ./src/knockd.c	2011-08-17 13:24:16.000000000 +0200
3
@@ -1,8 +1,8 @@
4
 /*
5
  *  knockd.c
6
- * 
7
+ *
8
  *  Copyright (c) 2004-2005 by Judd Vinet <jvinet@zeroflux.org>
9
- * 
10
+ *
11
  *  This program is free software; you can redistribute it and/or modify
12
  *  it under the terms of the GNU General Public License as published by
13
  *  the Free Software Foundation; either version 2 of the License, or
14
@@ -15,7 +15,7 @@
15
  *
16
  *  You should have received a copy of the GNU General Public License
17
  *  along with this program; if not, write to the Free Software
18
- *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 
19
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20
  *  USA.
21
  */
22
 
23
@@ -28,6 +28,11 @@
24
 #include <ctype.h>
25
 #include <string.h>
26
 #include <fcntl.h>
27
+#if defined(__FreeBSD__) || defined(__APPLE__)
28
+#include <limits.h>
29
+#include <sys/socket.h>
30
+#include <netinet/in_systm.h>
31
+#endif
32
 #include <netinet/in.h>
33
 #include <netinet/if_ether.h>
34
 #include <netinet/ip.h>
35
@@ -35,8 +40,9 @@
36
 #include <netinet/udp.h>
37
 #include <netinet/ip_icmp.h>
38
 #include <net/if.h>
39
+#if !defined(__FreeBSD__) && !defined(__APPLE__)
40
 #include <bits/time.h>
41
-#include <sys/socket.h>
42
+#endif
43
 #include <sys/types.h>
44
 #include <sys/wait.h>
45
 #include <sys/ioctl.h>
46
@@ -48,7 +54,7 @@
47
 #include <errno.h>
48
 #include "list.h"
49
 
50
-static char version[] = "0.5";
51
+static char version[] = "#VERSION#";
52
 
53
 #define SEQ_TIMEOUT 25 /* default knock timeout in seconds */
54
 #define CMD_TIMEOUT 10 /* default timeout in seconds between start and stop commands */
55
@@ -131,7 +137,7 @@
56
 int  o_daemon    = 0;
57
 int  o_lookup    = 0;
58
 char o_int[32]           = "";		/* default (eth0) is set after parseconfig() */
59
-char o_cfg[PATH_MAX]     = "/etc/knockd.conf";
60
+char o_cfg[PATH_MAX]     = "%%PREFIX%%/etc/knockd.conf";
61
 char o_pidfile[PATH_MAX] = "/var/run/knockd.pid";
62
 char o_logfile[PATH_MAX] = "";
63
 
64
@@ -193,7 +199,11 @@
65
 		}
66
 	}
67
 
68
+#if defined(__FreeBSD__) || defined(__APPLE__)
69
+	cap = pcap_open_live(o_int, 65535, 0, 1, pcapErr);
70
+#else
71
 	cap = pcap_open_live(o_int, 65535, 0, 0, pcapErr);
72
+#endif
73
 	if(strlen(pcapErr)) {
74
 		fprintf(stderr, "could not open %s: %s\n", o_int, pcapErr);
75
 	}
76
@@ -212,7 +222,10 @@
77
 		case DLT_RAW:
78
 			dprint("raw interface detected, no encapsulation\n");
79
 			break;
80
-		default: 
81
+		case DLT_NULL:
82
+			dprint("tun interface detected. pppoe on freebsd\n");
83
+			break;
84
+		default:
85
 			fprintf(stderr, "error: unsupported link-layer type: %d\n", lltype);
86
 			cleanup(1);
87
 			break;
88
@@ -301,9 +314,9 @@
89
 		struct tm *tm;
90
 		t = time(NULL);
91
 		tm = localtime(&t);
92
-		
93
-		fprintf(logfd, "[%04d-%02d-%02d %02d:%02d] %s\n", tm->tm_year+1900,
94
-			tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min, msg);
95
+
96
+		fprintf(logfd, "[%04d-%02d-%02d %02d:%02d:%02d] %s\n", tm->tm_year+1900,
97
+			tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, msg);
98
 		fflush(logfd);
99
 	}
100
 }
101
@@ -420,7 +433,7 @@
102
 	if(pch != str) {
103
 		memmove(str, pch, (strlen(pch) + 1));
104
 	}
105
-	
106
+
107
 	pch = (char*)(str + (strlen(str) - 1));
108
 	while(isspace(*pch)) {
109
 		pch--;
110
@@ -503,7 +516,7 @@
111
 					dprint("config: usesyslog\n");
112
 				} else {
113
 					fprintf(stderr, "config: line %d: syntax error\n", linenum);
114
-					return(1);			
115
+					return(1);
116
 				}
117
 			} else {
118
 				trim(ptr);
119
@@ -681,7 +694,7 @@
120
 		return(1);
121
 	}
122
 	dprint_sequence(door, "new sequence for door %s: ", door->name);
123
-	
124
+
125
 	return(0);
126
 }
127
 
128
@@ -694,7 +707,7 @@
129
 {
130
 	char line[PATH_MAX+1];
131
 	int pos;
132
-	
133
+
134
 	pos = ftell(door->one_time_sequences_fd);
135
 	while(fgets(line, PATH_MAX, door->one_time_sequences_fd)) {
136
 		trim(line);
137
@@ -746,7 +759,7 @@
138
 
139
 	rewind(door->one_time_sequences_fd);
140
 	pseudo_door.one_time_sequences_fd = door->one_time_sequences_fd;
141
-	
142
+
143
 	pos = get_next_one_time_sequence(&pseudo_door);
144
 	while(pos >= 0) {
145
 		if(door->seqcount == pseudo_door.seqcount) {
146
@@ -769,10 +782,10 @@
147
 void generate_pcap_filter()
148
 {
149
 	/* NOTE: We're doing string manipulations in a daemon -- use defensive programming! */
150
-		
151
+
152
 	PMList *lp;
153
 	opendoor_t *door;
154
-	char *buffer = NULL;			/* temporary buffer to create the individual filter strings */ 
155
+	char *buffer = NULL;			/* temporary buffer to create the individual filter strings */
156
 	size_t bufsize = 0;			/* size of buffer */
157
 	char port_str[10];			/* used by snprintf to convert unsigned short --> string */
158
 	short head_set = 0;			/* flag indicating if protocol head is set (i.e. "((tcp dst port") */
159
@@ -781,7 +794,7 @@
160
 	unsigned int i;
161
 	short modified_filters = 0;		/* flag indicating if at least one filter has changed --> recompile the filter */
162
 	struct bpf_program bpf_prog;		/* compiled BPF filter program */
163
-	
164
+
165
 	/* generate subfilters for each door having a NULL pcap_filter_exp
166
 	 *
167
 	 * Example filter for one single door:
168
@@ -796,11 +809,11 @@
169
 
170
 		/* if we get here at least one door had a pcap_filter_exp == NULL */
171
 		modified_filters = 1;
172
-		
173
+
174
 		head_set = 0;
175
 		tcp_present = 0;
176
 		udp_present = 0;
177
-		
178
+
179
 		/* allocate memory for buffer if needed.
180
 		 * The first allocation will be 200 Bytes (should be large enough for common sequences). If there is
181
 		 * not enough space, a call to realloc_strcat() will eventually increase its size. The buffer will be
182
@@ -913,7 +926,7 @@
183
 		if(udp_present) {
184
 			bufsize = realloc_strcat(&buffer, ")", bufsize);		/* close parentheses of UDP ports */
185
 		}
186
-		
187
+
188
 		/* test if in any of the precedent calls to realloc_strcat() failed. We can do this safely here because
189
 		 * realloc_strcat() returns 0 on failure and if a buffer size of 0 is passed to it, the function does
190
 		 * nothing but returning 0 again. Because we never read buffer in the above code, it is secure to test
191
@@ -930,7 +943,7 @@
192
 			perror("malloc");
193
 			cleanup(1);
194
 		}
195
-		strcpy(door->pcap_filter_exp, buffer);	
196
+		strcpy(door->pcap_filter_exp, buffer);
197
 
198
 		buffer[0] = '\0';	/* "clear" the buffer */
199
 	}
200
@@ -941,7 +954,7 @@
201
 	 * Note that we don't check if a port is included in multiple doors, we simply concatenate the individual door
202
 	 * filters and rely on pcap's optimization capabilities.
203
 	 *
204
-	 * Example filter for two doors with sequences 8000:tcp,4000:udp,8001:tcp,4001:udp,8002:tcp (syn) and 
205
+	 * Example filter for two doors with sequences 8000:tcp,4000:udp,8001:tcp,4001:udp,8002:tcp (syn) and
206
 	 * 1234:tcp,4567:tcp,8901:tcp (syn,ack) :
207
 	 * dst host the.hosts.ip.address and (
208
 	 *      ((tcp dst port 8000 or 8001 or 8002) and tcp[tcpflags] & tcp-syn != 0) or (udp dst port 4000 or 4001)
209
@@ -1005,7 +1018,7 @@
210
 
211
 	needed_size = strlen(*dest) + strlen(src) + 1;		/* '+ 1' for '\0' */
212
 	new_size = size;
213
-	
214
+
215
 	while(needed_size > new_size) {
216
 		new_size *= 2;
217
 	}
218
@@ -1018,7 +1031,7 @@
219
 
220
 	/* now dest is large enough to strcat() the src */
221
 	strcat(*dest, src);
222
-	
223
+
224
 	return new_size;
225
 }
226
 
227
@@ -1059,7 +1072,11 @@
228
 	}
229
 	buf[0] = '\0';
230
 
231
+#if defined(__FreeBSD__) || defined(__APPLE__)
232
 	s = socket(AF_INET, SOCK_DGRAM, 0);
233
+#else
234
+	s = socket(PF_PACKET, SOCK_DGRAM, 0);
235
+#endif
236
 	if(s < 0) {
237
 		return(NULL);
238
 	}
239
@@ -1161,12 +1178,17 @@
240
 void sniff(u_char* arg, const struct pcap_pkthdr* hdr, const u_char* packet)
241
 {
242
 	/* packet structs */
243
+#if defined(__FreeBSD__) || defined(__APPLE__)
244
+	struct ether_header* eth = NULL;
245
+	struct ip* ip = NULL;
246
+#else
247
 	struct ethhdr* eth = NULL;
248
 	struct iphdr* ip   = NULL;
249
+#endif
250
 	struct tcphdr* tcp = NULL;
251
 	struct udphdr* udp = NULL;
252
 	char proto[8];
253
-	/* TCP/IP data */	
254
+	/* TCP/IP data */
255
 	struct in_addr inaddr;
256
 	unsigned short sport, dport;
257
 	char srcIP[16], dstIP[16];
258
@@ -1179,23 +1201,46 @@
259
 	knocker_t *attempt = NULL;
260
 
261
 	if(lltype == DLT_EN10MB) {
262
+#if defined(__FreeBSD__) || defined(__APPLE__)
263
+		eth = (struct ether_header*)packet;
264
+		if(ntohs(eth->ether_type) != ETHERTYPE_IP) {
265
+			return;
266
+		}
267
+
268
+		ip = (struct ip*)(packet + sizeof(struct ether_header));
269
+	} else if(lltype == DLT_LINUX_SLL) {
270
+		ip = (struct ip*)((u_char*)packet + 16);
271
+	} else if(lltype == DLT_RAW) {
272
+		ip = (struct ip*)((u_char*)packet);
273
+	} else if(lltype == DLT_NULL) {
274
+		ip = (struct ip*)((u_char*)packet + 4);
275
+	}
276
+
277
+	if(ip->ip_v != 4) {
278
+#else
279
 		eth = (struct ethhdr*)packet;
280
 		if(ntohs(eth->h_proto) != ETH_P_IP) {
281
 			return;
282
 		}
283
+
284
 		ip = (struct iphdr*)(packet + sizeof(struct ethhdr));
285
 	} else if(lltype == DLT_LINUX_SLL) {
286
 		ip = (struct iphdr*)((u_char*)packet + 16);
287
 	} else if(lltype == DLT_RAW) {
288
 		ip = (struct iphdr*)((u_char*)packet);
289
 	}
290
-	
291
+
292
 	if(ip->version != 4) {
293
+#endif
294
 		/* no IPv6 yet */
295
 		dprint("packet is not IPv4, ignoring...\n");
296
 		return;
297
 	}
298
+#if defined(__FreeBSD__) || defined(__APPLE__)
299
+	if(ip->ip_p == IPPROTO_ICMP) {
300
+#else
301
 	if(ip->protocol == IPPROTO_ICMP) {
302
+#endif
303
 		/* we don't do ICMP */
304
 		return;
305
 	}
306
@@ -1207,12 +1252,30 @@
307
 		fprintf(stderr, "error: could not understand IP address: %s\n", myip);
308
 		return;
309
 	}
310
+#if defined(__FreeBSD__) || defined(__APPLE__)
311
+	if(ip->ip_dst.s_addr != inaddr.s_addr) {
312
+#else
313
 	if(ip->daddr != inaddr.s_addr) {
314
+#endif
315
 		dprint("packet destined for another host, ignoring...\n");
316
 		return;
317
 	}
318
-	
319
+
320
 	sport = dport = 0;
321
+
322
+#if defined(__FreeBSD__) || defined(__APPLE__)
323
+	if(ip->ip_p == IPPROTO_TCP) {
324
+		strncpy(proto, "tcp", sizeof(proto));
325
+		tcp = (struct tcphdr*)((u_char*)ip + (ip->ip_hl *4));
326
+		sport = ntohs(tcp->th_sport);
327
+		dport = ntohs(tcp->th_dport);
328
+	}
329
+	if(ip->ip_p == IPPROTO_UDP) {
330
+		strncpy(proto, "udp", sizeof(proto));
331
+		udp = (struct udphdr*)((u_char*)ip + (ip->ip_hl * 4));
332
+		sport = ntohs(udp->uh_sport);
333
+		dport = ntohs(udp->uh_dport);
334
+#else
335
 	if(ip->protocol == IPPROTO_TCP) {
336
 		strncpy(proto, "tcp", sizeof(proto));
337
 		tcp = (struct tcphdr*)((u_char*)ip + (ip->ihl * 4));
338
@@ -1224,6 +1287,7 @@
339
 		udp = (struct udphdr*)((u_char*)ip + (ip->ihl * 4));
340
 		sport = ntohs(udp->source);
341
 		dport = ntohs(udp->dest);
342
+#endif
343
 	}
344
 
345
 	/* get the date/time */
346
@@ -1234,10 +1298,17 @@
347
 			pkt_tm->tm_sec);
348
 
349
 	/* convert IPs from binary to string */
350
+#if defined(__FreeBSD__) || defined(__APPLE__)
351
+	inaddr.s_addr = ip->ip_src.s_addr;
352
+	strncpy(srcIP, inet_ntoa(inaddr), sizeof(srcIP)-1);
353
+	srcIP[sizeof(srcIP)-1] = '\0';
354
+	inaddr.s_addr = ip->ip_dst.s_addr;
355
+#else
356
 	inaddr.s_addr = ip->saddr;
357
 	strncpy(srcIP, inet_ntoa(inaddr), sizeof(srcIP)-1);
358
 	srcIP[sizeof(srcIP)-1] = '\0';
359
 	inaddr.s_addr = ip->daddr;
360
+#endif
361
 	strncpy(dstIP, inet_ntoa(inaddr), sizeof(dstIP)-1);
362
 	dstIP[sizeof(dstIP)-1] = '\0';
363
 
364
@@ -1297,6 +1368,73 @@
365
 		/* if tcp, check the flags to ignore the packets we don't want
366
 		 * (don't even use it to cancel sequences)
367
 		 */
368
+#if defined(__FreeBSD__) || defined(__APPLE__)
369
+		if(ip->ip_p == IPPROTO_TCP) {
370
+			if(attempt->door->flag_fin != DONT_CARE) {
371
+				if(attempt->door->flag_fin == SET && !(tcp->th_flags & TH_FIN)) {
372
+					dprint("packet is not FIN, ignoring...\n");
373
+					flagsmatch = 0;
374
+				}
375
+				if(attempt->door->flag_fin == NOT_SET && (tcp->th_flags & TH_FIN)) {
376
+					dprint("packet is not !FIN, ignoring...\n");
377
+					flagsmatch = 0;
378
+				}
379
+			}
380
+			if(attempt->door->flag_syn != DONT_CARE) {
381
+				if(attempt->door->flag_syn == SET && !(tcp->th_flags & TH_SYN)) {
382
+					dprint("packet is not SYN, ignoring...\n");
383
+					flagsmatch = 0;
384
+				}
385
+				if(attempt->door->flag_syn == NOT_SET && (tcp->th_flags & TH_SYN)) {
386
+					dprint("packet is not !SYN, ignoring...\n");
387
+					flagsmatch = 0;
388
+				}
389
+			}
390
+			if(attempt->door->flag_rst != DONT_CARE) {
391
+				if(attempt->door->flag_rst == SET && !(tcp->th_flags & TH_RST)) {
392
+					dprint("packet is not RST, ignoring...\n");
393
+					flagsmatch = 0;
394
+				}
395
+				if(attempt->door->flag_rst == NOT_SET && (tcp->th_flags & TH_RST)) {
396
+					dprint("packet is not !RST, ignoring...\n");
397
+					flagsmatch = 0;
398
+				}
399
+			}
400
+			if(attempt->door->flag_psh != DONT_CARE) {
401
+				if(attempt->door->flag_psh == SET && !(tcp->th_flags & TH_PUSH)) {
402
+					dprint("packet is not PSH, ignoring...\n");
403
+					flagsmatch = 0;
404
+				}
405
+				if(attempt->door->flag_psh == NOT_SET && (tcp->th_flags & TH_PUSH)) {
406
+					dprint("packet is not !PSH, ignoring...\n");
407
+					flagsmatch = 0;
408
+				}
409
+			}
410
+			if(attempt->door->flag_ack != DONT_CARE) {
411
+				if(attempt->door->flag_ack == SET && !(tcp->th_flags & TH_ACK)) {
412
+					dprint("packet is not ACK, ignoring...\n");
413
+					flagsmatch = 0;
414
+				}
415
+				if(attempt->door->flag_ack == NOT_SET && !(tcp->th_flags & TH_ACK)) {
416
+					dprint("packet is not !ACK, ignoring...\n");
417
+					flagsmatch = 0;
418
+				}
419
+			}
420
+			if(attempt->door->flag_urg != DONT_CARE) {
421
+				if(attempt->door->flag_urg == SET && !(tcp->th_flags & TH_URG)) {
422
+					dprint("packet is not URG, ignoring...\n");
423
+					flagsmatch = 0;
424
+				}
425
+				if(attempt->door->flag_urg == NOT_SET && !(tcp->th_flags & TH_URG)) {
426
+					dprint("packet is not !URG, ignoring...\n");
427
+					flagsmatch = 0;
428
+				}
429
+			}
430
+		}
431
+		if(flagsmatch && ip->ip_p == attempt->door->protocol[attempt->stage] &&
432
+				dport == attempt->door->sequence[attempt->stage]) {
433
+
434
+#else
435
 		if(ip->protocol == IPPROTO_TCP) {
436
 			if(attempt->door->flag_fin != DONT_CARE) {
437
 				if(attempt->door->flag_fin == SET && tcp->fin != 1) {
438
@@ -1361,6 +1499,7 @@
439
 		}
440
 		if(flagsmatch && ip->protocol == attempt->door->protocol[attempt->stage] &&
441
 				dport == attempt->door->sequence[attempt->stage]) {
442
+#endif
443
 			/* level up! */
444
 			attempt->stage++;
445
 			if(attempt->srchost) {
446
@@ -1387,7 +1526,7 @@
447
 						size_t cmd_len = 0;
448
 
449
 						setsid();
450
-						
451
+
452
 						/* parse start and stop command and check if the parsed commands fit in the given buffer. Don't
453
 						 * execute any command if one of them has been truncated */
454
 						cmd_len = parse_cmd(parsed_start_cmd, sizeof(parsed_start_cmd), attempt->door->start_command, attempt->src);
455
@@ -1419,7 +1558,7 @@
456
 							}
457
 							exec_cmd(parsed_stop_cmd, attempt->door->name);
458
 						}
459
-						
460
+
461
 						exit(0); /* exit child */
462
 					}
463
 				}
464
@@ -1451,6 +1590,36 @@
465
 		for(lp = doors; lp; lp = lp->next) {
466
 			opendoor_t *door = (opendoor_t*)lp->data;
467
 			/* if we're working with TCP, try to match the flags */
468
+#if defined(__FreeBSD__) || defined(__APPLE__)
469
+			if(ip->ip_p == IPPROTO_TCP){
470
+				if(door->flag_fin != DONT_CARE) {
471
+					if(door->flag_fin == SET && !(tcp->th_flags & TH_FIN)) {dprint("packet is not FIN, ignoring...\n");continue;}
472
+					if(door->flag_fin == NOT_SET && (tcp->th_flags & TH_FIN)) {dprint("packet is not !FIN, ignoring...\n");continue;}
473
+				}
474
+				if(door->flag_syn != DONT_CARE) {
475
+					if(door->flag_syn == SET && !(tcp->th_flags & TH_SYN)) {dprint("packet is not SYN, ignoring...\n");continue;}
476
+					if(door->flag_syn == NOT_SET && (tcp->th_flags & TH_SYN)) {dprint("packet is not !SYN, ignoring...\n");continue;}
477
+				}
478
+				if(door->flag_rst != DONT_CARE) {
479
+					if(door->flag_rst == SET && !(tcp->th_flags & TH_RST)) {dprint("packet is not RST, ignoring...\n");continue;}
480
+					if(door->flag_rst == NOT_SET && (tcp->th_flags & TH_RST)) {dprint("packet is not !RST, ignoring...\n");continue;}
481
+				}
482
+				if(door->flag_psh != DONT_CARE) {
483
+					if(door->flag_psh == SET && !(tcp->th_flags & TH_PUSH)) {dprint("packet is not PSH, ignoring...\n");continue;}
484
+					if(door->flag_psh == NOT_SET && (tcp->th_flags & TH_PUSH)) {dprint("packet is not !PSH, ignoring...\n");continue;}
485
+				}
486
+				if(door->flag_ack != DONT_CARE) {
487
+					if(door->flag_ack == SET && !(tcp->th_flags & TH_ACK)) {dprint("packet is not ACK, ignoring...\n");continue;}
488
+					if(door->flag_ack == NOT_SET && (tcp->th_flags & TH_ACK)) {dprint("packet is not !ACK, ignoring...\n");continue;}
489
+				}
490
+				if(door->flag_urg != DONT_CARE) {
491
+					if(door->flag_urg == SET && !(tcp->th_flags & TH_URG)) {dprint("packet is not URG, ignoring...\n");continue;}
492
+					if(door->flag_urg == NOT_SET && (tcp->th_flags & TH_URG)) {dprint("packet is not !URG, ignoring...\n");continue;}
493
+				}
494
+			}
495
+
496
+			if(ip->ip_p == door->protocol[0] && dport == door->sequence[0]) {
497
+#else
498
 			if(ip->protocol == IPPROTO_TCP){
499
 				if(door->flag_fin != DONT_CARE) {
500
 					if(door->flag_fin == SET && tcp->fin != 1) {dprint("packet is not FIN, ignoring...\n");continue;}
501
@@ -1479,6 +1648,7 @@
502
 			}
503
 
504
 			if(ip->protocol == door->protocol[0] && dport == door->sequence[0]) {
505
+#endif
506
 				struct hostent *he;
507
 				/* create a new entry */
508
 				attempt = (knocker_t*)malloc(sizeof(knocker_t));
509
@@ -1490,7 +1660,11 @@
510
 				strcpy(attempt->src, srcIP);
511
 				/* try a reverse lookup if enabled  */
512
 				if (o_lookup) {
513
+#if defined(__FreeBSD__) || defined(__APPLE__)
514
+					inaddr.s_addr = ip->ip_src.s_addr;
515
+#else
516
 					inaddr.s_addr = ip->saddr;
517
+#endif
518
 					he = gethostbyaddr((void *)&inaddr, sizeof(inaddr), AF_INET);
519
 					if(he) {
520
 						attempt->srchost = strdup(he->h_name);
(-)b/security/knock/files/patch-src_knock.c (+11 lines)
Added Link Here
1
--- src/knock.c.orig	2023-07-23 20:01:56 UTC
2
+++ src/knock.c
3
@@ -35,7 +35,7 @@
4
 #include <getopt.h>
5
 #include <fcntl.h>
6
 
7
-static char version[] = "0.8";
8
+static char version[] = "#VERSION#";
9
 
10
 #define PROTO_TCP 1
11
 #define PROTO_UDP 2
(-)b/security/knock/files/patch-src_knockd.c (-1 / +20 lines)
Added Link Here
0
- 
1
--- src/knockd.c.orig	2021-04-24 16:54:37 UTC
2
+++ src/knockd.c
3
@@ -63,7 +63,7 @@
4
 extern int daemon(int, int);
5
 #endif
6
 
7
-static char version[] = "0.8";
8
+static char version[] = "#VERSION#";
9
 
10
 #define SEQ_TIMEOUT 25 /* default knock timeout in seconds */
11
 #define CMD_TIMEOUT 10 /* default timeout in seconds between start and stop commands */
12
@@ -163,7 +163,7 @@ int  o_daemon    = 0;
13
 int  o_lookup    = 0;
14
 int  o_skipIpV6  = 0;
15
 char o_int[32]           = "";		/* default (eth0) is set after parseconfig() */
16
-char o_cfg[PATH_MAX]     = "/etc/knockd.conf";
17
+char o_cfg[PATH_MAX]     = "%%PREFIX%%/etc/knockd.conf";
18
 char o_pidfile[PATH_MAX] = "/var/run/knockd.pid";
19
 char o_logfile[PATH_MAX] = "";
20
 

Return to bug 267232