首页 > 开发 > 综合 > 正文

用UTL_INADDR包获取已经连接用户的IP地址

2024-07-21 02:43:19
字体:
来源:转载
供稿:网友
许多人都知道,通过SYS_CONTEXT函数可以获得部分信息,当前用户的ip等信息则可以通过下面的命令来进行获取:

SQL> select sys_context('userenv','host') from dual; SYS_CONTEXT('USERENV','HOST')---------------------------------------------------WORKGROUP/GQGAISQL> select sys_context('userenv','ip_address') from dual;SYS_CONTEXT('USERENV','IP_ADDR---------------------------------------------------172.16.34.20

假如你需要获取其它session的地址等信息,SYS_CONTEXT函数则只能通过LOGON触发器来完成。

在没有触发器记录的前提下,你可以通过UTL_INADDR Package来实现。

下面具体介绍一下UTL_INADDR包获取ip等信息的工作原理。

首先,在数据库中进行下面的查询:

[Oracle@jumper oracle]$ sqlplus "/ as sysdba"SQL*Plus: Release 9.2.0.4.0 - PRoduction on Wed Oct 25 11:24:22 2006Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.Connected to:Oracle9i Enterprise Edition Release 9.2.0.4.0 - ProductionWith the Partitioning optionJServer Release 9.2.0.4.0 - ProductionSQL> ![oracle@jumper oracle]$ ps -ef|grep sqloracle 14700 14663 1 11:24 pts/0 00:00:00 sqlplus oracle 14732 14702 0 11:24 pts/0 00:00:00 grep sql[oracle@jumper oracle]$ ps -ef|grep LOoracle 14701 14700 0 11:24 ? 00:00:00 oracleeygle (DESCRIPTION=(LOCAL=YES)(ADDRESS=(PROTOCOL=beq)))oracle 14734 14702 0 11:24 pts/0 00:00:00 grep LO[oracle@jumper oracle]$ exitexitSQL> SELECT UTL_INADDR.get_host_address('www.anysql.net') from dual;UTL_INADDR.GET_HOST_ADDRESS('WWW.ANYSQL.NET')---------------------------------------------------------208.113.151.109

linux系统中,你可以通过strace跟踪此进程,得到下面的堆栈信息:

[oracle@jumper oracle]$ strace -p 14701Process 14701 attached - interrupt to quitread(7, "/0/313/0/0/6/0/0/0/0/0/3^/10a/200/0/0/0/0/0/0@/342/22/10"..., 2064) = 203gettimeofday({1161746697, 269895}, NULL) = 0getrusage(RUSAGE_SELF, {ru_utime={0, 30000}, ru_stime={0, 10000}, ...}) = 0getrusage(RUSAGE_SELF, {ru_utime={0, 40000}, ru_stime={0, 10000}, ...}) = 0gettimeofday({1161746697, 270542}, NULL) = 0gettimeofday({1161746697, 270670}, NULL) = 0getrusage(RUSAGE_SELF, {ru_utime={0, 40000}, ru_stime={0, 10000}, ...}) = 0getrusage(RUSAGE_SELF, {ru_utime={0, 40000}, ru_stime={0, 10000}, ...}) = 0gettimeofday({1161746697, 271614}, NULL) = 0gettimeofday({1161746697, 271748}, NULL) = 0getrusage(RUSAGE_SELF, {ru_utime={0, 40000}, ru_stime={0, 10000}, ...}) = 0getrusage(RUSAGE_SELF, {ru_utime={0, 40000}, ru_stime={0, 10000}, ...}) = 0gettimeofday({1161746697, 272347}, NULL) = 0gettimeofday({1161746697, 272699}, NULL) = 0getrusage(RUSAGE_SELF, {ru_utime={0, 40000}, ru_stime={0, 10000}, ...}) = 0gettimeofday({1161746697, 272989}, NULL) = 0gettimeofday({1161746697, 273140}, NULL) = 0gettimeofday({1161746697, 273273}, NULL) = 0getrusage(RUSAGE_SELF, {ru_utime={0, 40000}, ru_stime={0, 10000}, ...}) = 0gettimeofday({1161746697, 273771}, NULL) = 0gettimeofday({1161746697, 275526}, NULL) = 0getpid() = 14701open("/etc/resolv.conf", O_RDONLY) = 12fstat64(12, {st_mode=S_IFREG|0644, st_size=46, ...}) = 0mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb6fba000read(12, "search hurray.com.cn/nnameserver "..., 4096) = 46read(12, "", 4096) = 0close(12) = 0munmap(0xb6fba000, 4096) = 0socket(PF_UNIX, SOCK_STREAM, 0) = 12connect(12, {sa_family=AF_UNIX, path="/var/run/.nscd_socket"}, 110) = -1 ENOENT (No such file or directory)close(12) = 0open("/etc/host.conf", O_RDONLY) = 12fstat64(12, {st_mode=S_IFREG|0644, st_size=17, ...}) = 0mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb6fba000read(12, "order hosts,bind/n", 4096) = 17read(12, "", 4096) = 0close(12) = 0munmap(0xb6fba000, 4096) = 0futex(0xb71a1a20, FUTEX_WAKE, 2147483647) = 0open("/etc/hosts", O_RDONLY) = 12fcntl64(12, F_GETFD) = 0fcntl64(12, F_SETFD, FD_CLOEXEC) = 0fstat64(12, {st_mode=S_IFREG|0644, st_size=175, ...}) = 0mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb6fba000read(12, "# Do not remove the following li"..., 4096) = 175read(12, "", 4096) = 0close(12) = 0munmap(0xb6fba000, 4096) = 0open("/opt/oracle/product/9.2.0/lib/libnss_dns.so.2", O_RDONLY) = -1 ENOENT (No such file or directory)open("/lib/tls/libnss_dns.so.2", O_RDONLY) = -1 ENOENT (No such file or directory)open("/lib/i686/libnss_dns.so.2", O_RDONLY) = -1 ENOENT (No such file or directory)open("/lib/libnss_dns.so.2", O_RDONLY) = 12read(12, "/177ELF/1/1/1/0/0/0/0/0/0/0/0/0/3/0/3/0/1/0/0/0/240/16"..., 512) = 512fstat64(12, {st_mode=S_IFREG|0755, st_size=18632, ...}) = 0old_mmap(NULL, 17100, PROT_READ|PROT_EXEC, MAP_PRIVATE, 12, 0) = 0xb6fb6000old_mmap(0xb6fba000, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED, 12, 0x3000) = 0xb6fba000close(12) = 0open("/opt/oracle/product/9.2.0/lib/libresolv.so.2", O_RDONLY) = -1 ENOENT (No such file or directory)open("/lib/tls/libresolv.so.2", O_RDONLY) = -1 ENOENT (No such file or directory)open("/lib/i686/libresolv.so.2", O_RDONLY) = -1 ENOENT (No such file or directory)open("/lib/libresolv.so.2", O_RDONLY) = 12read(12, "/177ELF/1/1/1/0/0/0/0/0/0/0/0/0/3/0/3/0/1/0/0/0/320(/0"..., 512) = 512fstat64(12, {st_mode=S_IFREG|0755, st_size=76508, ...}) = 0old_mmap(NULL, 73604, PROT_READ|PROT_EXEC, MAP_PRIVATE, 12, 0) = 0xb6fa4000old_mmap(0xb6fb3000, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED, 12, 0xf000) = 0xb6fb3000old_mmap(0xb6fb4000, 8068, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0xb6fb4000close(12) = 0socket(PF_INET, SOCK_DGRAM, IPPROTO_IP) = 12connect(12, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("208.113.151.109")}, 28) = 0send(12, "/324#/1/0/0/1/0/0/0/0/0/0/3www/5anysql/3com/0/0/1/0/1", 31, 0) = 31gettimeofday({1161746697, 286025}, NULL) = 0poll([{fd=12, events=POLLIN, revents=POLLIN}], 1, 5000) = 1ioctl(12, FIONREAD, [74]) = 0recvfrom(12, "/324#/201/200/0/1/0/1/0/1/0/0/3www/5anysql/3com/0/0/1/0"..., 1024, 0, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("208.113.151.109")}, [16]) = 74close(12) = 0gettimeofday({1161746697, 290245}, NULL) = 0getrusage(RUSAGE_SELF, {ru_utime={0, 40000}, ru_stime={0, 20000}, ...}) = 0getrusage(RUSAGE_SELF, {ru_utime={0, 40000}, ru_stime={0, 20000}, ...}) = 0gettimeofday({1161746697, 291553}, NULL) = 0write(10, "/2/275/0/0/6/0/0/0/0/0/20/31/266/344/217/3700/320/341S"..., 701) = 701read(7, "/0/215/0/0/6/0/0/0/0/0/3^/t@/0/0/0/1/0/0/0/0/0/0/0/0/0"..., 2064) = 141gettimeofday({1161746697, 294898}, NULL) = 0getrusage(RUSAGE_SELF, {ru_utime={0, 40000}, ru_stime={0, 20000}, ...}) = 0getrusage(RUSAGE_SELF, {ru_utime={0, 40000}, ru_stime={0, 20000}, ...}) = 0gettimeofday({1161746697, 295496}, NULL) = 0getrusage(RUSAGE_SELF, {ru_utime={0, 40000}, ru_stime={0, 20000}, ...}) = 0gettimeofday({1161746697, 295847}, NULL) = 0gettimeofday({1161746697, 295981}, NULL) = 0lseek(9, 1024, SEEK_SET) = 1024read(9, "/30/0$/0007/0@/0J/0V/0`/0i/0t/0~/0/232/0/245/0/320/0/330"..., 512) = 512lseek(9, 47104, SEEK_SET) = 47104read(9, "/f/0^/5/0/0P/0x/5/0/0/214/0y/5/0/0/250/0z/5/0/0/313/0{"..., 512) = 512gettimeofday({1161746697, 297024}, NULL) = 0write(10, "/0/202/0/0/6/0/0/0/0/0/4/1/0/0/0/1/1/0/0/0{/5/0/0/0/0/1"..., 130) = 130read(7, Process 14701 detached

在此信息中,你可以发现Oracle顺序访问了下面的文件来完成地址定位:

open("/etc/resolv.conf", O_RDONLY) = 12open("/etc/host.conf", O_RDONLY) = 12open("/etc/hosts", O_RDONLY) = 12

首先,需要获取域名解析服务器,然后再根据host.conf文件确定解析顺序,因为缺省hosts文件是优先的,又继续读取/etc/hosts文件。

假如hosts文件存在解析关系,就会返回信息;假如不存在的话,则继续问询DNS服务器,获得解析地址,如果不能解析,则会出现错误:

SQL> select UTL_INADDR.get_host_address('www.a.com') from dual;select UTL_INADDR.get_host_address('www.a.com') from dual*ERROR at line 1:ORA-29257: host www.a.com unknownORA-06512: at "SYS.UTL_INADDR", line 35ORA-06512: at "SYS.UTL_INADDR", line 40ORA-06512: at line 1

综上所述,大家可以发现UTL_INADDR的数据获取已经不再依赖数据库信息,而SYS_CONTEXT的信息获取依然来自数据库的内部。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表