Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
linux:iptables [2013/08/10 08:14] 5.39.219.26 bavgouhn |
linux:iptables [2013/10/25 15:16] (current) a add Per user traffic accounting (moved from linux:networking) |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | bohrouou, http://lomm.org/ Xanax medication, lYjjWvu. | + | ====== Linux filtering / firewalling (netfilter/ |
| + | |||
| + | |||
| + | |||
| + | ==== P2P blocking/ | ||
| + | == Links == | ||
| + | * [[http:// | ||
| + | * [[http:// | ||
| + | * [[http:// | ||
| + | * [[linux: | ||
| + | * [[http:// | ||
| + | * [[http://dev.inversepath.com/ | ||
| + | |||
| + | ==== Netfilter concept / network flow ==== | ||
| + | Click on picture below to see more .. | ||
| + | {{ linux: | ||
| + | |||
| + | ==== Logging and limiting SSH bruteforce attacks ==== | ||
| + | |||
| + | Logging is easy, just add the same rule but with a '' | ||
| + | |||
| + | | ||
| + | 1/min –hashlimit-mode srcip –hashlimit-name ssh -m state \ | ||
| + | | ||
| + | |||
| + | |||
| + | As for permantely adding hosts, why? Poluting a firewall ruleset with a rule that isn’t going to be hit frequently is a waste. Which is why the hashlimit rule is perfect for this situation. | ||
| + | |||
| + | See also [[http:// | ||
| + | |||
| + | ==== A solution for blocking ssh probers/ | ||
| + | |||
| + | ### Catch SSH probes | ||
| + | iptables -A FORWARD -p tcp --dport 22 -d <local net> -o eth0 -s 0/0 -i ppp0 | ||
| + | -m state --state NEW | ||
| + | -m recent --rcheck --hitcount 3 --seconds 60 --name SSH_PROBERS | ||
| + | -j LOG --log-prefix " | ||
| + | |||
| + | iptables -A FORWARD -p tcp --dport 22 -d <local net> -o eth0 -s 0/0 -i ppp0 | ||
| + | -m state --state NEW | ||
| + | -m recent --update --hitcount 3 --seconds 60 --name SSH_PROBERS | ||
| + | -j DROP | ||
| + | |||
| + | iptables -A FORWARD -p tcp --dport 22 -d <local net> -o eth0 -s 0/0 -i ppp0 | ||
| + | -m state --state NEW | ||
| + | -m recent --set --name SSH_PROBERS | ||
| + | -j ACCEPT | ||
| + | |||
| + | So, in the INPUT chain, you wouldn' | ||
| + | |||
| + | What it does, is uses the '' | ||
| + | |||
| + | |||
| + | ===== Per user traffic accounting ===== | ||
| + | |||
| + | Modern times require you to know how much traffic each user on a system is generating. A lightweight and unobtrusive way to do it is: | ||
| + | <code bash> | ||
| + | iptables -A PREROUTING -t mangle -j CONNMARK --restore-mark | ||
| + | for interesting user in /etc/passwd # | ||
| + | do | ||
| + | #mark all user packets with their uid | ||
| + | iptables -A OUTPUT -t mangle -m owner --uid-owner $uid -j MARK --set-mark $uid | ||
| + | iptables -A OUTPUT -t mangle -m owner --uid-owner $uid -j CONNMARK --save-mark | ||
| + | #add rules to count packets | ||
| + | iptables -A PREROUTING -t mangle -m mark --mark $uid -m comment --comment "count $user" | ||
| + | iptables -A POSTROUTING -t mangle -m mark --mark $uid -m comment --comment "count $user" | ||
| + | done | ||
| + | </ | ||
| + | |||
| + | Integrating this with existing firewall rules is left as an excercise for the reader. | ||
| + | |||
| + | Observing counters is as easy as | ||
| + | <code bash> | ||
| + | watch " | ||
| + | </ | ||
| + | Or you can parse them periodically and store values somewhere for further processing. | ||
| + | |||
| + | This method identifies which user caused some traffic only for the traffic that is initiated on the machine. Traffic that originates on a remote system is not caught. I haven' | ||
| + | |||
| + | Tested on rhel6. | ||
| + | |||
| + | |||
| + | ===== Strategy for penalising IPs with too many simultaneous sessions | ||
| + | |||
| + | Something like this (eth0 is the user's network): | ||
| + | |||
| + | | ||
| + | | ||
| + | |||
| + | | ||
| + | | ||
| + | |||
| + | | ||
| + | 1024:65535 -m set --set p2p src -j MARK --set-mark 60 | ||
| + | |||
| + | // | ||
| + | |||
| + | |||
| + | ===== Conntrack table full ===== | ||
| + | > Feb 23 14:26:19 gestor1 kernel: printk: 38 messages suppressed. | ||
| + | > Feb 23 14:26:19 gestor1 kernel: ip_conntrack: | ||
| + | |||
| + | Not necessarily the answer you were looking for, but this is what connlimit was written for. Connlimit will limit the number of parallel | ||
| + | TCP connections per host. Do something like: | ||
| + | |||
| + | iptables -t mangle -A PREROUTING -p tcp -i eth0 --dport 1024: \ | ||
| + | -m connlimit --connlimit-above 30 -j DROP | ||
| + | |||
| + | connlimit is not in the vanilla kernel at the minute; you need to patch with pom. You can download pom from | ||
| + | http:// | ||
| + | |||
| + | ===== Preventing webserver hackers from connecting to IRC servers ===== | ||
| + | |||
| + | Sometimes when a user runs some picture-gallery or forum software, your server gets more or less hacked: a hacker will start under the user with which your webserver runs (' | ||
| + | |||
| + | | ||
| + | |||
| + | //This will not work if the hacker runs his/her irc-server on a different portnumber then the ones blocked.// | ||
| + | |||
| + | ==== Firewall example (the good old TNT firewall) ==== | ||
| + | Download {{linux: | ||

