up vote 0 down vote favorite
1
share [g+] share [fb]

To prevent brute force attacks against ssh I've added some iptables rules (below). The question is: How can I list the blocked IP addresses?

(1)
iptables -A INPUT -i eth0 -p tcp --dport 22 -m state --state NEW -m recent --set --name SSH
iptables -A INPUT -i eth0 -p tcp --dport 22 -m state --state NEW -m recent --update --seconds 60 --hitcount 8 --rttl --name SSH -j DROP
link|improve this question
feedback

4 Answers

up vote 4 down vote accepted

One option would be to log any of your dropped packets with a rule like:

iptables -A INPUT -i eth0 -p tcp --dport 22 -m state --state NEW -m recent --update --seconds 60 --hitcount 8 --rttl -j LOG --log-prefix "FW_DROPPED: "

Insert that immediately before the DROP rule. Then, you can grep the syslog file for anything with "FW_DROPPED" in it and the list of IPs will be there. The entries in the log file look something like this:

Jun  3 08:05:57 some-machine kernel: [15852451.420557] FW_DROPPED: IN=eth0 OUT= MAC=00:50:ba:4a:d9:e3:00:12:17:3a:e3:64:08:00 SRC=228.23.45.189 DST=192.168.1.1 LEN=48 TOS=0x00 PREC=0x00 TTL=106 ID=10941 PROTO=TCP SPT=58212 DPT=22 WINDOW=65535 RES=0x00 SYN URGP=0

So, snipping out what follows "SRC=" will show you the dropped IPs. Sort that, eliminating duplicates, and you'll have your list.

I've found the Iptables Tutorial to be the most useful documentation for iptables/netfilter.

link|improve this answer
Yes, this is the way to do it. – Brent Jun 3 '09 at 16:18
This would require you to duplicate your rules as you don't log and drop in the same rule. – David Pashley Jun 3 '09 at 16:23
2  
It would be even better if he creates a user chain where he logs and drops the offending packets. By doing this, unnecessarily duplication is removed. It can be done like this: iptables -N attacks; iptables -A INPUT -i eth0 -p tcp --dport 22 -m state --state NEW -m recent --update --seconds 60 --hitcount 8 --rttl --name SSH -j attacks; iptables -A attacks -j LOG --log-prefix "FW_DROPPED: "; iptables -A attacks -j DROP – Cristian Ciupitu Jun 3 '09 at 17:00
Agreed. Tables/chains are the way to go; the whole point of iptables, really. I create separate tables for accepted, silently dropped, audibly rejected, discarded, suspected floods, illegal, suspected probes, suspected scans, unknown types, and watched traffic. My FWs are based on this old script (warning: self-promotion): code.google.com/p/klondike-firewall/source/browse/trunk/… – yukondude Jun 3 '09 at 17:27
1  
The ipt_recent module that ipozgaj and David Pashley referenced is a pretty nifty addition, although it can't provide the same fine-grained reason a particular IP was dropped (as you can with different --log-prefix settings) or keep track of a long history of attacks with timestamps. Definitely a good idea to use both techniques in concert though. – yukondude Jun 3 '09 at 19:00
feedback

You can find details under /proc/net/ipt_recent/SSH.

This article has more information.

link|improve this answer
feedback

Look at

/proc/net/ipt_recent/YOURNAME

where YOURNAME is the name you used with --name option in your iptables rule.

link|improve this answer
feedback

What I do, for example for input address spoofing, is to define the chain SPOOF_REJECT:

iptables -N SPOOF_REJECT
iptables -A SPOOF_REJECT -j LOG --log-prefix "Input spoof detected: "
iptables -A SPOOF_REJECT -j REJECT

then to send packets to this chain if they are spoofed:

iptables -A INPUT -i $EXT_DEV1 -s $INT_NET -j SPOOF_REJECT
iptables -A INPUT -i $EXT_DEV2 -s $INT_NET -j SPOOF_REJECT

You could do something like this for each category of packets you drop or reject to get a line in the syslog to look for, then periodically grep, cut, sort, to get just the IP addresses from these log lines.

The benefit to using individual chains for each category is that your configuration gets more straightforward and it gets easier to read your iptables configuration. As you add more and more rules, you'll be glad that you used individual chains for specific different actions.

link|improve this answer
How exactly, do you detect spoofed packets? In my experience this is impossible in the general case. – MarkR Jun 3 '09 at 20:25
The above is just an example. Here, I'm looking for any packet arriving at the INPUT chain that is coming from an external device yet claims to come from an IP address internal to my network. In this case, I can absolutely detect address spoofing, but yes, in the general case things are different. – Eddie Jun 4 '09 at 2:22
feedback

Your Answer

 
or
required, but never shown