8 Commits
v3 ... v5

Author SHA1 Message Date
Auke Kok 8a05303802 v5 2017-05-17 17:18:01 -07:00
Auke Kok e296f501c7 Use pcre to match logs, and find IP addresses.
This is a much more reliable method to extract the IP address
from the log entries, and allows us to consolidate 2 matches into
a single operation.

Once matched, we extract the IP substring and pass it to `find()`
as usual. We can add more regexes later if that is useful.
2017-05-16 15:55:18 -07:00
Auke Kok 379f74a071 Fix Travis.
Check in the man pages so we don't need to run `ronn` on CI.
2017-05-11 11:24:20 -07:00
Auke Kok 9042a01eab v4 2017-05-11 08:56:56 -07:00
Auke Kok 2225ee029d Revert "also catch port probers that try ssl level evils"
This reverts commit dc8f37e41f.

This message can print on a normal and legitimate user when they
disconnect, and therefore would be a false positive. We should
100% never get close to blocking legitimate users, ever.
2017-05-10 21:49:16 -07:00
Auke Kok dee23b8275 Lazy initialization.
At start, only initialize the journal, but wait until we actually
need to block anything before initializing ipset and iptables.
2017-05-10 21:14:07 -07:00
Auke Kok 34bd8d55bd Remove SIGUSR1 handler - dumping lists is obsolete with ipset. 2017-05-10 21:07:18 -07:00
Auke Kok 2a33768293 Don't break our LL on block.
We will prune regularly anyway, so this is entirely unneeded.
2017-05-10 20:59:36 -07:00
7 changed files with 159 additions and 107 deletions
-3
View File
@@ -12,9 +12,6 @@ missing
tallow
tallow-*.tar.gz
tallow-*/
tallow.conf.5
tallow.1
tallow.1.html
tallow.o
tallow.service
*~
+6 -9
View File
@@ -1,21 +1,18 @@
sudo: required
dist: trusty
compiler:
- gcc
os:
- linux
language: c
compiler: gcc
os: linux
before_script:
./autogen.sh
language: c
- ./autogen.sh
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- libsystemd-journal-dev
- valgrind
- autoconf
- automake
+2 -2
View File
@@ -1,12 +1,12 @@
AM_CFLAGS = -g $(LIBSYSTEMD_CFLAGS) $(LIBSYSTEMD_JOURNAL_CFLAGS) -Wall -Wno-uninitialized -W -D_FORTIFY_SOURCE=2
AM_CFLAGS = -g $(PCRE_CFLAGS) $(LIBSYSTEMD_CFLAGS) -Wall -Wno-uninitialized -W -D_FORTIFY_SOURCE=2
systemdsystemunitdir = @SYSTEMD_SYSTEMUNITDIR@
systemdsystemunit_DATA = tallow.service
sbin_PROGRAMS = tallow
tallow_SOURCES = tallow.c
tallow_LDADD = $(LIBSYSTEMD_LIBS) $(LIBSYSTEMD_JOURNAL_LIBS)
tallow_LDADD = $(PCRE_LIBS) $(LIBSYSTEMD_LIBS)
EXTRA_DIST = AUTHORS COPYING INSTALL tallow.service.in tallow.1.md
+3 -2
View File
@@ -2,7 +2,7 @@
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.64])
AC_INIT([tallow], [3], [auke-jan.h.kok@intel.com])
AC_INIT([tallow], [5], [auke-jan.h.kok@intel.com])
AM_INIT_AUTOMAKE([])
AC_CONFIG_FILES([Makefile])
@@ -10,7 +10,8 @@ AC_CONFIG_FILES([Makefile])
AC_PROG_CC
AC_PROG_INSTALL
PKG_CHECK_MODULES(LIBSYSTEMD, libsystemd,, [PKG_CHECK_MODULES(LIBSYSTEMD_JOURNAL, libsystemd-journal)])
PKG_CHECK_MODULES(PCRE, libpcre)
PKG_CHECK_MODULES(LIBSYSTEMD, libsystemd,, [PKG_CHECK_MODULES(LIBSYSTEMD, libsystemd-journal)])
AC_SUBST(LIBSYSTEMD_CFLAGS)
AC_SUBST(LIBSYSTEMD_LIBS)
AC_SUBST(LIBSYSTEMD_JOURNAL_CFLAGS)
+37
View File
@@ -0,0 +1,37 @@
.\" generated with Ronn/v0.7.3
.\" http://github.com/rtomayko/ronn/tree/0.7.3
.
.TH "TALLOW" "1" "May 2017" "" ""
.
.SH "NAME"
\fBtallow\fR
.
.SH "tallow"
Reduce log clutter due to ssh login attempts\.
.
.SH "SYNOPSIS"
\fB/usr/sbin/tallow\fR
.
.SH "DESCRIPTION"
\fBtallow\fR is a daemon that watches the systemd journal for messages from the \fBsshd\fR service\. It parses the messages and looks for attempted random logins such as failed logins to the root account and failed logins to invalid user accounts\.
.
.P
If such logins were detected, the offending IP address is stored in a list\. Items from this list are regularly purged, but if the amount of times that a specific IP address is seen exceeds a threshold (default 3), an ipset(1) entry is inserted in the \fBtallow\fR or \fBtallow6\fR ipset, and further packets from that ip address will be blocked by an \fBiptables(1)\fR or \fBip6tables(1)\fR rule that tallow creates at startup\.
.
.P
The system administrator needs to assure that the tallow and tallow6 ipsets are left alone and that the inserted iptables rules are properly matching on packets\.
.
.P
Care should be taken to assure that legitimate users are not blocked inadvertently\. You may wish to list any valid IP address with the whitelist option in tallow\.conf(5)\. Multiple addresses can be whitelisted\.
.
.SH "OPTIONS"
The \fBtallow\fR daemon itself has no runtime configuration\. All configuration is done through the tallow\.conf(5) config file\.
.
.SH "SEE ALSO"
systemd\-journald(1), iptables(1), tallow\.conf(5)
.
.SH "BUGS"
\fBtallow\fR is \fBNOT A SECURITY SOLUTION\fR, nor does it protect against random password logins\. A attacker may still be able to logon to your systems if you allow password logins\.
.
.SH "AUTHOR"
Auke Kok \fIauke\-jan\.h\.kok@intel\.com\fR
+71 -91
View File
@@ -15,10 +15,12 @@
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdbool.h>
#include <signal.h>
#include <unistd.h>
#include <limits.h>
#include <sys/time.h>
#include <pcre.h>
#include <systemd/sd-journal.h>
@@ -34,6 +36,9 @@ static struct tallow_struct *head;
static struct tallow_struct *whitelist;
#define FILTER_STRING "SYSLOG_IDENTIFIER=sshd"
static char *pattern = "MESSAGE=Failed password for .* from ([0-9a-z:.]+) port \\d+ ssh2";
#define MAX_OFFSETS 30
static char ipt_path[PATH_MAX];
static int threshold = 3;
@@ -69,11 +74,47 @@ static void ext_ignore(char *fmt, ...)
__attribute__((unused)) int ret = system(cmd);
}
static void setup(void)
{
static bool done = false;
if (done)
return;
done = true;
/* init ipset and iptables */
/* delete iptables ref to set before the ipset! */
ext_ignore("%s/iptables -t filter -D INPUT -m set --match-set tallow src -j DROP 2> /dev/null", ipt_path);
ext_ignore("%s/ipset destroy tallow 2> /dev/null", ipt_path);
if (ext("%s/ipset create tallow hash:ip family inet timeout %d", ipt_path, expires)) {
fprintf(stderr, "Unable to create ipv4 ipset.\n");
exit(EXIT_FAILURE);
}
if (ext("%s/iptables -t filter -A INPUT -m set --match-set tallow src -j DROP", ipt_path)) {
fprintf(stderr, "Unable to create iptables rule.\n");
exit(EXIT_FAILURE);
}
if (has_ipv6) {
ext_ignore("%s/ip6tables -t filter -D INPUT -m set --match-set tallow6 src -j DROP 2> /dev/null", ipt_path);
ext_ignore("%s/ipset destroy tallow6 2> /dev/null", ipt_path);
if (ext("%s/ipset create tallow6 hash:ip family inet6 timeout %d", ipt_path, expires)) {
fprintf(stderr, "Unable to create ipv6 ipset.\n");
exit(EXIT_FAILURE);
}
if (ext("%s/ip6tables -t filter -A INPUT -m set --match-set tallow6 src -j DROP", ipt_path)) {
fprintf(stderr, "Unable to create ipt6ables rule.\n");
exit(EXIT_FAILURE);
}
}
}
static void block(struct tallow_struct *s)
{
if (s->count != threshold)
return;
setup();
if (strchr(s->ip, ':')) {
if (has_ipv6)
(void) ext("%s/ipset -A tallow6 %s", ipt_path, s->ip);
@@ -82,18 +123,6 @@ static void block(struct tallow_struct *s)
}
fprintf(stderr, "Blocked %s\n", s->ip);
/* remove entry from the list */
if (head == s) {
head = s->next;
free(s->ip);
free(s);
} else {
struct tallow_struct *p = s;
s = s->next;
free(p->ip);
free(p);
}
}
static void whitelist_add(char *ip)
@@ -107,7 +136,7 @@ static void whitelist_add(char *ip)
n = malloc(sizeof(struct tallow_struct));
if (!n) {
fprintf(stderr, "Out of memory.\n");
exit(1);
exit(EXIT_FAILURE);
}
memset(n, 0, sizeof(struct tallow_struct));
n->ip = strdup(ip);
@@ -119,7 +148,7 @@ static void whitelist_add(char *ip)
w->next = n;
}
static void find(char *ip)
static void find(const char *ip)
{
struct tallow_struct *s = head;
struct tallow_struct *n;
@@ -163,7 +192,7 @@ static void find(char *ip)
n = malloc(sizeof(struct tallow_struct));
if (!n) {
fprintf(stderr, "Out of memory.\n");
exit(1);
exit(EXIT_FAILURE);
}
memset(n, 0, sizeof(struct tallow_struct));
@@ -181,36 +210,21 @@ static void find(char *ip)
return;
}
static void dump(void)
static void sig(int u __attribute__ ((unused)))
{
fprintf(stderr, "Exiting on request.\n");
sd_journal_close(j);
struct tallow_struct *s = head;
fprintf(stderr, "Received SIGUSR1 - dumping address table: address: count, time\n");
while (s) {
fprintf(stderr, "%s: %d, %lu.%lu\n", s->ip, s->count, s->time.tv_sec, s->time.tv_usec);
struct tallow_struct *n = NULL;
free(s->ip);
n = s;
s = s->next;
free(n);
}
}
static void sig(int s)
{
if (s == SIGUSR1) {
dump();
} else {
fprintf(stderr, "Exiting on request.\n");
sd_journal_close(j);
struct tallow_struct *s = head;
while (s) {
struct tallow_struct *n = NULL;
free(s->ip);
n = s;
s = s->next;
free(n);
}
exit(0);
}
exit(EXIT_SUCCESS);
}
static void prune(void)
@@ -255,7 +269,6 @@ int main(void)
memset(&s, 0, sizeof(struct sigaction));
s.sa_handler = sig;
sigaction(SIGUSR1, &s, NULL);
sigaction(SIGHUP, &s, NULL);
sigaction(SIGTERM, &s, NULL);
sigaction(SIGINT, &s, NULL);
@@ -286,7 +299,6 @@ int main(void)
continue;
// todo: filter leading/trailing whitespace
if (!strcmp(key, "ipt_path"))
strncpy(ipt_path, val, PATH_MAX - 1);
if (!strcmp(key, "threshold"))
@@ -310,34 +322,9 @@ int main(void)
r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
if (r < 0) {
fprintf(stderr, "Failed to open journal: %s\n", strerror(-r));
exit(1);
exit(EXIT_FAILURE);
}
/* init ipset and iptables */
/* delete iptables ref to set before the ipset! */
ext_ignore("%s/iptables -t filter -D INPUT -m set --match-set tallow src -j DROP 2> /dev/null", ipt_path);
ext_ignore("%s/ipset destroy tallow 2> /dev/null", ipt_path);
if (ext("%s/ipset create tallow hash:ip family inet timeout %d", ipt_path, expires)) {
fprintf(stderr, "Unable to create ipv4 ipset.\n");
exit(1);
}
if (ext("%s/iptables -t filter -A INPUT -m set --match-set tallow src -j DROP", ipt_path)) {
fprintf(stderr, "Unable to create iptables rule.\n");
exit(1);
}
if (has_ipv6) {
ext_ignore("%s/ip6tables -t filter -D INPUT -m set --match-set tallow6 src -j DROP 2> /dev/null", ipt_path);
ext_ignore("%s/ipset destroy tallow6 2> /dev/null", ipt_path);
if (ext("%s/ipset create tallow6 hash:ip family inet6 timeout %d", ipt_path, expires)) {
fprintf(stderr, "Unable to create ipv6 ipset.\n");
exit(1);
}
if (ext("%s/ip6tables -t filter -A INPUT -m set --match-set tallow6 src -j DROP", ipt_path)) {
fprintf(stderr, "Unable to create ipt6ables rule.\n");
exit(1);
}
}
/* ffwd journal */
sd_journal_add_match(j, FILTER_STRING, 0);
@@ -350,6 +337,11 @@ int main(void)
fprintf(stderr, "Started\n");
pcre *re = NULL;
int err;
const char *pcre_err;
re = pcre_compile(pattern, 0, &pcre_err, &err, NULL);
for (;;) {
const void *d;
size_t l;
@@ -361,9 +353,7 @@ int main(void)
}
while (sd_journal_next(j) != 0) {
char *t;
char *m;
int i;
if (sd_journal_get_data(j, "MESSAGE", &d, &l) < 0) {
fprintf(stderr, "Failed to read message field: %s\n", strerror(-r));
@@ -373,29 +363,19 @@ int main(void)
m = strndup(d, l+1);
m[l] = '\0';
if (strstr(m, "MESSAGE=Failed password for invalid user ")) {
t = strtok(m, " ");
for (i = 0; i < 7; i++)
t = strtok(NULL, " ");
find(t);
}
if (strstr(m, "MESSAGE=Failed password for root ")) {
t = strtok(m, " ");
for (i = 0; i < 5; i++)
t = strtok(NULL, " ");
find(t);
}
if (strstr(m, "MESSAGE=Received disconnect from ")) {
t = strtok(m, " ");
for (i = 0; i < 4; i++)
t = strtok(NULL, " ");
find(t);
int off[MAX_OFFSETS];
int ret = pcre_exec(re, NULL, m, l, 0, 0, off, MAX_OFFSETS);
if (ret == 2) {
const char *s;
ret = pcre_get_substring(m, off, 2, 1, &s);
if (ret > 0) {
find(s);
pcre_free_substring(s);
}
}
free(m);
}
prune();
@@ -403,5 +383,5 @@ int main(void)
sd_journal_close(j);
exit(0);
exit(EXIT_SUCCESS);
}
+40
View File
@@ -0,0 +1,40 @@
.\" generated with Ronn/v0.7.3
.\" http://github.com/rtomayko/ronn/tree/0.7.3
.
.TH "TALLOW" "5" "May 2017" "" ""
.
.SH "NAME"
\fBtallow\fR
.
.SH "tallow\.conf"
The tallow configuration file
.
.SH "NAME"
tallow\.conf \- Tallow daemon configuration file
.
.SH "SYNOPSIS"
\fB/etc/tallow\.conf\fR
.
.SH "DESCRIPTION"
This file is read on startup by the tallow(1) daemon, and can be used to provide options to the tallow daemon\. If not present, tallow will operate with built\-in defaults\.
.
.SH "OPTIONS"
\fBipt_path\fR=\fB<string>\fR Specifies the location of the ipset(1), iptables(1) or ip6tables(1) program\. By default, tallow will look in "/usr/sbin" for them\.
.
.P
\fBexpires\fR=\fB<int>\fR The number of seconds that IP addresses are blocked for\. Note that due to the implementation, IP addresses may be blocked for much longer than this period\. If IP addresses are seen, but not blocked within this period, they are also removed from the watch list\. Defaults to 3600s\.
.
.P
\fBthreshold\fR=\fB<int>\fR Specifies the number of times an IP address may appear before it is blocked\. Defaults to 3\.
.
.P
\fBwhitelist\fR=\fB<ipv4 address>\fR Specify an IP address that should never be blocked\. Multiple IP addresses can be included by repeating the \fBwhitelist\fR option several times\. By default, only 127\.0\.0\.1 is whitelisted\.
.
.P
\fBipv6\fR=\fB<0|1>\fR Enable of disable ipv6 (ip6tables) support\. Ipv6 is disabled automatically on systems that do not appear to have ipv6 support and enabled when ipv6 is present\. Use this option to explicitly disable ipv6 support if your system does not have ipv6 or is missing ip6tables\. Even with ipv6 disabled, tallow will track and log ipv6 addresses\.
.
.SH "SEE ALSO"
tallow(1), iptables(1)
.
.SH "AUTHOR"
Auke Kok \fIauke\-jan\.h\.kok@intel\.com\fR