1、前言
最近在看到同事写了一款封印病毒的程序,非常有意思!原理大致是将PE文件中的ASCII转换成HEX输出到文本中。这样做的目的是为了保存病毒样本的时候不会被杀毒软件查杀!然而却是delphi写的,特别想用C语言自己也实现一个,于是从google翻出了此代码。
2、编译后的结果
3、代码实现原理
为了兼容Win32 + *nix,需要定义头文件,在读取时把【\r\n】和【\n】区分。
// 20171105_bin2hex.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include
#include
//Win32 + *nix
#ifdef WIN32
#define CRLF "\r\n"
#else
#define CRLF "\n"
#endif
//number of bytes per line
#define LINELEN 16
void printFileAsHex(FILE* file);
char toVisibleCharacter(int byteVal);
void writeLine(char* lineBuffer, int buflen);
char upperToHex(int byteVal);
char lowerToHex(int byteVal);
char nibbleToHex(int nibble);
int main(int argc, const char* argv[])
{
FILE* file;
//if (argc != 2)
//{
// printf("Usage: phex ");
// printf(CRLF);
// return 1;
//}
// 读写文件
file = fopen("D:\\20171111-test.exe", "rb"); //打开文件
if (NULL == file)
{
printf("Cannot open %s", argv[1]);
printf(CRLF);
return 2;
}
printFileAsHex(file);
fclose(file);
system("pause");
return 0;
}
//主要实现函数
void printFileAsHex(FILE* file)
{
int count = 0;
char buffer[LINELEN]; //临时存放字符的数组
while (1)
{
int byteVal = fgetc(file); //指向的文件中读取一个字符,读取一个字节后,光标位置后移一个字节。
if (EOF == byteVal) //如果文件已经到尾部跳出循环
{
break;
}
buffer[count] = byteVal; //将文件字符存储到数组中
count++; //计算每个字符的数目
if (count >= LINELEN) //提前处理前16个字节
{
writeLine(buffer, LINELEN);
count = 0;
}
}
if (count > 0) //不满16字节的情况处理
{
writeLine(buffer, count);
}
}
char toVisibleCharacter(int byteVal)
{
if (byteVal >= 32 && byteVal <= 126) // 输出可打印字符
{
return (char)byteVal;
}
return '_'; //不可打印就输出_号
}
void writeLine(char* lineBuffer, int buflen)
{
int i;
char str[LINELEN];
// 读写文件
FILE* file = fopen("D:\\20171111-test.txt", "a+");
for (i = 0; i < buflen; i++)
{
char chu = upperToHex(lineBuffer[i]); //取高4位 -->处理成十进制的值
char chl = lowerToHex(lineBuffer[i]); //取低4位 -->处理成十进制的值
printf("%c%c ", chu, chl);
//自增加的-写文件到txt
sprintf(str, "%c%c", chu, chl);
fseek(file, 0, SEEK_END);
fwrite(str, strlen(str), 1, file);
}
fclose(file);
if (buflen < LINELEN)
{
for (i = LINELEN - buflen; i > 0; i--)
{
printf(" ");
}
}
printf("\t");
char str1[LINELEN];
for (i = 0; i < buflen; i++)
{
char ch = toVisibleCharacter(lineBuffer[i]);
printf("%c", ch);
}
printf(CRLF);
}
char upperToHex(int byteVal)
{
int i = (byteVal & 0xF0) >> 4; //0F二进制为00001111,低四位保留,然后右移4位,也就是只取出高4位的意思
return nibbleToHex(i);
}
char lowerToHex(int byteVal)
{
int i = (byteVal & 0x0F); //保留低4位00001111,这次没有右移,所以保留低4位
return nibbleToHex(i);
}
char nibbleToHex(int nibble)
{
const int ascii_zero = 48;
const int ascii_a = 65;
if ((nibble >= 0) && (nibble <= 9))
{
return (char)(nibble + ascii_zero); // 十六进制转换成十进制 返回数字,本来是int型,强转成char了
}
if ((nibble >= 10) && (nibble <= 15))
{
return (char)(nibble - 10 + ascii_a); // 十六进制转换成十进制的字母,本来是int型,强转成char了
}
return '?'; //其余情况返回'?'
}
4、参考
C++ 读取PE文件并十六进制打印输出
C: hex dump application
把十进制数(long型)分别以二进制和十六进制形式输出,不能使用printf系列。
编程实现:把十进制数(long型)分别以二进制和十六进制形式输出,不能使用printf系列. 实现了unsigned long型的转换. // 十进制转换为二进制,十进制数的每1bit转换为二进制的1 ...
C语言 printf格式化输出,参数详解
有关输出对齐 int main(int argc, char* argv[]){ char insertTime[20] = {"1234567890"}; double in ...
c语言 printf格式化输出
#include #include #include using namespace std; int ...
【C语言】- 数据输出-printf( )和putchar( )
格式化输出函数printf( ) printf( )功能: 向系统指定输出设备按指定的格式输入任意个任意类型的数据,并返回实际输出的字符数.若出错,将返回负数. printf( )使用形式: prin ...
C语言中以字符串形式输出枚举变量
C语言中以字符串形式输出枚举变量 摘自:https://blog.csdn.net/haifeilang/article/details/41079255 2014年11月13日 15:17:20 h ...
xxd - 以十六进制形式表示
总览 (SYNOPSIS) xxd -h[elp] xxd [options] [infile [outfile]] xxd -r[evert] [options] [infile [outfile] ...
C语言之一天一个小程序
程序示例: #include #include int main() { printf("Hello,world!\n&qu ...
[Windows] [VS] [C] [取得指针所指内存的十六进制形式字符串]
接口定义如下: #include // 取得指针所指内存的十六进制形式字符串,size指定字节长度 #define Mem_toString(address, si ...
统计一个16位二进制数中1的个数,并将结果以十六进制形式显示在屏幕上,用COM格式实现。
问题 统计一个16位二进制数中1的个数,并将结果以十六进制形式显示在屏幕上,用COM格式实现. 代码 code segment assume cs:code org 100h main proc ne ...
随机推荐
谈谈软件项目的dependency
说到软件项目的依赖管理,可以从三个方面来考虑: 一.由build system控制的dependency 现在的build system,都支持一定程度上的dependency management, ...
转(Delphi 新窑洞):使用delphi 开发多层应用(十七)使用RTC web 服务器返回JSON
RTC作为delphi 的最专业的web 应用服务器,如果客户端要使用JSON 的话,那么使用RTC 应该也是一种 非常好的选择.下面我们做一个使用RTC web 服务器返回数据库JSON 的例子. ...
HDU 4704
http://acm.hdu.edu.cn/showproblem.php?pid=4704 求(2^n)%mod的方法 #include #include < ...
jsp 有哪些内置对象?作用分别是什么? 分别有什么方 法?
JSP共有以下9种基本内置组件 request对象 客户端请求,此请求包含来自GET/POST的请求参数,通过它才能了解到客户的需求,然后做出相应. response对象 ...
tkinter第一章1
tk1 ------------------------------------------------------------------------------------------ impor ...
kafka集群broker频繁挂掉问题解决方案
现象:kafka集群频繁挂掉 排查日志:查看日志文件[kafkaServer.out],发现错误日志:ERROR Shutdown broker because all log dirs in /tm ...
ubuntu14.04安装 Apache2 并配置https
一.安装 Apache2 sudo apt-get update sudo apt-get install apache2 安装完apache2,默认根目录在/var/www/html 下,点击其下的 ...
ProgrammingError: You must not use 8-bit bytestrings...
问题出现: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit byte ...
Hide-Music-Player 一个完整的音乐播放器《IT蓝豹》
Hide-Music-Player 一个完整的音乐播放器 Hide-Music-Player 一个完整的音乐播放器,本例子主要包括几个点 (1)摇一摇进入播放器 (2)下拉展开新视图(扫描音乐) (3 ...
UCOS2系统内核讲述(二)_初始化调用函数
Ⅰ.写在前面 学习本文之前可以参看我前面的文章: UCOS2_STM32F1移植详细过程(汇总文章) UCOS2系统内核讲述(一)_总体描述 还是按照上一篇文章的思维(从外到内),本文(结合源代码)进 ...