c - 如何修改内存并写入磁盘?
问题描述
我是 ELF 和 C 编程的新手。
我想将定位 ELF 标头的 e_shstrndx 设置为 0,以避免使用 msync 系统调用进行调试。
我编写了这段代码,但它似乎不起作用。
#include<elf.h>
#include<stdio.h>
#include<string.h>
#include<stdint.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/mman.h>
#include<sys/stat.h>
#include<sys/fcntl.h>
int
main(int argc, char *argv[]) {
int fd;
uint8_t *mem;
struct stat st;
Elf32_Ehdr *ehdr;
Elf32_Shdr *shdr;
if (argc < 2) {
printf("Usage:%s <executable>\n", argv[0]);
printf("Warning:Use it before backup\n");
exit(0);
}
/*
* Open file withe open syscall
*/
if ((fd = open(argv[1], O_RDONLY)) < 0) {
perror("open");
exit(-1);
}
/*
* Read file status
*/
if (fstat(fd, &st) < 0) {
perror("stat");
}
/*
* Map executable into memory
*/
mem = mmap (NULL, st.st_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
if (mem == MAP_FAILED) {
perror("mmap");
exit(-1);
}
ehdr = (Elf32_Ehdr *)mem;
shdr = (Elf32_Shdr *)&mem[ehdr->e_shoff];
/*
* Check magic number
*/
if (mem[0] != 0x7f && strcmp(&mem[1], "ELF")) {
fprintf(stderr, "Not an ELF file\n");
exit(-1);
}
/*
* Check if executable
*/
if (ehdr->e_type == ET_EXEC) {
fprintf(stderr, "Not an executable file\n");
exit(-1);
}
/*
* Set ehdr->e_shstrndx to 0
*/
ehdr->e_shstrndx = 0;
/*
* Sync memory into disk
*/
if (-1 == msync(mem, st.st_size, MS_ASYNC)) {
perror("msync");
exit(-1);
}
/*
* Unmap memory
*/
if (-1 == munmap(mem, st.st_size)) {
perror("munmap");
exit(-1);
}
close(fd);
}
我运行程序并运行 readelf -h 从修改后的程序中获取“Section header string table index”,没有任何改变。
我不知道我的代码有什么问题?
任何帮助将不胜感激。
解决方案
您的代码中有 2 个错误。
- 您需要使用 O_RDWR 标志而不是 O_RDONLY 打开文件。
- 您需要使用 MAP_SHARED 参数而不是 MAP_PRIVATE 调用 mmap。
这是您的代码的简化版本,其中包含修改 6 字节 ASCII 文件的第 4 个字节的日志语句:
#include<stdio.h>
#include<string.h>
#include<stdint.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/mman.h>
#include<sys/stat.h>
#include<sys/fcntl.h>
int main(int argc, char *argv[])
{
int fd;
void *mem;
char *data;
struct stat st;
int rc;
if (argc < 2) {
printf("Usage:%s <filename>\n", argv[0]);
exit(1);
}
printf("argv[1]=%s\n", argv[1]);
/*
* Open file with open syscall
*/
rc = open(argv[1], O_RDWR);
if (rc < 0) {
perror("open");
exit(-1);
}
fd = rc;
printf("fd=%d\n", fd);
/*
* Read file status
*/
if (fstat(fd, &st) < 0) {
perror("stat");
}
printf("st.st_size=%ld\n", st.st_size);
/*
* Map file into memory
*/
mem = mmap (NULL, st.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if (mem == MAP_FAILED) {
perror("mmap");
exit(-1);
}
printf("mem=%p\n", mem);
/*
* modify memory mapped data
*/
data = (char *)mem + 2;
*data = 'M';
/*
* Sync memory into disk
*/
rc = msync(mem, st.st_size, MS_ASYNC);
if (rc == -1) {
perror("msync");
exit(-1);
}
printf("msync rc=%d\n", rc);
/*
* Unmap memory
*/
rc = munmap(mem, st.st_size);
if (rc == -1) {
perror("munmap");
exit(-1);
}
printf("munmap rc=%d\n", rc);
rc = close(fd);
printf("close rc=%d\n", rc);
return 0;
}
执行:
$ cat test.dat
A
b
C
$ ./mmap test.dat
argv[1]=test.dat
fd=3
st.st_size=6
mem=0x7fbf9f79c000
msync rc=0
munmap rc=0
close rc=0
$ cat test.dat
A
M
C
$
推荐阅读
- c# - UWP 根据类按钮扩展中的 ButtonType 属性更改按钮样式的最佳方法是什么?
- html - 粘性 div 永久保留在顶角
- c++ - 如何从 sfml 窗口复制文本?
- .net-core - 如何解决我的 API 项目中的错误?
- python - 列表中每个元素的总和
- docker - 在 Windows 的 docker 上获取客户端源 IP
- python-3.x - 如何在tableau中的相同数据中显示不同的颜色?
- java - 为什么 ANSI 颜色转义码不起作用
- android - Android:默认禁用 MediaRouteButton
- python - 管理员中的 Django 按钮