找回密码
 加入我们

QQ登录

只需一步,快速开始

搜索
查看: 4445|回复: 3

Shellcode In X64-3 Test Your Shellcode

[复制链接]

21

主题

110

回帖

4

精华

铂金会员

积分
7522
发表于 2012-8-31 20:12:42 | 显示全部楼层 |阅读模式
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 (21.95 KB, 下载次数: 2969)

857

主题

2632

回帖

2

精华

管理员

此生无悔入华夏,  长居日耳曼尼亚。  

积分
36130
发表于 2012-9-5 21:06:27 | 显示全部楼层
好帖。。。怎么之前没看到呢。。。加精!!!

7

主题

414

回帖

1

精华

铂金会员

积分
2173
发表于 2013-4-11 17:56:13 | 显示全部楼层
好東西
头像被屏蔽

4

主题

101

回帖

0

精华

初来乍到

积分
19658
发表于 2014-1-15 10:30:05 | 显示全部楼层
好东西
您需要登录后才可以回帖 登录 | 加入我们

本版积分规则

快速回复 返回顶部 返回列表