From ead4ec3089b97eda1b438da248caf76f169345ad Mon Sep 17 00:00:00 2001 From: netblue30 Date: Sat, 18 Nov 2017 08:39:02 -0500 Subject: netfilter template support --- test/fnetfilter/cmdline.exp | 37 ++++++++++++ test/fnetfilter/copy.exp | 52 +++++++++++++++++ test/fnetfilter/default.exp | 40 +++++++++++++ test/fnetfilter/fnetfilter.sh | 28 ++++++++++ test/fnetfilter/outlocked | 0 test/fnetfilter/template.exp | 82 +++++++++++++++++++++++++++ test/fnetfilter/test1.net | 19 +++++++ test/fnetfilter/test2.net | 19 +++++++ test/fnetfilter/test3.net | 1 + test/network/netfilter-template.exp | 44 +++++++++++++++ test/network/network.sh | 6 ++ test/network/tcpserver.c | 108 ++++++++++++++++++++++++++++++++++++ 12 files changed, 436 insertions(+) create mode 100755 test/fnetfilter/cmdline.exp create mode 100755 test/fnetfilter/copy.exp create mode 100755 test/fnetfilter/default.exp create mode 100755 test/fnetfilter/fnetfilter.sh create mode 100644 test/fnetfilter/outlocked create mode 100755 test/fnetfilter/template.exp create mode 100644 test/fnetfilter/test1.net create mode 100644 test/fnetfilter/test2.net create mode 100644 test/fnetfilter/test3.net create mode 100755 test/network/netfilter-template.exp create mode 100644 test/network/tcpserver.c (limited to 'test') diff --git a/test/fnetfilter/cmdline.exp b/test/fnetfilter/cmdline.exp new file mode 100755 index 000000000..1a0b1c5aa --- /dev/null +++ b/test/fnetfilter/cmdline.exp @@ -0,0 +1,37 @@ +#!/usr/bin/expect -f +# This file is part of Firejail project +# Copyright (C) 2014-2017 Firejail Authors +# License GPL v2 + +set timeout 10 +spawn $env(SHELL) +match_max 100000 + +send -- "fnetfilter\r" +expect { + timeout {puts "TESTING ERROR 1\n";exit} + "Usage:" +} +after 100 + +send -- "fnetfilter -h\r" +expect { + timeout {puts "TESTING ERROR 2\n";exit} + "Usage:" +} +after 100 + +send -- "fnetfilter -h a b c d\r" +expect { + timeout {puts "TESTING ERROR 2\n";exit} + "Usage:" +} +after 100 + +send -- "fnetfilter a b c d\r" +expect { + timeout {puts "TESTING ERROR 2\n";exit} + "Usage:" +} +after 100 +puts "\nall done\n" diff --git a/test/fnetfilter/copy.exp b/test/fnetfilter/copy.exp new file mode 100755 index 000000000..65145ec4b --- /dev/null +++ b/test/fnetfilter/copy.exp @@ -0,0 +1,52 @@ +#!/usr/bin/expect -f +# This file is part of Firejail project +# Copyright (C) 2014-2017 Firejail Authors +# License GPL v2 + +set timeout 10 +spawn $env(SHELL) +match_max 100000 + +send -- "rm outfile\r" +after 100 + +send -- "fnetfilter test1.net outfile\r" +after 100 + +send -- "cat outfile\r" +expect { + timeout {puts "TESTING ERROR 1\n";exit} + "test1" +} +expect { + timeout {puts "TESTING ERROR 2\n";exit} + "*filter" +} +expect { + timeout {puts "TESTING ERROR 3\n";exit} + "INPUT -m state --state RELATED,ESTABLISHED" +} +expect { + timeout {puts "TESTING ERROR 4\n";exit} + "disable STUN" +} +after 100 + +send -- "fnetfilter foo outfile\r" +expect { + timeout {puts "TESTING ERROR 5\n";exit} + "cannot open foo" +} +after 100 + +send -- "fnetfilter test1.net outlocked\r" +expect { + timeout {puts "TESTING ERROR 6\n";exit} + "cannot open outlocked" +} +after 100 + +send -- "rm outfile\r" +after 100 + +puts "\nall done\n" diff --git a/test/fnetfilter/default.exp b/test/fnetfilter/default.exp new file mode 100755 index 000000000..d7c9d91d5 --- /dev/null +++ b/test/fnetfilter/default.exp @@ -0,0 +1,40 @@ +#!/usr/bin/expect -f +# This file is part of Firejail project +# Copyright (C) 2014-2017 Firejail Authors +# License GPL v2 + +set timeout 10 +spawn $env(SHELL) +match_max 100000 + +send -- "rm outfile\r" +after 100 + +send -- "fnetfilter outfile\r" +after 100 + +send -- "cat outfile\r" +expect { + timeout {puts "TESTING ERROR 1\n";exit} + "*filter" +} +expect { + timeout {puts "TESTING ERROR 2\n";exit} + "INPUT -m state --state RELATED,ESTABLISHED" +} +expect { + timeout {puts "TESTING ERROR 3\n";exit} + "disable STUN" +} +after 100 + +send -- "fnetfilter test1.net,33\r" +expect { + timeout {puts "TESTING ERROR 4\n";exit} + "invalid destination file in netfilter command" +} +after 100 +send -- "rm outfile\r" +after 100 + +puts "\nall done\n" diff --git a/test/fnetfilter/fnetfilter.sh b/test/fnetfilter/fnetfilter.sh new file mode 100755 index 000000000..5fd08d186 --- /dev/null +++ b/test/fnetfilter/fnetfilter.sh @@ -0,0 +1,28 @@ +#!/bin/bash +# This file is part of Firejail project +# Copyright (C) 2014-2017 Firejail Authors +# License GPL v2 + +export MALLOC_CHECK_=3 +export MALLOC_PERTURB_=$(($RANDOM % 255 + 1)) + +if [ -f /etc/debian_version ]; then + libdir=$(dirname "$(dpkg -L firejail | grep fcopy)") + export PATH="$PATH:$libdir" +fi + +export PATH="$PATH:/usr/lib/firejail" + +echo "TESTING: fnetfilter cmdline (test/fnetfilter/cmdline.exp)" +./cmdline.exp + +echo "TESTING: fnetfilter default (test/fnetfilter/default.exp)" +./default.exp + +echo "TESTING: fnetfilter copy (test/fnetfilter/copy.exp)" +./copy.exp + +echo "TESTING: fnetfilter template (test/fnetfilter/template.exp)" +./template.exp + +rm -f outfile diff --git a/test/fnetfilter/outlocked b/test/fnetfilter/outlocked new file mode 100644 index 000000000..e69de29bb diff --git a/test/fnetfilter/template.exp b/test/fnetfilter/template.exp new file mode 100755 index 000000000..eb57313bd --- /dev/null +++ b/test/fnetfilter/template.exp @@ -0,0 +1,82 @@ +#!/usr/bin/expect -f +# This file is part of Firejail project +# Copyright (C) 2014-2017 Firejail Authors +# License GPL v2 + +set timeout 10 +spawn $env(SHELL) +match_max 100000 + +send -- "rm outfile\r" +after 100 + +send -- "fnetfilter test2.net,icmp-type,destination-unreachable,time-exceeded,echo-request,3478,3479 outfile\r" +after 100 + +send -- "cat outfile\r" +expect { + timeout {puts "TESTING ERROR 1\n";exit} + "*filter" +} +expect { + timeout {puts "TESTING ERROR 2\n";exit} + "INPUT -m state --state RELATED,ESTABLISHED" +} +expect { + timeout {puts "TESTING ERROR 3\n";exit} + "icmp-type echo-reply" +} +expect { + timeout {puts "TESTING ERROR 4\n";exit} + "icmp-type destination-unreachable" +} +expect { + timeout {puts "TESTING ERROR 5\n";exit} + "icmp-type time-exceeded" +} +expect { + timeout {puts "TESTING ERROR 6\n";exit} + "icmp-type echo-request" +} +expect { + timeout {puts "TESTING ERROR 7\n";exit} + "dport 3478" +} +expect { + timeout {puts "TESTING ERROR 8\n";exit} + "dport 3479" +} +expect { + timeout {puts "TESTING ERROR 8\n";exit} + "dport 3478" +} +expect { + timeout {puts "TESTING ERROR 10\n";exit} + "dport 3479" +} +after 100 + +send -- "fnetfilter test2.net,icmp-type,destination-unreachable,time-exceeded,echo-request outfile\r" +expect { + timeout {puts "TESTING ERROR 11\n";exit} + "ARG5 on line 14 was not defined" +} +after 100 + +send -- "fnetfilter test2.net,icmp-type,destination-unreachable,time-exceeded,echo-request\r" +expect { + timeout {puts "TESTING ERROR 12\n";exit} + "invalid destination file in netfilter command" +} +after 100 + +send -- "fnetfilter test3.net,44 outfile\r" +expect { + timeout {puts "TESTING ERROR 13\n";exit} + "invalid template argument on line 1" +} +after 100 +send -- "rm outfile\r" +after 100 + +puts "\nall done\n" diff --git a/test/fnetfilter/test1.net b/test/fnetfilter/test1.net new file mode 100644 index 000000000..59bef1443 --- /dev/null +++ b/test/fnetfilter/test1.net @@ -0,0 +1,19 @@ +*filter +# test2 +:INPUT DROP [0:0] +:FORWARD DROP [0:0] +:OUTPUT ACCEPT [0:0] +-A INPUT -i lo -j ACCEPT +-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT +# echo replay is handled by -m state RELATED/ESTABLISHED above +#-A INPUT -p icmp --icmp-type echo-reply -j ACCEPT +-A INPUT -p icmp --icmp-type destination-unreachable -j ACCEPT +-A INPUT -p icmp --icmp-type time-exceeded -j ACCEPT +-A INPUT -p icmp --icmp-type echo-request -j ACCEPT +# disable STUN +-A OUTPUT -p udp --dport 3478 -j DROP +-A OUTPUT -p udp --dport 3479 -j DROP +-A OUTPUT -p tcp --dport 3478 -j DROP +-A OUTPUT -p tcp --dport 3479 -j DROP +COMMIT + diff --git a/test/fnetfilter/test2.net b/test/fnetfilter/test2.net new file mode 100644 index 000000000..a02785413 --- /dev/null +++ b/test/fnetfilter/test2.net @@ -0,0 +1,19 @@ +*filter +# test2 +:INPUT DROP [0:0] +:FORWARD DROP [0:0] +:OUTPUT ACCEPT [0:0] +-A INPUT -i lo -j ACCEPT +-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT +# echo replay is handled by -m state RELATED/ESTABLISHED above +#-A INPUT -p icmp --$ARG1 echo-reply -j ACCEPT +-A INPUT -p icmp --$ARG1 $ARG2 -j ACCEPT +-A INPUT -p icmp --$ARG1 $ARG3 -j ACCEPT +-A INPUT -p icmp --$ARG1 $ARG4 -j ACCEPT +# disable STUN +-A OUTPUT -p udp --dport $ARG5 -j DROP +-A OUTPUT -p udp --dport $ARG6 -j DROP +-A OUTPUT -p tcp --dport $ARG5 -j DROP +-A OUTPUT -p tcp --dport $ARG6 -j DROP +COMMIT + diff --git a/test/fnetfilter/test3.net b/test/fnetfilter/test3.net new file mode 100644 index 000000000..702cb06b3 --- /dev/null +++ b/test/fnetfilter/test3.net @@ -0,0 +1 @@ +asdfasdf $ARG asdfasdfdasf diff --git a/test/network/netfilter-template.exp b/test/network/netfilter-template.exp new file mode 100755 index 000000000..637b32468 --- /dev/null +++ b/test/network/netfilter-template.exp @@ -0,0 +1,44 @@ +#!/usr/bin/expect -f +# This file is part of Firejail project +# Copyright (C) 2014-2017 Firejail Authors +# License GPL v2 + +set timeout 10 +spawn $env(SHELL) +match_max 100000 + +send -- "firejail --net=br1 --ip=10.10.30.10 --name=test1 --netfilter=/etc/firejail/tcpserver.net,5555 ./tcpserver 5555\r" +expect { + timeout {puts "TESTING ERROR 1\n";exit} + "Child process initialized" +} +sleep 1 + +spawn $env(SHELL) +send -- "telnet 10.10.30.10 5555\r" +expect { + timeout {puts "TESTING ERROR 2\n";exit} + "Connected to 10.10.30.10" +} +sleep 1 + +send "sdfklsjadfl;ksadjfl;sdkfj\r" +expect { + timeout {puts "TESTING ERROR 3\n";exit} + "response" +} +expect { + timeout {puts "TESTING ERROR 4\n";exit} + "Connection closed" +} +sleep 1 + +send -- "telnet 10.10.30.10 5556\r" +expect { + timeout {puts "OK\n"} + "Connected to 10.10.30.10" {puts "TESTING ERROR 6\n";exit} + "dikasdfjasdjf" +} + +after 100 +puts "all done\n" diff --git a/test/network/network.sh b/test/network/network.sh index 739644c8e..83a70f1e3 100755 --- a/test/network/network.sh +++ b/test/network/network.sh @@ -8,6 +8,12 @@ export MALLOC_PERTURB_=$(($RANDOM % 255 + 1)) sudo ./configure +echo "TESTING: netfilter template (netfilter-template.exp)" +rm -f ./tcpserver +gcc -o tcpserver tcpserver.c +./netfilter-template.exp +rm ./tcpserver + echo "TESTING: firemon interface (firemon-interfaces.exp)" sudo ./firemon-interfaces.exp diff --git a/test/network/tcpserver.c b/test/network/tcpserver.c new file mode 100644 index 000000000..b2395a4ad --- /dev/null +++ b/test/network/tcpserver.c @@ -0,0 +1,108 @@ +/* + * Copyright (C) 2014-2017 Firejail Authors + * + * This file is part of firejail project + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ +#include +#include +#include +#include +#include +#include + + +int main(int argc, char **argv) { + int fd, newfd, client_len; + struct sockaddr_in serv_addr, client_addr; + int n, pid; + + if (argc < 2) { + printf("Usage: ./server port-number\n"); + return 1; + } + int portno = atoi(argv[1]); + + // init socket + fd = socket(AF_INET, SOCK_STREAM, 0); + if (fd < 0) { + perror("ERROR opening socket"); + return 1; + } + + // Initialize socket structure + memset(&serv_addr, 0, sizeof(serv_addr)); + + serv_addr.sin_family = AF_INET; + serv_addr.sin_addr.s_addr = INADDR_ANY; + serv_addr.sin_port = htons(portno); + + // bind + if (bind(fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { + perror("bind"); + return 1; + } + + // listen - 5 pending conncections + if (listen(fd, 5) < 0) { + perror("listen"); + return 1; + } + client_len = sizeof(client_addr); + + while (1) { + newfd = accept(fd, (struct sockaddr *) &client_addr, &client_len); + + if (newfd < 0) { + perror("accept"); + return 1; + } + + /* Create child process */ + pid = fork(); + + if (pid < 0) { + perror("fork"); + return 1; + } + + if (pid == 0) { + // child + close(fd); +#define MAXBUF 4096 + char buf[MAXBUF]; + memset(buf, 0, MAXBUF); + + int rcv = read(newfd, buf, MAXBUF - 1); + if (rcv < 0) { + perror("read"); + exit(1); + } + + int sent = write(newfd, "response\n", 9); + if (sent < 9) { + perror("write"); + return 1; + } + + exit(0); + } + else + close(newfd); + } + + return 0; +} -- cgit v1.2.3-54-g00ecf