如何修改内存并写入磁盘?

问题描述 投票:0回答:1

我是ELF和C编程的新手。

我想设置e_shstrndx来将ELF头定位为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”,未做任何更改。

我不知道我的代码有什么问题吗?

任何帮助将不胜感激。

c mmap
1个回答
1
投票

您的代码中有2个错误。

  1. 您需要使用O_RDWR标志而不​​是O_RDONLY打开文件。
  2. 您需要使用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
$
© www.soinside.com 2019 - 2024. All rights reserved.