UFW is just a front end for iptables. Typically, the generated rule set has as RELATED,ESTABLISHED ACCEPT rule early in the INPUT chain. If you add a specific DROP rule before that generic rule, the packets should be intercepted and dropped.
In this example, I will add a rule and drop subsequent packets from 192.168.111.122 to my main test server at 192.168.111.132. For demonstration, I have a very simple iptables rule set:
$ sudo iptables -xvnL
Chain INPUT (policy DROP 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
2 100 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
0 0 LOG tcp -- br0 * 0.0.0.0/0 0.0.0.0/0 ctstate INVALID LOG flags 0 level 6 prefix "IINVALID:"
0 0 DROP tcp -- br0 * 0.0.0.0/0 0.0.0.0/0 ctstate INVALID
0 0 LOG tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp flags:!0x17/0x02 ctstate NEW LOG flags 0 level 6 prefix "NEW TCP no SYN:"
0 0 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp flags:!0x17/0x02 ctstate NEW
159 11289 ACCEPT all -- br0 * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
1 52 ACCEPT tcp -- br0 * 0.0.0.0/0 0.0.0.0/0 ctstate NEW tcp dpt:22
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
And am currently connected via SSH from 2 computers:
$ who
doug pts/0 2023-01-05 15:55 (192.168.111.1)
doug pts/1 2023-01-05 15:56 (192.168.111.122)
Now add the new rule to DROP packets from 192.168.111.122:
$ sudo iptables -I INPUT 2 -s 192.168.111.122 -i br0 -j DROP
The SSH session terminates (might take a while).
$ client_loop: send disconnect: Connection reset
And packets are being dropped:
$ sudo iptables -xvnL
Chain INPUT (policy DROP 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
26 2926 DROP all -- br0 * 192.168.111.122 0.0.0.0/0
0 0 LOG tcp -- br0 * 0.0.0.0/0 0.0.0.0/0 ctstate INVALID LOG flags 0 level 6 prefix "IINVALID:"
0 0 DROP tcp -- br0 * 0.0.0.0/0 0.0.0.0/0 ctstate INVALID
0 0 LOG tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp flags:!0x17/0x02 ctstate NEW LOG flags 0 level 6 prefix "NEW TCP no SYN:"
0 0 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp flags:!0x17/0x02 ctstate NEW
100 8437 ACCEPT all -- br0 * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
1 52 ACCEPT tcp -- br0 * 0.0.0.0/0 0.0.0.0/0 ctstate NEW tcp dpt:22
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Note that the session still shows as logged in, but it will eventually be deleted:
$ who
doug pts/0 2023-01-05 15:55 (192.168.111.1)
doug pts/1 2023-01-05 15:56 (192.168.111.122)