Score:0

Create an NFQUEUE rule to match a local addresses destination in my raspberry pi router

pe flag

I'm working on a project to verify the source of each packet if its destination is one of several IPs on the LAN network. I'm interested in the LAN IPs, not the WAN.

I tried to create many matches like the following but nothing worked.

iptables -t nat -d <list of IPs> -A FORWARD -j NFQUEUE --queue-num 1

I have used the following rules to enable routing in my raspberry pi

sudo iptables -F

sudo iptables -t nat -F

sudo iptables -t nat -A POSTROUTING -o $eth -j MASQUERADE

sudo iptables -A FORWARD -i $eth -o $wlan -m state --state RELATED,ESTABLISHED -j ACCEPT

sudo iptables -A FORWARD -i $wlan -o $eth -j ACCEPT

The question is where should I put the NFQUEUE rule?

-EDIT-

I have been told to enable proxy_arp, so that any local requests are being responded to by the raspberry pi router. I believe I have to set up the routing tables inside the raspberry pi, don't I?

Any thoughts will be appreciated.


Unfortunately, the Proxy ARP was not helpful in reaching my desired control on the ARP packets within the network. Anyway. I have seen a solution (OpenVPN client-to-client) but I did not implement it yet, I will back to this question to post if it did work or not.

pl flag
There is no 'nat' table in the FORWARD chain. Does you command throw an error? What are you trying to achieve, send the packet to a user space program?
zezo mehdawi avatar
pe flag
Thanks for your replys! i have updated my question. please inform me if you need more details
zezo mehdawi avatar
pe flag
@BrahimGaabab Exactly, im trying to send the packets that match a specific destination to the userspace.
zezo mehdawi avatar
pe flag
@BrahimGaabab What is the suitable iptables rule should I add to the previous rules?
Score:0
pl flag

When using netfilter, you have to understand how a packet flows throughout the kernel, i.e. which chains (similar to a 'road check point') it visits, and which kinds of processing it gets in each chain (referred to with the term table). Processing and chains are shown in this illustration, each box have the chain in its lower part and the processing type in the upper one. (You have to focus on the Network layer.) Depending on the packet path, only a subset of chains are involved: packets flowing through your raspberry will only goes through PREROUTING, FORWARD and POSTROUTING.

Having FORWARD chain, does not imply that it routes packets. You have to enable it using the command sysctl -w net.ipv4.ip_forward=1 (non persistent).

Moreover, as shown in the figure, there is no nat processing in the FORWARD chain, only mangle and filter, so the command

iptables -t nat -d <list of IPs> -A FORWARD -j NFQUEUE --queue-num 1

is incorrect.

Packet processing for each table/chain is actually driven by an ordered list of rules, the table, that you define with the iptables command. Each rule is made up of matching criteria and an action which depends on the table type (nfqueue is only allowed in filter tables, masquerade in nat tables, etc.)

Now, coming to nfqueue. We use such action when further processing of some packets is to be made outside the kernel, by a program you create yourself (see a python example here, for instance, in intrusion detection systems. Packets are put on a queue (identified with 16-bits number), processed then returned to the kernel to resume their flow at the next table/chain. (They can also be dropped in user space). In a common scenario, you'll typically want to send only accepted packets to userspace (those filtered are ignored at kernel level). You have to be careful about your exact needs, which are not yet clear. I'll try to give an example to explain how it works using your scenario:

sudo iptables -F
sudo iptables -t nat -F
sudo iptables -t nat -A POSTROUTING -o $eth -j MASQUERADE
sudo iptables -A FORWARD -i $eth -o $wlan -m state --state RELATED,ESTABLISHED -j NFQUEUE --queue-num 1
sudo iptables -A FORWARD -i $wlan -o $eth -j NFQUEUE --queue-num 1

This means that packets flowing between $eth and $wlan will be put on the same queue. You have to make sure that some program is handling queue no. 1 and ready to process packets.

zezo mehdawi avatar
pe flag
Thanks for the wonderful explanation, it really cleared up my mind. But the thing is your suggested rules match only the traffic that goes to the internet. As I'm building an IDS, I'm looking to match the internal traffic only. I'm testing if it does work by hosting a web page from my laptop and then trying to reach it from my phone. In this scenario, NFQUEUE should detect that request!
zezo mehdawi avatar
pe flag
here is my own log file after applying your suggested rules. That may clarify the results. Connection IP: 157.240.196.60 Connection MAC: e8:cd:2d:a9:01:f7 Connection IP: 20.93.28.56 Connection MAC: e8:cd:2d:a9:01:f7 Connection IP: 172.64.154.162 Connection MAC: e8:cd:2d:a9:01:f7 –
pl flag
Do not expect me to figure out your scenario and which traffic is to be queued :-) So, laptop and phone are on the wlan side within the same IP subnet? If this is the case, you probably need to rely on the wlan interface driver to capture and queue packet to user space, because netfilter does not allow `NFQUEUE` target for thernet frames. Do you want to make forwarding decision there, is this is just to copy frames?
zezo mehdawi avatar
pe flag
Yes exactly, but the purpose is not to copy it is to accept or drop the packet. Do not worry about my userspace program it works perfectly. i just need to add the iptables rule to queue for it. I can set a zoom session for you to show the details if you wish.
pl flag
One way is to force packets to go through the router IP driver by making your router advertise the wlan interface's MAC in the LAN. I'll let you know if available tonight or yomorrow morning.
zezo mehdawi avatar
pe flag
Alright! Thanks for your kindness
zezo mehdawi avatar
pe flag
Hello sir, hope you contact me soon so I can meet my project deadline :(
pl flag
@zezomehdawi, I am available now. In order to force packets through the router, you have to advertise its MAC within your WLAN. You can do it by enabling ARP proxying on router.
zezo mehdawi avatar
pe flag
Actually, i have never installed an arp proxy. You can instruct me here or we can continue on this zoom session. It is your choice. https://iauvle.zoom.us/j/7036217353
pl flag
It is not working for some reason (you have corporate zoom account?). You can setup a different service.
zezo mehdawi avatar
pe flag
i have another one, just give me a sec
zezo mehdawi avatar
pe flag
This is my personal account: https://us04web.zoom.us/j/7559513161?pwd=bW9JVWJhdG95Mkd4RTFZM2hPQitEUT09
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.