滴水逆向(内存分配-文件读写)课后作业

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

#include "stdafx.h"
#include 


void fun();
int Size(FILE*);
int main(int argc, char* argv[])
{
	fun();
	return 0;
}

//此函数返回文件的大小
int Size(FILE * p){
	
	int num = 0;
	fseek(p,0,SEEK_END);
	num = ftell(p);
	//再把指针移到开头
	fseek(p,0,SEEK_SET);
	return num;

}


void fun(){

	FILE * p = NULL;
	int FileSize = 0;
	p = fopen("C:\\Windows\\System32\\notepad.exe","rb");
	FileSize = Size(p);

	printf("文件大小是%d\n",FileSize);

	//申请内存空间
	char * Buffer = (char *)malloc(FileSize);


	if(Buffer == NULL){
		
		printf("分配空间失败");

	}
	
	else
		{

		fread(Buffer,FileSize,1,p);
	}

	
	int addr = (int)Buffer;
	printf("内存中的地址是%x\n",addr);


	free(Buffer);
	fclose(p);

}

题目2:将内存中的数据存储到一个文件中,(.exe格式),然后双击打开,看是否能够使用

int Size(FILE*);

void fun();


int main(int argc, char* argv[])
{
	fun();

	return 0;
	
}

int Size(FILE * p){
	
	int num = 0;
	fseek(p,0,SEEK_END);
	num = ftell(p);
	//再把指针移到开头
	fseek(p,0,SEEK_SET);
	return num;

}


void fun(){

	FILE * fp1 = NULL;
	FILE * fp2 = NULL;
	
	int FileSize = 0;
	
	fp1 = fopen("C:\\Windows\\System32\\notepad.exe","rb");
	fp2 = fopen("C:\\Users\\gu\\Desktop\\test.exe","wb");


	FileSize = Size(fp1);

	char * Buffer = (char*)malloc(FileSize);

	if(Buffer != NULL){
		
		fread(Buffer,FileSize,1,fp1);
		fwrite(Buffer,FileSize,1,fp2);
	}

	free(Buffer);
	fclose(fp1);
	fclose(fp2);

}

你可能感兴趣的:(滴水逆向,mfc)