首页
社区
课程
招聘
[原创] Shellcode In X64-3 Test Your Shellcode
2012-8-31 16:33 6861

[原创] Shellcode In X64-3 Test Your Shellcode

2012-8-31 16:33
6861
1   在32位下测试shellcode
大体的,在32位下我们测试shellcode的程序可以是这样。
// shellcodetest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "windows.h"

typedef void (WINAPI *FUN)(void);

char shellcode[]="\x90\x90\x90\x90\x68\x6A\x0A\x38\x1E\x68\x63\x89\xD1\x4F\x68\x32\x74\x91\x0C\x8B\xF4"
    "\x8D\x7E\xF4\x33\xDB\xB7\x04\x2B\xE3\x66\xBB\x33\x32\x53\x68\x75\x73\x65\x72\x54\x33"
    "\xD2\x64\x8B\x5A\x30\x8B\x4B\x0C\x8B\x49\x1C\x8B\x09\x8B\x69\x08\xAD\x3D\x6A\x0A\x38"
    "\x1E\x75\x05\x95\xFF\x57\xF8\x95\x60\x8B\x45\x3C\x8B\x4C\x05\x78\x03\xCD\x8B\x59\x20"
    "\x03\xDD\x33\xFF\x47\x8B\x34\xBB\x03\xF5\x99\x0F\xBE\x06\x3A\xC4\x74\x08\xC1\xCA\x07"
    "\x03\xD0\x46\xEB\xF1\x3B\x54\x24\x1C\x75\xE4\x8B\x59\x24\x03\xDD\x66\x8B\x3C\x7B\x8B"
    "\x59\x1C\x03\xDD\x03\x2C\xBB\x95\x5F\xAB\x57\x61\x3D\x6A\x0A\x38\x1E\x75\xA9\x33\xDB"
    "\x53\x68\x65\x61\x73\x74\x68\x73\x68\x69\x6E\x8B\xC4\x53\x50\x50\x53\xFF\x57\xFC\x53"
    "\xFF\x57\xF8";

int main(int argc, char* argv[])
{
        FUN myfun=NULL;
        myfun=(FUN)&shellcode;
        myfun();
        return 0;
}

用VC6编译之后,就会生成一个可以运行于32位xp下的shellcode。测试工程在shellcodetestShinest.7z中。我们看一下在X64下面怎样来测试我们的shellcode。

                         2  test shellcode in fasm
format PE64 GUI

   include 'win64a.inc'
entry start

section  '.text' code readable executable

start:
call shellcode

shellcode: file '3.bin'

            section '.import' import data readable writeable
     library kernel32, 'kernel32.dll',\
         user32, 'user32.dll'
     include 'api\kernel32.inc'
     include 'api\user32.inc'

        这是一个在fasm中测试shellcode的程序,你可以看到,只用到很短的几句话就可以测试我们的shellcode,关键在于一个file伪指令的应用,相信你学了第二节的fasm的一些基础知识之后,这个代码不难读懂。代码打包在testinfasm中。
                          3 test your shellcode using vc
   下面的这段代码,是我在看国外的一些x64 shellcode的时候见过的,相信很多人还是希望能够在VS当中测试我们的shellcode,我们可以看下下面这些代码。
  // Runbin.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <io.h>
#include <stdlib.h>
#include <malloc.h>
#include <fcntl.h>
#include <intrin.h>

typedef void (*FUNCPTR)();
int main(int argc, char **argv)
{
        //hello.exe Shiqi Yu  argc 参数数量在这是3 argv[0]是"hello.exe",argv[1]是"Shiqi",argv[2]是"Yu"。

        FUNCPTR func;
        void *buf;
        int fd, len;
        int debug;
        char *filename;
        DWORD oldProtect;

        if (argc == 3 && strlen(argv[1]) == 2 && strncmp(argv[1], "-d", 2) == 0) {
                debug = 1;
                filename = argv[2];
        } else if (argc == 2) {
                debug = 0;
                filename = argv[1];
        } else {
                fprintf(stderr, "usage: runbin [-d] <filename>\n");
                fprintf(stderr, "  -d    insert debugger breakpoint\n");
                return 1;
        }

        fd = _open(filename, _O_RDONLY | _O_BINARY);

        if (-1 == fd) {
                perror("Error opening file");
                return 1;
        }

        len = _filelength(fd);

        if (-1 == len) {
                perror("Error getting file size");
                return 1;
        }

        buf = malloc(len);

        if (NULL == buf) {
                perror("Error allocating memory");
                return 1;
        }

        if (0 == VirtualProtect(buf, len, PAGE_EXECUTE_READWRITE, &oldProtect)) {
                fprintf(stderr, "Error setting memory executable: error code %d\n", GetLastError());
                return 1;
        }        

        if (len != _read(fd, buf, len)) {
                perror("error reading from file");
                return 1;
        }

        func = (FUNCPTR)buf;

        if (debug) {
                __debugbreak();
        }

        func();

        return 0;
}

其中与xp下不同的就这一句
VirtualProtect(buf, len, PAGE_EXECUTE_READWRITE, &oldProtect)
你可能会奇怪我们的fasm为什么没有用到这个函数来修改页面的属性,看看我们定义好的section属性
section  '.text' code readable executable
我们编译出来runbin之后,直接可以在命令行runbin xxx.bin就可以了。
工程我打包在testinvs2008.7z中。

shellcode-3附件.7z

[培训]二进制漏洞攻防(第3期);满10人开班;模糊测试与工具使用二次开发;网络协议漏洞挖掘;Linux内核漏洞挖掘与利用;AOSP漏洞挖掘与利用;代码审计。

上传的附件:
收藏
点赞3
打赏
分享
最新回复 (10)
雪    币: 589
活跃值: (119)
能力值: ( LV11,RANK:190 )
在线值:
发帖
回帖
粉丝
promsied 4 2012-9-9 20:29
2
0
消灭0恢复
雪    币: 540
活跃值: (264)
能力值: ( LV12,RANK:210 )
在线值:
发帖
回帖
粉丝
KiDebug 4 2012-9-9 22:30
3
0
好麻烦。。。vs里面直接能编译64位的asm,直接F5就能调。
雪    币: 673
活跃值: (253)
能力值: ( LV15,RANK:360 )
在线值:
发帖
回帖
粉丝
KMSRussian 8 2012-9-10 00:54
4
0
VS里面 要怎么设置才能编译64位的ASM呢 求楼上指点
雪    币: 589
活跃值: (119)
能力值: ( LV11,RANK:190 )
在线值:
发帖
回帖
粉丝
promsied 4 2012-9-10 02:13
5
0
http://hi.baidu.com/ithurricane/item/99b3741d4d393b7b7a5f253e
做一个记录而已。。。

x64里由于无法内联汇编语句,所以必须专门写asm文件来编译汇编语言,

先总结一下R0里面如何编译:

1. 下载MASM64编译器(Tesla.Angela修改版),下载地址:http://good.gd/1399004.htm

然后写一个编译的bat文件(假设装在C盘下面,项目在D:\Devlopment\test,asm文件叫x64.asm)

@Set ProgName=64bit

@Color 0E

@Title %ProgName%

@call C:\Masm64\Env.Cmd

@set Directory="D:\Devlopment\test"

@Set SrcName=x64

@cd %Directory%

@del "%Directory%\%SrcName%.obj"

@C:\Masm64\BIN\x64\ml64 /c "%Directory%\%SrcName%.asm"

@pause


2.修改sources文件

添加 AMD64_SOURCES=x64.asm和TARGETLIBS = x64.obj这两行


3.最后修改一下mybuild.bat文件

ddkbuild.cmd -WLHX64 chk . -cZ -WDF,用64位来编译


下面是R3的工程编译ASM文件,我本来想在设定里面搞定的,无奈一直无法成功,只好直接修改工程文件

1. VS2008的工程文件:

修改**.PowerTool64.vcproj

添加一个Filter节点,内容如下


<Filter Name="ASM" >

<FileRelativePath=".\ASM\x64.asm">

<FileConfiguration Name="Release|x64">

<Tool Name="VCCustomBuildTool"

CommandLine="cd "$(IntDir)" ml64.exe /D_WIN32 /c /Cx "$(InputPath)""

Outputs="$(IntDir)\$(InputName).obj"

/>

</FileConfiguration>

</File>

</Filter>



2. VS2010的工程,需要修改两个文件,首先是***.vcxproj

添加一个ItemGroup


<ItemGroup>

<CustomBuild Include=".\ASM\x64.asm">

<Command Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">cd "$(IntDir)"

ml64.exe /D_WIN32 /c /Cx "%(FullPath)"

</Command>

<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(IntDir)%(Filename).obj;%(Outputs)</Outputs>

<Command Condition="'$(Configuration)|$(Platform)'=='Release|x64'">cd "$(IntDir)"

ml64.exe /D_WIN32 /c /Cx "%(FullPath)"

</Command>

<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(IntDir)%(Filename).obj;%(Outputs)</Outputs>

</CustomBuild>

</ItemGroup>

然后修改***.vcxproj.filters,添加如下内容:


<ItemGroup>

<CustomBuild Include=".\ASM\x64.asm">

<Filter>Source Files\ASM</Filter>

</CustomBuild>

</ItemGroup>





这样,就可以在x64里面使用汇编编程了~~~
雪    币: 673
活跃值: (253)
能力值: ( LV15,RANK:360 )
在线值:
发帖
回帖
粉丝
KMSRussian 8 2012-9-10 08:39
6
0
恩 这个方法看过介绍 但没用过 还是感觉有点麻烦的

写shellcode打算换Delphi XE2 原生态支持X64内联汇编
雪    币: 468
活跃值: (52)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
aait 2012-9-10 20:37
7
0
5楼说的应该是用vs ide编写asm程序吧。不是在vc64的源代码中间嵌入64位内联汇编吧?
不能嵌入汇编,有什么用,纯粹写64位asm用什么不能写,非得vs ide来写纯64位asm?
要的是嵌入汇编,5楼的方法无用。
雪    币: 468
活跃值: (52)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
aait 2012-9-10 20:41
8
0
VS里面直接写_asm{
                            xor eax,eax//举个例子
                            xxx
                            }

用intel c++编译没一点问题。intel c++就是嵌入到vs2010里面直接调用的。和使用ms c++编译器操作上没两样,ms c++编译器64位是通不过的,这个用intel c++64位可以通过的。代码质量可能还要更高。
雪    币: 468
活跃值: (52)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
aait 2012-9-10 20:55
9
0
Delphi XE2支持X64内联汇编确实不错。而且界面编程VCL也很不错,个人感觉比MFC好多了。
可惜shellcode无界面。
雪    币: 156
活跃值: (190)
能力值: ( LV7,RANK:110 )
在线值:
发帖
回帖
粉丝
Gall 2012-12-19 20:11
10
0
请问一下,测试过了吗?
本人VC编译MSF生成的x64 shellcode,有问题.
所以建议大家也测试一下.

至于ASM的方法,大家也可以验证,应该可行.
雪    币: 346
活跃值: (25)
能力值: ( LV3,RANK:30 )
在线值:
发帖
回帖
粉丝
OnlyForU 2014-12-28 15:41
11
0
好贴!  收藏!!!
游客
登录 | 注册 方可回帖
返回