LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Security
User Name
Password
Linux - Security This forum is for all security related questions.
Questions, tips, system compromises, firewalls, etc. are all included here.

Notices


Reply
  Search this Thread
Old 02-08-2024, 03:52 PM   #1
Jason.nix
Member
 
Registered: Feb 2023
Posts: 565

Rep: Reputation: 10
Post iptables and a list of addresses


Hello,
Can iptables read from a file the list of IP or MAC addresses that are supposed to be allowed to connect or not to connect to the server? For example, if I have 100 MAC addresses, do I need to repeat the following command 100 times?
Code:
# iptables -A INPUT -p tcp --destination-port 22 -m mac --mac-source "MAC Address" -j ACCEPT
Thank you.
 
Old 02-08-2024, 05:02 PM   #2
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,803

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by Jason.nix View Post
Hello,
Can iptables read from a file the list of IP or MAC addresses that are supposed to be allowed to connect or not to connect to the server? For example, if I have 100 MAC addresses, do I need to repeat the following command 100 times?
Code:
# iptables -A INPUT -p tcp --destination-port 22 -m mac --mac-source "MAC Address" -j ACCEPT
Write a bash script that accepts the list of IP addresses (or MAC addresses) and loops through the list to execute the iptables command for each.

Hope this helps...
 
Old 02-10-2024, 05:50 AM   #3
Jason.nix
Member
 
Registered: Feb 2023
Posts: 565

Original Poster
Rep: Reputation: 10
Quote:
Originally Posted by rnturn View Post
Write a bash script that accepts the list of IP addresses (or MAC addresses) and loops through the list to execute the iptables command for each.

Hope this helps...
Hello,
Thank you so much for your reply.
I found a script by searching on google and modified it as below:
Code:
for MAC in 'cat mac_addresses_file'; do

  **iptables -A INPUT -p tcp --destination-port 22 -m mac --mac-source $MAC -j ACCEPT**
Is it correct?
 
Old 02-11-2024, 01:45 PM   #4
jayjwa
Member
 
Registered: Jul 2003
Location: NY
Distribution: Slackware, Termux
Posts: 786

Rep: Reputation: 250Reputation: 250Reputation: 250
ipset is the right tool for this. Example snippets from a script I have:

Code:
ipset create blocklist hash:net family inet hashsize 64 maxelem 512  
ipset add blocklist 8.0.0.0/8                                        
ipset list blocklist                                                 
ipset -f /etc/ipset/blocklist.ipset save                             
ipset -f /etc/ipset/blocklist.ipset restore

...

# Color codes for fancy output.
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m'

test -f /etc/ipset/blocklist.ipset && {
		printf "${GREEN}[+] Log/blocking networks from the general blocklist.${NC}\\n"
		ipset -exist -file /etc/ipset/blocklist.ipset restore
		iptables -A INPUT -m set --match-set blocklist src -m limit -j LOG --log-level 7 --log-prefix "Ipset blocklist: "
		iptables -A INPUT -m set --match-set blocklist src -j DROP
}
See the documentation for ipset. It's made for this use, instead of 100's of iptables rules. I default allow, and only block abusive hosts, but you can do it the other way as well.
 
1 members found this post helpful.
Old 02-12-2024, 04:51 AM   #5
Jason.nix
Member
 
Registered: Feb 2023
Posts: 565

Original Poster
Rep: Reputation: 10
Quote:
Originally Posted by jayjwa View Post
ipset is the right tool for this. Example snippets from a script I have:

Code:
ipset create blocklist hash:net family inet hashsize 64 maxelem 512  
ipset add blocklist 8.0.0.0/8                                        
ipset list blocklist                                                 
ipset -f /etc/ipset/blocklist.ipset save                             
ipset -f /etc/ipset/blocklist.ipset restore

...

# Color codes for fancy output.
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m'

test -f /etc/ipset/blocklist.ipset && {
		printf "${GREEN}[+] Log/blocking networks from the general blocklist.${NC}\\n"
		ipset -exist -file /etc/ipset/blocklist.ipset restore
		iptables -A INPUT -m set --match-set blocklist src -m limit -j LOG --log-level 7 --log-prefix "Ipset blocklist: "
		iptables -A INPUT -m set --match-set blocklist src -j DROP
}
See the documentation for ipset. It's made for this use, instead of 100's of iptables rules. I default allow, and only block abusive hosts, but you can do it the other way as well.
Hi,
Thank you so much for your reply.
When your IP addresses are not in the same range, then this script is not useful!
 
Old 02-12-2024, 10:44 AM   #6
Jason.nix
Member
 
Registered: Feb 2023
Posts: 565

Original Poster
Rep: Reputation: 10
Hello,
How can I make it possible to create only one session from each MAC address?

Thank you.
 
Old 02-12-2024, 12:39 PM   #7
jayjwa
Member
 
Registered: Jul 2003
Location: NY
Distribution: Slackware, Termux
Posts: 786

Rep: Reputation: 250Reputation: 250Reputation: 250
Quote:
When your IP addresses are not in the same range, then this script is not useful!
Not true. That's just the example.

Code:
su -c 'ipset list blocklist'
Password: 
Name: blocklist
Type: hash:net
Revision: 7
Header: family inet hashsize 64 maxelem 512 bucketsize 12 initval 0xb5dfcf22
Size in memory: 1608
References: 2
Number of entries: 27
Members:
194.165.16.10
45.129.14.99
80.94.92.241
193.42.33.7
185.225.75.182
164.92.106.15
85.217.144.82
13.40.5.44
194.165.16.73
147.78.103.108
37.139.129.238
91.92.244.201
103.219.154.96
165.227.214.182
194.165.16.76
91.92.242.137
165.227.110.45
93.112.20.159
87.120.84.80
94.23.30.184
195.133.40.42
194.87.151.143
84.54.50.104
87.121.221.75
195.178.120.5
194.33.191.197
87.120.84.30
You can manually add them to the text file blocklist, use the ipset add command, or work ipset add into a script.
 
Old 02-14-2024, 11:57 AM   #8
Jason.nix
Member
 
Registered: Feb 2023
Posts: 565

Original Poster
Rep: Reputation: 10
Quote:
Originally Posted by jayjwa View Post
Not true. That's just the example.

Code:
su -c 'ipset list blocklist'
Password: 
Name: blocklist
Type: hash:net
Revision: 7
Header: family inet hashsize 64 maxelem 512 bucketsize 12 initval 0xb5dfcf22
Size in memory: 1608
References: 2
Number of entries: 27
Members:
194.165.16.10
45.129.14.99
80.94.92.241
193.42.33.7
185.225.75.182
164.92.106.15
85.217.144.82
13.40.5.44
194.165.16.73
147.78.103.108
37.139.129.238
91.92.244.201
103.219.154.96
165.227.214.182
194.165.16.76
91.92.242.137
165.227.110.45
93.112.20.159
87.120.84.80
94.23.30.184
195.133.40.42
194.87.151.143
84.54.50.104
87.121.221.75
195.178.120.5
194.33.191.197
87.120.84.30
You can manually add them to the text file blocklist, use the ipset add command, or work ipset add into a script.
Hi,
Thanks again.
You used IP range 8.0.0.0/8.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: How to Check the Available Network Interfaces, Associated IP Addresses, MAC Addresses, and Interface Speed on Linux LXer Syndicated Linux News 0 06-08-2020 08:13 AM
Trying to be figure out the total Usable IP Addresses using whatmask and adding multiple IP addresses in a text file with a script Shakespearr Linux - Newbie 7 11-02-2018 05:28 PM
[SOLVED] How to - List of IP/MAC addresses (etc) to block/allow in iptables Basher52 Linux - Software 3 04-03-2015 07:34 PM
Can I use iptables to limit bandwidth to certain IP addresses and MAC addresses baronobeefdip Linux - Networking 2 01-07-2014 07:36 PM
iptables v1.2.9: Unknown arg `/sbin/iptables' Try `iptables -h' or 'iptables --help' Niceman2005 Linux - Security 4 12-29-2005 08:20 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Security

All times are GMT -5. The time now is 09:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration