drip39

b4nd1t

滴水逆向作业39

将记事本的.exe文件读取的内存,并返回读取后在内存中的地址

步骤

1689830341312

用到的文件操作函数

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"); //重点是打开一个exe文件,以前作业的一个程序
long n;
void* p;
if(fp == NULL)
{
printf("failed");
exit(0);
}
fseek(fp,0,2); //将文件内的指针调到最后
n = ftell(fp); //获取偏移,n就是文件的大小,单位字节
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); //在winhex中第一个字节是0x4d,这里检验一下

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有相同的效果