限制ip訪問數據庫的方法


一、需求

限制某個ip或某個ip段才能訪問Oracle數據庫

  • 通過sqlnet.ora
  • 通過/etc/hosts.deny和/etc/hosts.allow
  • 通過iptables
  • 通過Oracle觸發器限制

二、實現方法

數據庫服務器ip地址為192.168.31.71

1. 通過sqlnet.ora

a. 關閉數據庫服務器上的防火牆,修改sqlnet.ora文件

該文件放在$ORACLE_HOME/network/admin下,如果沒有就在該目錄下創建一個即可

添加以下兩行

tcp.validnode_checking = yes

tcp.invited_nodes = (192.168.31.71, 192.168.31.77)

這里需要注意的是必須把本機ip地址加進來(不能寫成localhost和127.0.0.1),否則監聽啟動會報錯

b. 重啟監聽,讓sqlnet.ora的修改生效

lsnrctl stop

lsnrctl start

設置之后就只有這兩個ip地址192.168.31.71, 192.168.31.77能訪問數據庫,其它ip地址訪問會報ORA-12547: TNS:lost contact錯誤

tcp.invited_nodes的意思是開通白名單,不在白名單中的一律拒絕訪問,它也可以寫成(192.168.31.*, 192.168.31.0/24)等方式,表明這個網段都能訪問

另外還有個參數tcp.excluded_nodes,表示黑名單

2. 通過/etc/hosts.deny和/etc/hosts.allow

sqlnet.ora屬於數據庫層面的限制,但如果一個ip能夠使用root或者oracle,ssh到這台數據庫服務器的話,那么它依然能夠訪問數據庫。為了避免這種情況,這時就需要通過/etc/hosts.allow和/etc/hosts.deny去限制某個ip或者ip段才能ssh訪問數據庫服務器

先刪除前面實驗添加的sqlnet.ora,然后重啟監聽

lsnrctl stop

lsnrctl start

a. 修改/etc/hosts.deny

在文件尾部添加一行

all:all:deny

第一個all表示禁掉所有使用tcp_wrappers庫的服務,舉例來說就是ssh,telnet等服務
第二個all表示所有網段

b. 修改/etc/hosts.allow

在前面一步中禁掉所有的網段,所以在這一步中要開通指定的網段

修改/etc/hosts.allow,在文件尾部添加

all:192.168.31.71:allow

all:192.168.31.47:allow

格式與hosts.deny一樣,第一行表示把本機放開,第二行表示給.47開通白名單如果用其他機器ssh或telnet連接71這個機器,就會出現如下報錯

[oracle@oracle19c1 ~]$ ssh 192.168.31.71

ssh_exchange_identification: read: Connection reset by peer

[oracle@oracle19c1 ~]$ telnet 192.168.31.71 22

Trying 192.168.31.71...

Connected to 192.168.31.71.

Escape character is '^]'.

Connection closed by foreign host.

連數據庫卻不受影響,因為數據庫服務不歸hosts.deny和hosts.allow限制

[oracle@oracle19c1 ~]$ sqlplus sys/xxxxx@192.168.31.71:1521/orcltest as sysdba

SQL*Plus: Release 19.0.0.0.0 - Production on Sun Aug 16 23:12:492020

Version 19.3.0.0.0

Copyright (c) 1982, 2019, Oracle. All rights reserved.

Connected to:

Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

其中ip地址也可以換成以下的寫法

通配符的形式 192.168.31.*表示192.168.31這個網段

網段/掩碼 192.168.31.0/255.255.255.0也表示192.168.31這個網段

3. 通過iptables

sqlnet.ora能夠限制數據庫的訪問,/etc/hosts.deny和/etc/hosts.allow能夠限制ssh的訪問,使用linux自帶的防火牆功能既能限制數據庫的訪問,也能限制ssh的訪問呢

使用root執行以下命令

service iptables start # 打開防火牆服務

iptables -I INPUT -s 192.168.31.0/24 -p tcp --dport 1521 -j ACCEPT # 允許192.168.31網段的ip訪問本機1521端口

iptables -I INPUT ! -s 192.168.31.0/24 -p tcp --dport 22 -j DROP # 拒絕非192.168.31網段的ip訪問本機22端口

service iptables save # 規則保存到配置文件/etc/sysconfig/iptables中

 

這樣就同時限制了其它ip對服務器的ssh和數據庫訪問

  • iptables -L -n --line-numbers # 查看當前系統中的iptables
  • iptables -D INPUT 2 # 刪除input鏈中編號為2的規則,編號數字可以過上一個命令得到
  1. 觸發器限制登錄

還可以使用觸發器來限制ip訪問數據庫,代碼如下:

create or replace trigger logon_ip_control
after logon on database
declare
  ip STRING(30);
  user STRING(30);
begin
SELECT SYS_CONTEXT('USERENV','SESSION_USER') into user from dual;
SELECT SYS_CONTEXT('USERENV','IP_ADDRESS') into ip from dual;
if user='EPAY_USER'
  THEN
      IF ip not in ('192.168.219.20','192.168.219.22') 
      THEN raise_application_error(-20001,'User '||user||' is not allowed to connect from '||ip);
      END IF;
END IF;
end;


該觸發器對用戶EPAY_USER進行了IP限制(只允許192.168.31.71192.168.31.77,如果需要設置IP段,用%或?代替即可,如'192.168.31.%‘)。

三、總結

1.只是限制其它ip對數據庫的訪問,使用sqlnet.ora

2.限制其它ip對數據庫所在服務器上的ssh連接,使用/etc/hosts.deny和/etc/hosts.allow

3.使用sqlnet.ora/etc/hosts.deny/etc/hosts.allow配合,效果與直接使用iptables限制一樣

 

 ————歡迎大家溝通、指正---- QQ群:1071136320————

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM