shellcode是一种小型内嵌代码,一般用于入侵,和修补漏洞的。
这种代码是非常简洁的而且跨平台性能高,因为后面博主要写关于shellcode的文章,所以今天这里给大家介绍如何使用c语言写一个能够实现汇编到机器码的转换程序
首先你需要一个汇编解释器:
这里博主已经上传到网盘:
链接:https://pan.baidu.com/s/1TuF1OeBJzAwcwVrlR4d4BQ
提取码: dptv
主要是对工具进行了封装,没有技术难点,所以这里就不做额外的说明了
// ConsoleApplication11.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>
#define ASSEM "mov ax,bx\r\nmov ax,bx\0"
int _tmain(int argc, _TCHAR* argv[])
{
//构造汇编代码
char _assem[1024] = {0};
strcat_s(_assem, "bits 32\n"); //汇编头,代表多少位的编译
//汇编体
strcat_s(_assem, "mov ax,bx\r\n"); //汇编代码,每行汇编需要一个换行
strcat_s(_assem, "mov ax,bx\0"); //末尾不需要换行
//将汇编代码写到本地
FILE* _fp = NULL;
fopen_s(&_fp,"_asm", "w");
fwrite(_assem, strlen(_assem), 1, _fp);
fclose(_fp);
//调用eax_asm汇编编译器编译
system("eax_asm _asm -o hex");
//编译完成后将编译好的二进制hex文件读入
fopen_s(&_fp, "hex", "rb+");
//获取文件大小
fseek(_fp, 0, SEEK_END);
int _size = ftell(_fp);
//移动回来
fseek(_fp, 0, SEEK_SET);
//读入时必须是存放到字节集里
unsigned char hex[256] = { 0 };
fread(hex, _size, 1, _fp);
fclose(_fp);
//打印
printf("汇编代码:\n%s\n\n", ASSEM);
printf("机器码:\n");
for (int i = 0; i < _size; ++i){
printf("%d ", hex[i]);
}
//机器码到汇编
printf("\n\n机器码转汇编:\n");
//调用ecx_asm汇编编译器编译
system("ecx_asm -b 32 hex");
getchar();
return 0;
}
实现效果:
其中 00000开头是代表文件内的偏移地址,6689d8是代表十六进制的汇编机器码