Using recent
module in iptables
, how can I both check if the src ip address exists in a list and if it does, add it to another list?
I'm trying to implement port-knocking using iptables
together with this module (recent
).
So, I'd like to detect if a TCP packets arrive in a specific order by shifting the src ip address from one list to another, thus progressing it towards the final list, which would allow the src ip access to the machine.
My question is, basically, how to add src ip to a next step (list), when a packet has the correct destination port AND is in the correct current step (list), all in one rule. Something like this:
1:
# we'd like to accept the already authenticated packets quickly, hence the first rule
iptables -A KNOCKING -m recent --rcheck --seconds 60 --reap --name knockfinal -j ACCEPT
2:
# src ip is not authenticated, let's verify the first knock
# if the first port knock was correct (port 1111), add the src ip to the 'knock1' list
iptables -A KNOCKING -p tcp --dport 1111 -m recent --name knock1 --set -j DROP
3:
# now, here is the issue...
# how do we both check if the src ip is already in the 'knock1' list
# plus the second port knock was correct (port 2222), and add the src ip to 'knock2' list
# ideally, we would write something like this
iptables -A KNOCKING -m recent --rcheck --seconds 10 --reap --name knock1 -p tcp --dport 2222 -m recent --name knock2 --set -j DROP
I've read several different approaches to setting up port-knocking using iptables, but this one seemed the most trivial one for me, so I'd really like to either confirm or deny it's possible to do it using this approach.