<转> xor eax, ebp” being used

I just tried compiling a couple of C++ snippets on VS2010 and analyzed the executables on IDA Pro. Something I noticed is that there most of them have something like the following at the start(shortly after a call to __security_check_cookie)

xor eax, ebp

and something like

xor ecx, ebp

at the bottom. Why does this happen? The compiler optimization was turned off.

 

 

answer:

These are buffer overrun protection methods, and have nothing to do with compiler optimisation. MSVC will (if you specify the /GS switch) push a security cookie onto the stack near the return address so that it can detect a common case of stack corruption.

Stack corruption can either be caused by bad code along the lines of:

char buff[5]; strcpy (buff,"Man, this string is waaay too long!!"); 

or by malicious users taking advantage of bad coding practices, like the use of scanf ("%s", myBuff) for user input. Carefully crafted attacks like that can suborn your program to do things you probably don't want it to.

By placing a cookie close to the return address, a large number of bugs (and attack vectors) can be prevented, simply due to the fact that the memory corruptions tend to be sequential in nature. In other words, if you've overwritten the return address, it's probably because you started writing on one side of the cookie and corrupted memory all the way up to the return address on the other side of the cookie (hence the cookie will be overwritten as well).

It doesn't catch all bugs since you may have some code like:

char buff[5]; buff[87]='x'; 

which could potentially corrupt the return address without touching the cookie. But it will catch all those malicious ones which rely on entering a longer string than expected, which corrupt up to the return address (including cookie).

The sequence you're probably seeing in the code is something like:

mov  eax, dword ptr ds:___sec_cookie   ; fixed value. xor  eax, ebp                          ; adjust based on base pointer. mov  [ebp+SOMETHING], eax              ; store adjusted value. 

which is customising the cookie, depending on the current base pointer.

This will change what is actually put on the stack at each stack level (and also depending on parameter count and sizes as well) and is probably an attempt to further secure the code from malicious intent, by ensuring a variable signature is written to the stack rather than a fixed value (otherwise the attacker could enter characters including a valid cookie).

And the sequence at the end will run something like this:

mov  ecx,[ebp+SOMETHING]              ; get the adjusted cookie. xor  ecx, ebp                          ; un-adjust it, since                                        ;   ((N xor X) xor X)== N. call @__sec_check_cookie               ; check the cookie. 

It's basically just the reverse process of that described above. The @__sec_check_cookie call will only return if ecx is set to the correct cookie value. Otherwise it will raise a fault, as confirmed here:

The __security_check_cookie() routine is straightforward: if the cookie was unchanged, it executes the RET instruction and ends the function call. If the cookie fails to match, the routine calls report_failure().

The report_failure() function then calls __security_error_handler(). Both functions are defined in the seccook.c file of the C run-time (CRT) source files.

CRT support is needed to make these security checks work. When a security check failure occurs, control of the program is passed to __security_error_handler(), which is summarized here:

void __cdecl __security_error_handler(int code,void*data) {     if(user_handler != NULL){       __try {         user_handler(code, data);       } __except (EXCEPTION_EXECUTE_HANDLER){}     }else{       //...prepare outmsg...        __crtMessageBoxA(           outmsg,           "Microsoft Visual C++ Runtime Library",           MB_OK|MB_ICONHAND|MB_SETFOREGROUND|MB_TASKMODAL);     }     _exit(3); } 

By default, an application that fails a security check displays a dialog that states "Buffer overrun detected!". When the dialog is dismissed, the application terminates.

时间: 2024-10-09 14:38:15

<转> xor eax, ebp” being used的相关文章

security cookie

在微软的c/c++ 编译器中,增加了对于栈溢出进行检测的参数 "/GS",在调试shellcode 的时候,发现vs2005 产生的code 和 vc6 产生的code 有些不同,才让我注意到这个问题.      写了这样的一个测试程序:      void foo(const char * datas)      {        char szbuf[32];         strcpy(szbuf, datas);      }      汇编的代码如下: 00411F70 

浅谈网络游戏《天龙X部》的文件加密格式

三月份时玩了某狐公司的网络游戏<天龙X部>,感觉还是蛮有意思的,遂研究了一下.这个游戏是利用开源游戏引擎OGRE进行开发的,看了一下目录里面的文件结构,主要的数据都放在Data目录下面.不过文件基本都是.AXP后缀的,每一个动辄几十兆,料想肯定是把游戏文件打包到一起并加密过的,GOOGLE未遂.开始用UE打开看了一下这个AXP文件,发现里面居然大部分都是明文的,开始以为只是把文件罗列在一起,不过仔细看了一下,发现每个文件都有一段间隔,前面还有一个数据头,而且文件与名字也无法对应.于是打开OD手

实例分析c++拷贝初始化与直接初始化的底层区别

阅读说明:对于前面代码看不懂的朋友,可以先跳到最后看总结,然后再回头看上文内容,或者会有豁然开朗的感觉. 开发运行环境:visual studio 2013 源代码 #include <iostream>  #include <cstring>  using namespace std;class ClassTest{public:    ClassTest()    {        c[0] = '\0';        cout << "ClassTes

汇编xor小妙用

用xor与同一数据进行两次运算后将得到运算前的值,如: eax=200 xor eax,100 xor eax,100 结果eax还是等于200

SYMANTEC防火墙内核溢出漏洞利用之安全返回法

安全|防火墙 作者:SoBeIt   来自:https://www.xfocus.net 这个漏洞发生在SYMDNS.SYS中,当处理DNS答复时,由于未检验总域名长度,导致可以输入一超长域名导致溢出,溢出发生在RING0.IRQL = 2(DISPATCH_LEVEL). 进程PID为0(idle进程)的环境下.     一个DNS报文格式如下:    "\xEB\x0B"    //报文ID,可以随意设置,但在这个漏洞里是别有用途的,后面会说到    "\x80\x00&

简明x86汇编语言教程(6)

4.0 利用子程序与中断 已经掌握了汇编语言?没错,你现在已经可以去破译别人代码中的秘密.然而,我们还有一件重要的东西没有提到,那就是自程序和中断.这两件东西是如此的重要,以至于你的程序几乎不可能离开它们. 4.1 子程序 在高级语言中我们经常要用到子程序.高级语言中,子程序是如此的神奇,我们能够定义和主程序,或其他子程序一样的变量名,而访问不同的变量,并且,还不和程序的其他部分相冲突. 然而遗憾的是,这种"优势"在汇编语言中是不存在的. 汇编语言并不注重如何减轻程序员的负担:相反,汇

初学Delphi嵌入汇编[2]

汇编语言不区分大小写. 关键字 用途 AH   AL   AND   AX   BH   BL   BP   BX   BYTE   CH   CL   CS   CX   DH   DI   DL   DS   DWORD   DX   EAX   EBP   EBX   ECX   EDI   EDX   EIP   ES   ESI   ESP   FS   GS   HIGH   LOW   MOD   NOT   OFFSET   OR   PTR   QWORD   SHL   SH

AT&amp;amp;T x86 asm语法

DJGPP 使用AT&T格式的汇编语法.和一般的intel格式的语法有点不同.主要不同点如下: AT&T 语法颠倒了源和目的操作数的位置, 目的操作数在源操作数之后.寄存器操作数要有个%的前缀, 立即数操作数要有个$符号的前缀. 存储器操作数的大小取决于操作码的最后一个字符. 它们是b (8-bit), w (16-bit), 和 l (32-bit). 这里有一些例子. 左边部分是intel指令格式,右边是at&t格式. movw %bx, %ax // mov ax, bx x

CIH V1.5版本病毒源码

; ****************************************************************************; * The Virus Program Information *; ****************************************************************************; * *; * Designer : CIH Source : TTIT of TATUNG in Taiwan *