无法获取所有执行文件的内容
首先,抱歉我的英语不好。 我尝试在 linux 上获取执行文件的内容。但是出现错误。 而不是显示所有内容。它只显示其中的一部分
Myfile 是 b.out : quan@quan-desktop:~/quanrocktest/tuan1$ cat b.out �ELFquan���0�4 4($!44�4�44�4����� .... ... �4�44�4 ETC... b.out 是一个 ELF 文件
Mycode :
#include <stdio.h>
#include "stdlib.h"
int main(){
FILE *file;
char *buffer;
unsigned int fileLen;
//Open file
file = fopen("/home/quan/quanrocktest/a/b.out", "r");
//Get file length
fseek(file, 0, SEEK_END);
fileLen=ftell(file);
fseek(file, 0, SEEK_SET);
//Allocate memory
buffer=(char *)malloc(fileLen+1);
if (!buffer)
{
fprintf(stderr, "Memory error!");
fclose(file);
return 0;
}
//Read file contents into buffer
fread(buffer, fileLen, 1, file);
fclose(file);
printf("len is %d \n",fileLen);
//Do what ever with buffer
printf("%s",buffer);
free(buffer);
return 0; 结果
: quan@quan-desktop:~/quanrocktest/a$ g++ init.c
quan@quan-desktop:~/quanrocktest/a$ ./a.out
长度是 8256
�ELFquan���@quan-desktop:~/quanrocktest/a$
a.out 只显示
�ELFquan���
当我使用 vi 或 cat 时,b.out 仍然显示良好
First,sorry about my bad english.
I try to get content of excute file on linux.But there is a error.
instead of displaying all of content.It only display a part of them
Myfile is b.out :
quan@quan-desktop:~/quanrocktest/tuan1$ cat b.out
�ELFquan���0�4 4($!44�4�44�4�����
....
...
�4�44�4
etc...
b.out is an ELF file
Mycode :
#include <stdio.h>
#include "stdlib.h"
int main(){
FILE *file;
char *buffer;
unsigned int fileLen;
//Open file
file = fopen("/home/quan/quanrocktest/a/b.out", "r");
//Get file length
fseek(file, 0, SEEK_END);
fileLen=ftell(file);
fseek(file, 0, SEEK_SET);
//Allocate memory
buffer=(char *)malloc(fileLen+1);
if (!buffer)
{
fprintf(stderr, "Memory error!");
fclose(file);
return 0;
}
//Read file contents into buffer
fread(buffer, fileLen, 1, file);
fclose(file);
printf("len is %d \n",fileLen);
//Do what ever with buffer
printf("%s",buffer);
free(buffer);
return 0;
}
And result :
quan@quan-desktop:~/quanrocktest/a$ g++ init.c
quan@quan-desktop:~/quanrocktest/a$ ./a.out
len is 8256
�ELFquan���@quan-desktop:~/quanrocktest/a$
a.out just display
�ELFquan���
When i use vi or cat,b.out still display well
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
printf()
的"%s"
格式说明符旨在与 NUL 终止的字符串一起使用。因此,对printf()
的调用将停止在 ELF 图像中的第一个 NUL 字节处。The
"%s"
format specfier toprintf()
is meant to be used with NUL-terminated strings. Your call toprintf()
is thus stopping at the first NUL byte in the ELF image.对于新手来说,使用实用程序比编写程序更快。
您应该尝试nm(1), objdump(1) 或 readelf(1)。这些实用程序有很多选项。它们用于列出可执行文件或目标文件的内容。
如果你想查看裸文件内容,你应该使用hexdump(1),或ghex2 (它需要图形系统,例如侏儒桌面)。
祝你好运!是的,还有一件事:如果您不知道为什么 printf() 会这样工作,我建议您不要破解可执行文件。你现在不需要它。
Using utilities is quicker way for a newbie than writing a program.
You should try nm(1), objdump(1) or readelf(1). These utils have lot of options. They are for list the content of an executable or object file.
If you want to see the bare file content, you should use hexdump(1), or ghex2 (it requires graphical system, like Gnome Desktop).
Good luck! Yep, another thing: if you don't know, why printf() works that way, I recommend you not to hack executables. You don't need it now.