使用alpha3生成alphanumeric shellcode

0x00 背景

之前打杭电新生赛hgame的时候碰到一个题目, 题目会检测用户输入的shellcode, 限制shellcoode只能是大写字母和数字, 经社团大佬提醒得知对付这种问题用一个专门的工具: alpha3. 这篇文章就以这题为例来记录一下alpha3的使用方法.

0x00 程序分析

用ida打开二进制文件再反汇编得到main函数的伪代码如下


int __cdecl main()

{

char buf; // [esp+Fh] [ebp-19h]

int i; // [esp+10h] [ebp-18h]

unsigned int sc; // [esp+14h] [ebp-14h]

ssize_t v4; // [esp+18h] [ebp-10h]

unsigned int canary; // [esp+1Ch] [ebp-Ch]

canary = __readgsdword(0x14u);

setvbuf(stdout, 0, 2, 0);

sc = (unsigned int)malloc(0x1000u);

puts("========== ez shellcode ver2 ==========");

printf("> ");

for ( i = 0; i <= 4095; ++i )

{

v4 = read(0, &buf, 1u);

if ( v4 == -1 )

exit(0);

if ( (buf > 90 || buf <= 64) && (buf <= 47 || buf > 57) )

break;

*(_BYTE *)(sc + i) = buf;

}

if ( mprotect((void *)(sc & 0xFFFFF000), 0x1000u, 7) == -1 )

{

puts("error ,tell admin");

}

else

{

puts("exec shellcode...");

((void (*)(void))sc)();

}

return 0;

}

程序很简单, 只要找到仅有大写字母数字组成的shellcode(也叫做 alphanumeric shellcode)组成就可以成功pwn. 我们使用alpha3将普通的shellcode转化成alphanumeric shellcode, 不过alpha3 实在难用, 而且网上教程非常少, 这也是我写这个文章的主要原因.

破解过程

我们首先在github上面搜索 alpha3 找到代码的仓库, 然后下载到本地再build之后就可以使用了,过程十分麻烦...... 这儿就直接提供build之后的给大家下载(密码mmdj). 然后我们先找一个普通的可以getshell的shellcode, 然后我们需要将对应的机器码写入到一个文件中(例如sc.bin), 然后我们在cmd中cd到apha3的文件夹中执行alpha3来得到alphanumeric shellcode, 在之前我们先执行python ./ALPHA3.py看一下帮助:


[Usage]

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

[Encoder setting]

architecture Which processor architecture to target (x86,

x64).

character encoding Which character encoding to use (ascii, cp437,

latin-1, utf-16).

casing Which character casing to use (uppercase,

mixedcase, lowercase).

base address How to determine the base address in the decoder

code (each encoder has its own set of valid

values).

[I/O Setting]

--input="file" Path to a file that contains the shellcode to be

encoded (Optional, default is to read input from

stdin).

--output="file" Path to a file that will receive the encoded

shellcode (Optional, default is to write output

to stdout).

[Flags]

--verbose Display verbose information while executing. Use

this flag twice to output progress during

encoding.

--help Display this message and quit.

--test Run all available tests for all encoders.

(Useful while developing/testing new encoders).

--int3 Trigger a breakpoint before executing the result

of a test. (Use in combination with --test).

[Notes]

You can provide encoder settings in combination with the --help and --test

switches to filter which encoders you get help information for and which

get tested, respectively.

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]


我们这题是32位的, 所以architecture是X86; 因为main函数中是按字节检测的, 所以character encoding 选择 ascii; 而且题目中要求的是大写字母, 所以casing 自然就是upper. 但是最后的base address 是什么呢? 这个alpha会利用shellcode基址来重定位shellcode,相当于在shellcode运行过程中重新组装shellcode. 而查看ida中返回编的代码可知调用shellcode的汇编指令是call eax 所以base 就是EAX 在结合我们之前得到的普通shellcode就可以用python ./PYTHON.py x86 ascii uppercase eax --input="sc.bin" > out.bin就可以在out.bin中得到一个 alphanumeric shellcode, 然后再用pwntools输入这个alphanumeric shellcode 即可成功getshell !

总结

打这次hgame才知道pwn原来有这么多骚操作, 真的是太有意思了. 还是要多多学习呀.

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,997评论 6 502
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,603评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,359评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,309评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,346评论 6 390
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,258评论 1 300
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,122评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,970评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,403评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,596评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,769评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,464评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,075评论 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,705评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,848评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,831评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,678评论 2 354

推荐阅读更多精彩内容