滴水逆向作业39
将记事本的.exe文件读取的内存,并返回读取后在内存中的地址
步骤

用到的文件操作函数
1 2 3 4 5 6 7
| int fseek(FILE *stream, long int offset, int whence) stream -- 这是指向 FILE 对象的指针,该 FILE 对象标识了流。 offset -- 这是相对 whence 的偏移量,以字节为单位。 whence -- 这是表示开始添加偏移 offset 的位置。它一般指定为下列常量之一: SEEK_SET 文件的开头 0 SEEK_CUR 文件指针的当前位置 1 SEEK_END 文件的末尾 2
|
1 2
| long int ftell(FILE *stream) C 库函数 long int ftell(FILE *stream) 返回给定流 stream 的当前文件位置
|
源码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| #include<stdio.h> #include<stdlib.h> void* readtest() { FILE *fp = fopen("my_strcpy.exe","rb"); long n; void* p; if(fp == NULL) { printf("failed"); exit(0); } fseek(fp,0,2); n = ftell(fp); p = malloc(n); if(p == NULL) { printf("failed"); exit(0); } fseek(fp,0,0); fread(p,1,n,fp); return p; } int main() { char* ptr; ptr = (char*)readtest(); printf("%x\n",*ptr); return 0; }
|
将内存中的数据存储到一个文件中(.exe格式)然后双击打开看是否能够使用
步骤
先创建了个文件名叫newmy_strcpy,里面什么都没有,然后将内存刚才读取的内容写进去
源码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| #include<stdio.h> #include<stdlib.h> long n; void* readtest() { FILE *fp = fopen("my_strcpy.exe","rb"); void* p; if(fp == NULL) { printf("failed"); exit(0); } fseek(fp,0,2); n = ftell(fp); p = malloc(n); if(p == NULL) { printf("failed"); exit(0); } fseek(fp,0,0); fread(p,1,n,fp); fclose(fp); return p; } int main() { char* ptr; FILE* fp = fopen("newmy_strcpy.exe","wb"); if(fp == NULL) { printf("failed"); exit(0); } ptr = (char*)readtest(); printf("%x\n",*ptr); fwrite(ptr,1,n,fp); fclose(fp); free(ptr); return 0; }
|
执行完毕后打开新的newmy_strcpy,可以和my_strcpy有相同的效果