惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
大猫的无限游戏
大猫的无限游戏
B
Blog
阮一峰的网络日志
阮一峰的网络日志
IT之家
IT之家
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
Jina AI
Jina AI
博客园 - 聂微东
T
The Blog of Author Tim Ferriss
宝玉的分享
宝玉的分享
L
LangChain Blog
M
MIT News - Artificial intelligence
Blog — PlanetScale
Blog — PlanetScale
腾讯CDC
酷 壳 – CoolShell
酷 壳 – CoolShell
Y
Y Combinator Blog
F
Fortinet All Blogs
H
Help Net Security
B
Blog RSS Feed
J
Java Code Geeks
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Apple Machine Learning Research
Apple Machine Learning Research
S
SegmentFault 最新的问题

博客园 - Ady Lee

【C语言】二维数组在内存中的存储方式 C++:继承访问属性(public/protected/private) Makefile文件基本格式 异步IRP的教训(已附DUMP) IRQL 文件过滤驱动中的重入处理 FastIO 用poolmon来查找内存泄露 缓存管理器 浅议Windows 2000/XP Pagefile组织管理 FCB CCB FileObject WDM驱动改可手动加卸载的NT驱动 Windows 文件过滤驱动经验总结 PKCS #1 RSA Encryption Version 1.5 解决STM32 I2C接口死锁在BUSY状态的方法讨论 亲测实验,stm32待机模式和停机模式唤醒程序的区别,以及唤醒后程序入口 python中使用 C 类型的数组以及ctypes 的用法 python ctypes库3_如何传递并返回一个数组 Windbg 内核态调试用户态程序然后下断点正确触发方法(亲自实现发现有效) ___security_cookie机制,防止栈溢出
security cookie 机制(2)--- 初始化___security_cookie
Ady Lee · 2018-11-09 · via 博客园 - Ady Lee

在 cookie 检查中,必定先要取出初始的 cookie 值:

0011392E A1 14 70 11 00       mov         eax,dword ptr [___security_cookie (117014h)]  
00113933 33 C5                xor         eax,ebp  
00113935 89 45 FC             mov         dword ptr [ebp-4],eax 

这个 cookie 值是属于用户进程的,在这里是 helloworld.exe 映像

现在我们来看看这个 cookie 值被初始化什么,在哪里初始化?

0:000:x86> uf 0xb21221
helleworld!wWinMainCRTStartup [f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c @ 361]:
  361 00b22090 8bff            mov     edi,edi
  361 00b22092 55              push    ebp
  361 00b22093 8bec            mov     ebp,esp
  368 00b22095 e8c5efffff     call    helleworld!ILT+90(___security_init_cookie) (00b2105f)
  370 00b2209a e811000000      call    helleworld!__tmainCRTStartup (00b220b0)
  371 00b2209f 5d              pop     ebp
  371 00b220a0 c3              ret

helleworld!ILT+540(_wWinMainCRTStartup):
00b21221 e96a0e0000     jmp     helleworld!wWinMainCRTStartup (00b22090)

在 helloworld 的 wWinMainCRTStrartup() 启动例程里,首先调用 __security_init_cookie()例程进行 cookie 初始化,然后跳转到 __tmainCRTStartup() 最后流转到用户 wWinMain(),这些例程都是在用户空间,属于用户进程的。它们是 visual c++ 编译器为用户程序安插的启动例程,并且它们都是有源码提供,在 VC 的运行时库例程里可以找到源码。

下面我们来看看__security_init_cookie()例程是怎样运作的,在我目录是:C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\crt\src 下的文件 gs_support.c

#ifdef _WIN64
#define DEFAULT_SECURITY_COOKIE 0x00002B992DDFA232
#else 
#define DEFAULT_SECURITY_COOKIE 0xBB40E64E
#endif 


extern UINT_PTR __security_cookie;
extern UINT_PTR __security_cookie_complement;


typedef union {
    unsigned __int64 ft_scalar;
    FILETIME ft_struct;
} FT;

void __cdecl __security_init_cookie(void)
{
    UINT_PTR cookie;
    FT systime={0};
    LARGE_INTEGER perfctr;

    if (__security_cookie != DEFAULT_SECURITY_COOKIE
#if defined (_X86_)
        && (__security_cookie & 0xFFFF0000) != 0
#endif 
       )
    {
        __security_cookie_complement = ~__security_cookie;
        return;
    }

    GetSystemTimeAsFileTime(&systime.ft_struct);
#if defined (_WIN64)
    cookie = systime.ft_scalar;
#else 
    cookie = systime.ft_struct.dwLowDateTime;
    cookie ^= systime.ft_struct.dwHighDateTime;
#endif 

    cookie ^= GetCurrentProcessId();
    cookie ^= GetCurrentThreadId();
    cookie ^= GetTickCount();

    QueryPerformanceCounter(&perfctr);
#if defined (_WIN64)
    cookie ^= perfctr.QuadPart;
#else 
    cookie ^= perfctr.LowPart;
    cookie ^= perfctr.HighPart;
#endif 

#if defined (_WIN64)
   
    cookie &= 0x0000FFFFffffFFFFi64;
#endif 

    if (cookie == DEFAULT_SECURITY_COOKIE)
    {
        cookie = DEFAULT_SECURITY_COOKIE + 1;
    }
#if defined (_X86_)
    else if ((cookie & 0xFFFF0000) == 0)
    {
        cookie |= ( (cookie|0x4711) << 16);
    }
#endif 

    __security_cookie = cookie;
    __security_cookie_complement = ~cookie;

}

__security_cookie 在 CRT 库里是全局变量,见于另一个文件 gs_cookie.c

#ifdef _WIN64
#define DEFAULT_SECURITY_COOKIE 0x00002B992DDFA232
#else 
#define DEFAULT_SECURITY_COOKIE 0xBB40E64E
#endif 

DECLSPEC_SELECTANY UINT_PTR __security_cookie = DEFAULT_SECURITY_COOKIE;

DECLSPEC_SELECTANY UINT_PTR __security_cookie_complement = ~(DEFAULT_SECURITY_COOKIE);

CRT 库的 __security_cookie 的值就是 DEFAULT_SECURITY_COOKIE,在 Win32 下被定义为 0xBB40E64E,在 Win64 下被定义为 0x00002B992DDFA232

但是用户进程的 __security_cookie 值,需进行下面的设置:

  1. 获得 system time
  2. 与 GetCurrentProcessId() 异或
  3. 与 GetCurrentThreadId() 异或
  4. 与 GetTickCount() 异或
  5. 与 QueryPerformanceCounter()异或

经过一系列异或用户的 __security_cookie 值就出来,我们大概可以猜到 DEFAULT_SECURITY_COOKIE 这个值就是根据这样算出来的,因此如果算出来结果还是等于 DEFAULT_SECURITY_COOKIE 那么就需要加上1


版权 mik 所有,转载请注明出处