iptables拓展ipset

語法:
ipset [ OPTIONS ] COMMAND [ COMMAND-OPTIONS ]
COMMANDS := { create | add | del | test | destroy | list | save | restore | flush | rename | swap | help | version | - }
OPTIONS := { -exist | -output { plain | save | xml } | -quiet | -resolve | -sorted | -name | -terse | -file filename }
ipset create SETNAME TYPENAME [ CREATE-OPTIONS ]
ipset add SETNAME ADD-ENTRY [ ADD-OPTIONS ]
ipset del SETNAME DEL-ENTRY [ DEL-OPTIONS ]
ipset test SETNAME TEST-ENTRY [ TEST-OPTIONS ]
ipset destroy [ SETNAME ]
ipset list [ SETNAME ]
ipset save [ SETNAME ]
ipset restore
ipset flush [ SETNAME ]
ipset rename SETNAME-FROM SETNAME-TO
ipset swap SETNAME-FROM SETNAME-TO
ipset help [ TYPENAME ]
ipset version


TYPENAME := method:datatype[,datatype[,datatype]]
當(dāng)前使用的method方法是bitmap、hash, datatypes的list有 ip, net, mac, port and iface, botmap和list用于一個固定大小的storage,
The bitmap and list types use a fixed sized storage. The hash types use a hash to store the elements. In order to avoid clashes in the hash, a limited number of chaining, and if that is exhausted, the doubling of the hash size is performed when adding entries by the ipset command. When entries added by the SET target of iptables/ip6tables, then the hash size is fixed and the set won't be duplicated, even if the new entry cannot be added to the set.


一般的創(chuàng)建和添加操作:
timeout設(shè)置超時時間,如果設(shè)置為0,表示永久生效,超時時間可以通過 -exist來進(jìn)行修改:

ipset create test hash:ip timeout 300
ipset add test 192.168.0.1 timeout 60

ipset -exist add test 192.168.0.1 timeout 600

counters, packets, bytes:
如果不指定packets和bytes的話,會在初始化的時候變?yōu)?,或者在創(chuàng)建set的時候指定value:
ipset create foo hash:ip counters

ipset add foo 192.168.1.1 packets 42 bytes 1024

comment(擴(kuò)展):
所有的set類型都支持comments,此模塊是用來做注釋,相當(dāng)于git中的-m:

ipset create foo hash:ip comment
ipset add foo 192.168.1.1/24 comment "allow access to SMB share on \\fileserv\"

the above would appear as: "allow access to SMB share on \fileserv"

hashsize:
這個模塊用于創(chuàng)建set時候的命令(適用于所有hash set),默認(rèn)大小為1024,設(shè)置的時候必須是2的冪次方,不對話kernal會四舍五入:
ipset create test hash:ip hashsize 1536
maxelem:
這個模塊用于 所有的hash type sets create動作,這個是用來指定存出來set中最大元素的數(shù)量,默認(rèn)是65535:

           ipset create test hash:ip maxelem 2048

family(inet|inet6):
用于所有hash type sets(除了hash:mac)的create動作, It defines the protocol family of the IP addresses to be stored in the set ,對于inet家族,您可以通過在該條目的IP地址部分指定一個范圍或一個IPv4地址網(wǎng)絡(luò)來添加或刪除多個條目。

  •      ipaddr* := { *ip* | *fromaddr*-*toaddr* | *ip*/*cidr* }
    
  •      netaddr* := { *fromaddr*-*toaddr* | *ip*/*cidr* }
    

Example:

ipset create test hash:ip family inet6

hash:ip:
set type用一個hash存儲ip或者網(wǎng)絡(luò)地址,zero value是不能被存儲到hash:ip類型中的:
CREATE-OPTIONS := [ family { inet | inet6 } ] | [ hashsize value ] [ maxelem value ] [ netmask cidr ] [ timeout value ] [ counters ] [ comment ] [ skbinfo ]
ADD-ENTRY := ipaddr(要添加的ip地址)
ADD-OPTIONS := [ timeout value ] [ packets value ] [ bytes value ] [ comment string ] [ skbmark value ] [ skbprio value ] [ skbqueue value ]

DEL-ENTRY := ipaddr(要刪除的ip地址)

TEST-ENTRY := ipaddr
netmask cidr:
當(dāng)選定的netmask被指定的時候, network addresses將代替 IP host addresses存儲在set中,cidr的值對于iPv4是1-32,ipv6是1-128, An IP address will be in the set if the network address, which is resulted by masking the address with the netmask, can be found in the set. Examples:

ipset create foo hash:ip netmask 30
ipset add foo 192.168.1.0/24

ipset test foo 192.168.1.2

nomatch:
The hash set types which can store net type of data (i.e. hash:net) support the optional nomatch option when adding entries. When matching elements in the set, entries marked as nomatch are skipped as if those were not added to the set, which makes possible to build up sets with exceptions. See the example at hash type hash:net below.

When elements are tested by ipset, the nomatch flags are taken into account. If one wants to test the existence of an element marked with nomatch in a set, then the flag must be specified too.
總結(jié)下來就是,這個通常與hash:net搭配使用,用來跳過 hash:net指定的ip netmask address.

hash:net:
** 這個是用一個hash存儲不同大小的** IP network addresses,具有0前綴的的網(wǎng)絡(luò)地址不能被存儲到sets中:

CREATE-OPTIONS := [ family { inet | inet6 } ] | [ hashsize value ] [ maxelem value ] [ timeout value ] [ counters ] [ comment ] [ skbinfo ]
ADD-ENTRY := netaddr
ADD-OPTIONS := [ timeout value ] [ nomatch ] [ packets value ] [ bytes value ] [ comment string ] [ skbmark value ] [ skbprio value ] [ skbqueue value ]
DEL-ENTRY := netaddr
TEST-ENTRY := netaddr
where netaddr := ip[/cidr]

Example:

ipset create foo hash:net
ipset add foo 192.168.0.0/24
ipset add foo 10.1.0.0/16
ipset add foo 192.168.0/24

ipset add foo 192.168.0/30 nomatch

hash:net,net
** 這種set type是用一個hash存儲成對的不同大小的ip network address,** Bear in mind(記住)第一個參數(shù)比第二個參數(shù)優(yōu)先,加入有明確的第一和第二個參數(shù),很可能 nomatch就不生效了,帶0前綴的地址也是無法存儲的。

CREATE-OPTIONS := [ family { inet | inet6 } ] | [ hashsize value ] [ maxelem value ] [ timeout value ] [ counters ] [ comment ] [ skbinfo ]
ADD-ENTRY := netaddr,netaddr
ADD-OPTIONS := [ timeout value ] [ nomatch ] [ packets value ] [ bytes value ] [ comment string ] [ skbmark value ] [ skbprio value ] [ skbqueue value ]
DEL-ENTRY := netaddr,netaddr
TEST-ENTRY := netaddr,netaddr
where netaddr := ip[/cidr]
Example:

ipset create foo hash:net,net
ipset add foo 192.168.0.0/24,10.0.1.0/24
ipset add foo 10.1.0.0/16,10.255.0.0/24
ipset add foo 192.168.0/24,192.168.54.0-192.168.54.255

ipset add foo 192.168.0/30,192.168.64/30 nomatch

hash:ip,port
** ** The hash:ip,port set type uses a hash to store IP address and port number pairs. The port number is interpreted together with a protocol (default TCP) and zero protocol number cannot be used.
Examples:

ipset create foo hash:ip,port
ipset add foo 192.168.1.0/24,80-82
ipset add foo 192.168.1.1,udp:53
ipset add foo 192.168.1.1,vrrp:0

ipset test foo 192.168.1.1,80

創(chuàng)建一個集合
ipset create vader hash:ip

這條命令創(chuàng)建了名為 vader 的集合,以 hash 方式存儲,存儲內(nèi)容是 IP 地址。
添加 iptables 規(guī)則
iptables -I INPUT -m set --match-set vader src -j DROP

如果源地址(src)屬于 vader 這個集合,就進(jìn)行 DROP 操作。這條命令中,vader 是作為黑名單的,如果要把某個集合作為白名單,添加一個 ‘!’ 符號就可以。
iptables -I INPUT -m set ! --match-set vader src -j DROP

hash:net,port

The hash:net,port set type uses a hash to store different sized IP network address and port pairs. The port number is interpreted together with a protocol (default TCP) and zero protocol number cannot be used. Network address with zero prefix size is not accepted either.

Examples:

ipset create foo hash:net,port
ipset add foo 192.168.0/24,25
ipset add foo 10.1.0.0/16,80

ipset test foo 192.168.0/24,25

hash:ip,port,ip

The hash:ip,port,ip set type uses a hash to store IP address, port number and a second IP address triples. The port number is interpreted together with a protocol (default TCP) and zero protocol number cannot be used.

         Examples:

ipset create foo hash:ip,port,ip
ipset add foo 192.168.1.1,80,10.0.0.1

ipset test foo 192.168.1.1,udp:53,10.0.0.1

hash:ip,port,net

The hash:ip,port,net set type uses a hash to store IP address, port number and IP network address triples. The port number is interpreted together with a protocol (default TCP) and zero protocol number cannot be used. Network address with zero prefix size cannot be stored either.、

       Examples:

ipset create foo hash:ip,port,net
ipset add foo 192.168.1,80,10.0.0/24
ipset add foo 192.168.2,25,10.1.0.0/16

ipset test foo 192.168.1,80.10.0.0/24

hash:ip,mark

The hash:ip,mark set type uses a hash to store IP address and packet mark pairs.

Examples:

ipset create foo hash:ip,mark
ipset add foo 192.168.1.0/24,555
ipset add foo 192.168.1.1,0x63

ipset add foo 192.168.1.1,111236

hash:net,port,net

The hash:net,port,net set type behaves similarly to hash:ip,port,net but accepts a cidr value for both the first and last parameter. Either subnet is permitted to be a /0 should you wish to match port between all destinations.

Examples:

ipset create foo hash:net,port,net
ipset add foo 192.168.1.0/24,0,10.0.0/24
ipset add foo 192.168.2.0/24,25,10.1.0.0/16

ipset test foo 192.168.1.1,80,10.0.0.1

hash:net,iface

The hash:net,iface set type uses a hash to store different sized IP network address and interface name pairs.

Examples:

ipset create foo hash:net,iface
ipset add foo 192.168.0/24,eth0
ipset add foo 10.1.0.0/16,eth1

ipset test foo 192.168.0/24,eth0

list:set
** ** The list:set type uses a simple list in which you can store set names.

By the set match or SET target of netfilter you can test, add or delete entries in the sets added to the list:set type of set. The match will try to find a matching entry in the sets and the target will try to add an entry to the first set to which it can be added. The number of direction options of the match and target are important: sets which require more parameters than specified are skipped, while sets with equal or less parameters are checked, elements added/deleted. For example if a and b are list:set type of sets then in the command

iptables -m set --match-set a src,dst -j SET --add-set b src,dst

iptables想所有人開放9200端口:

[root@centos2 ~]# iptables -A INPUT -p tcp --dport 9200 -j ACCEPT
[root@centos2 ~]# iptables -A OUTPUT -p tcp --sport 9200 -j ACCEPT

添加 iptables 規(guī)則
iptables -I INPUT -m set --match-set vader src -j DROP

如果源地址(src)屬于 vader 這個集合,就進(jìn)行 DROP 操作。這條命令中,vader 是作為黑名單的,如果要把某個集合作為白名單,添加一個 ‘!’ 符號就可以。

iptables -I INPUT -m set ! --match-set yoda src -j DROP

創(chuàng)建一個新的ipset
ipset create openapi hash:net
1、查看已創(chuàng)建的ipset

2、ipset默認(rèn)可以存儲65536個element,使用maxelem指定數(shù)量

ipset create openapi hash:net maxelem 1000000
ipset list
3、加入一個黑名單ip
ipset add openapi 180.156.238.246
4、創(chuàng)建防火墻規(guī)則,與此同時,openapi這個ipset里的ip都無法訪問22端口
iptables -I INPUT -m set --match-set openapi src -p tcp --destination-port 22 -j DROP
service iptables save
5、去除黑名單,與此同時,又可以訪問了
ipset del openapi 180.156.238.246
6、將ipset規(guī)則保存到文件
ipset save openapi -f openapi.txt
7、刪除ipset
ipset destroy openapi
8、導(dǎo)入ipset規(guī)則
ipset restore -f openapi.txt

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,885評論 6 541
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,312評論 3 429
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,993評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,667評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 72,410評論 6 411
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,778評論 1 328
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,775評論 3 446
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,955評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,521評論 1 335
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 41,266評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,468評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,998評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,696評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,095評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,385評論 1 294
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,193評論 3 398
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,431評論 2 378

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,823評論 18 139
  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc閱讀 2,899評論 0 0
  • Redis 配置文件示例 注意:想要讀取配置文件,Redis的第一個參數(shù)必須是文件的路徑 ./redis-serv...
    起個名忒難閱讀 1,220評論 0 1
  • IBinder 與 Binder 一個在同進(jìn)程的對象的抽象是 Object,但這個對象是不能被跨進(jìn)程使用的,要想跨...
    小帝Ele閱讀 576評論 0 3
  • 一、 每一個在你的生命里出現(xiàn)的人,都有原因。喜歡你的人給了你溫暖和勇氣。你喜歡的人讓你學(xué)會了愛和自持。你不喜歡的人...
    瘋子Selina閱讀 99評論 0 0