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

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

#include "stdafx.h"
#include <malloc.h>


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);

}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值