Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pe2bin
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
问题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
银宸时代
Linux Lab Group
项目模板
pe2bin
提交
ee71732b
提交
ee71732b
11月 14, 2018
创建
作者:
赵鹏翀
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Initial commit
上级
显示空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
159 行增加
和
0 行删除
+159
-0
.gitignore
.gitignore
+3
-0
Readme.md
Readme.md
+2
-0
main.c
main.c
+123
-0
pe2bin.luxproj
pe2bin.luxproj
+31
-0
pe2bin.puo
pe2bin.puo
+0
-0
没有找到文件。
.gitignore
0 → 100644
浏览文件 @
ee71732b
/Debug
/Release
\ No newline at end of file
Readme.md
0 → 100644
浏览文件 @
ee71732b
用于创建pe2bin项目
\ No newline at end of file
main.c
0 → 100644
浏览文件 @
ee71732b
#include <windows.h>
#include <stdio.h>
#define BUFFERSIZE 4096
unsigned
char
DataBuffer
[
BUFFERSIZE
];
// 读各个节的缓冲区
int
main
(
int
argc
,
char
*
argv
[])
{
if
(
3
!=
argc
)
{
printf
(
"usage: pe2bin.exe exeFilePath binFilePath
\n
"
);
return
1
;
}
//
// 打开 PE 文件
//
FILE
*
pExeFile
=
fopen
(
argv
[
1
],
"rb"
);
// 此处必须以二进制的方式打开,否则出错。
if
(
NULL
==
pExeFile
)
{
printf
(
"Open exe file
\"
%s
\"
failed.
\n
"
,
argv
[
1
]);
return
1
;
}
//
// 读取 DOS_HEADER
//
IMAGE_DOS_HEADER
dosHeader
;
fread
(
&
dosHeader
,
sizeof
(
dosHeader
),
1
,
pExeFile
);
//
// 读取 NT_HEADER
//
IMAGE_NT_HEADERS
ntHeader
;
fseek
(
pExeFile
,
dosHeader
.
e_lfanew
,
SEEK_SET
);
fread
(
&
ntHeader
,
sizeof
(
ntHeader
),
1
,
pExeFile
);
if
(
IMAGE_NT_SIGNATURE
!=
ntHeader
.
Signature
)
{
printf
(
"File
\"
%s
\"
is not a PE file.
\n
"
,
argv
[
1
]);
fclose
(
pExeFile
);
return
1
;
}
unsigned
short
numSections
=
ntHeader
.
FileHeader
.
NumberOfSections
;
if
(
0
!=
ntHeader
.
FileHeader
.
NumberOfSymbols
&&
0
!=
ntHeader
.
FileHeader
.
PointerToSymbolTable
)
{
//
// 运行到这里表明没有运行 strip 命令
//
printf
(
"Exe file
\"
%s
\"
still has debug section.
\n
"
,
argv
[
1
]);
fclose
(
pExeFile
);
return
1
;
}
//
// 读取 SECTION_TABLE
//
PIMAGE_SECTION_HEADER
pSectionTable
=
(
PIMAGE_SECTION_HEADER
)
malloc
(
sizeof
(
IMAGE_SECTION_HEADER
)
*
numSections
);
fseek
(
pExeFile
,
sizeof
(
ntHeader
)
+
dosHeader
.
e_lfanew
,
SEEK_SET
);
fread
(
pSectionTable
,
sizeof
(
IMAGE_SECTION_HEADER
),
numSections
,
pExeFile
);
// 求出虚拟地址的总大小(也就是所有节的总大小)
int
virSize
=
0
;
for
(
int
i
=
0
;
i
<
numSections
;
i
++
)
virSize
+=
pSectionTable
[
i
].
Misc
.
VirtualSize
;
//
// 创建 bin 文件
//
FILE
*
pBinFile
=
fopen
(
argv
[
2
],
"wb"
);
if
(
NULL
==
pBinFile
)
{
printf
(
"Creating bin file
\"
%s
\"
failed.
\n
"
,
argv
[
2
]);
free
(
pSectionTable
);
fclose
(
pExeFile
);
return
1
;
}
// 将 bin 文件的大小设置为 virSize,同时用 0 填充
unsigned
char
c
=
0
;
for
(
int
i
=
0
;
i
<
virSize
;
i
++
)
fwrite
(
&
c
,
sizeof
(
unsigned
char
),
1
,
pBinFile
);
for
(
int
i
=
0
;
i
<
numSections
;
i
++
)
{
int
RawOffset
=
pSectionTable
[
i
].
PointerToRawData
;
// 节在 PE 文件中的偏移值
int
virAddress
=
pSectionTable
[
i
].
VirtualAddress
;
// 节映射到内存中的虚拟地址
int
sizeOfSection
=
pSectionTable
[
i
].
SizeOfRawData
;
// 对齐后节的大小
if
(
0
==
sizeOfSection
||
0
==
strcmp
((
char
*
)
pSectionTable
[
i
].
Name
,
".idata"
))
continue
;
printf
(
" Section Name: %s
\n
"
,
pSectionTable
[
i
].
Name
);
printf
(
" VirtualAddress: %08x
\n
"
,
pSectionTable
[
i
].
VirtualAddress
);
printf
(
" SizeOfRawData: %08x
\n
"
,
pSectionTable
[
i
].
SizeOfRawData
);
printf
(
"PointerToRawData: %08x
\n
"
,
pSectionTable
[
i
].
PointerToRawData
);
printf
(
"*********************************************************
\n
"
);
fseek
(
pExeFile
,
RawOffset
,
SEEK_SET
);
fseek
(
pBinFile
,
virAddress
,
SEEK_SET
);
// 从 argv[1] 读数据,同时写入 argv[2]
for
(;
sizeOfSection
>
0
;
)
{
int
nSize
=
sizeOfSection
>=
BUFFERSIZE
?
BUFFERSIZE
:
sizeOfSection
;
fread
(
DataBuffer
,
sizeof
(
unsigned
char
),
nSize
,
pExeFile
);
fwrite
(
DataBuffer
,
sizeof
(
unsigned
char
),
nSize
,
pBinFile
);
sizeOfSection
-=
BUFFERSIZE
;
}
}
free
(
pSectionTable
);
fclose
(
pExeFile
);
fclose
(
pBinFile
);
return
0
;
}
pe2bin.luxproj
0 → 100644
浏览文件 @
ee71732b
<?xml version="1.0" encoding="gb2312"?>
<OSLProject
Version=
"1.00"
Name=
"pe2bin"
SubjectID=
"1580a23c-aa04-4cf3-8cca-488dc577df8d"
IncrementalUpload=
"0"
>
<Configurations>
<Configuration
Name=
"Debug"
PreDebugCommand=
""
>
<Tool
Name=
"PreBuildEventTool"
/>
<Tool
Name=
"CustomBuildTool"
/>
<Tool
Name=
"GCCCompilerTool"
PreprocessorDefinitions=
"_DEBUG"
GenerateDebugInformation=
"-1"
AdditionalOptions=
"-std=c99"
/>
<Tool
Name=
"NASMAssemblerTool"
/>
<Tool
Name=
"PreLinkEventTool"
/>
<Tool
Name=
"GCCLinkerTool"
OutputFile=
"$(OutDir)\pe2bin.exe"
/>
<Tool
Name=
"PostBuildEventTool"
/>
</Configuration>
<Configuration
Name=
"Release"
>
<Tool
Name=
"PreBuildEventTool"
/>
<Tool
Name=
"CustomBuildTool"
/>
<Tool
Name=
"GCCCompilerTool"
PreprocessorDefinitions=
"NDEBUG"
AdditionalOptions=
"-std=c99"
/>
<Tool
Name=
"NASMAssemblerTool"
/>
<Tool
Name=
"PreLinkEventTool"
/>
<Tool
Name=
"GCCLinkerTool"
OutputFile=
"$(OutDir)\pe2bin.exe"
/>
<Tool
Name=
"PostBuildEventTool"
/>
</Configuration>
</Configurations>
<Files>
<Filter
Name=
"ͷļ"
Filter=
"h;hpp;hxx"
>
</Filter>
<Filter
Name=
"Դļ"
Filter=
"cpp;c;cc;cxx"
>
<File
RelativePath=
".\main.c"
>
</File>
</Filter>
</Files>
</OSLProject>
pe2bin.puo
0 → 100644
浏览文件 @
ee71732b
添加文件
编写
预览
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论