程序删除自己,改写自己

程序删除自己改写自己

有这样一个问题:让程序本身能限制它的运行次数,比如只能运行5次。
目前使用的方法大都是通过读取和改写外部文件的数据来判断运行的次数,比如增加配置文件(.inf),读写系统注册表,或增加其它类似文件。也就是说,程序运行要依靠外部数据来判断运行次数。
有没有不依靠外部数据的呢。。。
有这样想法:程中文件本身有一个数据5,当这个程序结束时,把这个5变成其它数,比如4,当下次运行的时候取读4,运行结束时再把4变成3,此下去。。。这样就能通过这个数字的变化来控制程序运行次数。
大家都知道一个windows应用程序在运程中,所占用的程序文件是不能常规删除和改写的。这就是说,程序在磁盘上的文件里面的这个“10”不能在运行时改写,文件被系统写保护了(可以读取)。
现在请做这样一件事:新建一个文本文件A.txt,然后在里面写入del %0,保存之后把A.txt文件后缀后改掉,变成A.bat。del %0的意思删除文件本身。这样就建成了一个批处理文件,双击它就能运行了。双击它之后A.bat文件没了!(至于为啥windows程序不能删除自己,bat批处理文件可以删除自己,我也说不清楚,我对cmd没深入了解)

程序思路是这样的:
1、程序hello.exe读取自己到内存中
2、hello.exe在内存中查找“5:次数在这里”这个字符串
3、将上步查找到的“5:次数在这里”这个字符串改写为“4:次数在这里”
4、将内存中的全部数据写入temp.exe文件
5、hello.exe先建立temp.bat文件,调用temp.bat文件后退出
6、temp.bat先删除hello.exe,再将temp.exe改名为hello.exe,后最删除自已

我用的环境是dev-c++,win8系统,能够正常编译执行。(直接copy回去研究吧,呵呵)

 

 

#include <iostream>
#include <stdlib.h>
#include <string.h>

struct file_inf{char* name;long len;};//用来存储文件的信息 

void* mem_with_file(struct file_inf&);//将文件读入内存,并返回内存的地址。文件的长度,文件名存入file_inf结构体 
char* file_with_mem(struct file_inf&,void*);//将内存中的内容写入文件 
void* write_mem(char *dest,char *src, int n);//更改内存中的数据 
char* search_bite(void *buffer, size_t count,char* str);//查找内存中的数据 
bool  write_bat(char*); // 用来建立bat文件 
char* FileName(char*);//获取文件名 
//以上函数的具实现,请下main函数后面的定义 

int main(int argc, char** argv) 
{
	file_inf testfile;
	testfile.name=argv[0];//程序的文件名 
	testfile.len=0;
	void* p=mem_with_file(testfile);//打开程序本身文件,并将文件内容读入内存 
	testfile.name="temp.exe";//默认为本程序的文件名,要更改 
	char* A="5:次数在这里";//用于存储程序运行次数的数据,初始为5 ,要定义为常量
	//不能用A[]="5:次数在这里"这条句,常量在编译时就已经存入了,要用变量  
	char temp[50];
	strcpy(temp,A);
	char* s=search_bite(p,testfile.len,temp);//在内存中查找 5:password 这个字符串 
	//if(s!=NULL){printf("find!\n");}else{printf("Can not find!\n");};//显示查找结果 
	int times=atoi(A);//将字符串转化成数字,数字应该是5 
	if(times==0){ 
	free(p);
	p=NULL; 
	printf("次数达到0\n",times);//次数限制相关的语句可以放在这里了 
	getchar();//暂停以便看到输出的信息 
	return 0;
	}else{
	printf("剩余次数%d\n",times);	
	}
	temp[0] ='0'+times-1;//将字符5变成字符4 
	if(s!=NULL)write_mem(s,temp,strlen(temp));//更改内存中的数据(以字符串改写的方式) 
	file_with_mem(testfile,p);// 将内存中的改好的数据写入文件
	free(p);//小小内存泄露哦 
	p=NULL; 
	char* MyFileName=FileName(argv[0]);
	write_bat(MyFileName);//建立bat文件 
	printf("q%c %d3299%d\n有想法可以讨论下\n按任意键继续。。。\n",'q',411,56);// 不喜欢就关掉吧,呵呵 
	getchar();//暂停程序,这里你可以查看程序生成的临时文件 	
	system("start temp.bat") ;
	return 0;
}

void* mem_with_file(file_inf& _inf ) 
{
	long L;//文件长度单位:字节 
	FILE* fp;
	void* addr=NULL; 
	fp=fopen(_inf.name,"rb");//二进制方式读取文本内容,换行占两个字节 
	//if(fp==NULL) { printf("file can not open!\n");return NULL;}else{printf("file open!\n");};
	fseek(fp,0,2);//文件内指向末尾 
	L=(long)ftell(fp);//文件长度 
	addr=malloc(L);//分配内存空间 
	if(addr!=NULL)
	{
	fseek(fp,0,0);//文件内指向开头
	long i=fread(addr,1,L,fp);
	_inf.len=i;
	} else {
	printf("addr is NULL !\n");return NULL;
	}
	fclose(fp);
	return addr; 
}

char* file_with_mem(struct file_inf& _inf,void* buffer)
{
	FILE* fp;
	fp=fopen(_inf.name,"wb");//二进制方式,新建一个二进制文件,已存在的文件将被删除,只允许写	
	fwrite(buffer,1,_inf.len,fp);
	fclose(fp);	
	return _inf.name;
}

void* write_mem(char *dest,char *src, int n)// 将src中的前n个字节复制到dest
{
	if(n>strlen(src)) return NULL;
	return strncpy(dest,src,n);
}

char* search_bite(void *buffer, size_t count,char* str)//在以buffer为始地点,count为度长度的一块内存中查找str,若找到返回找到的位置 
{
	if(strlen(str)>count) return NULL;
	for(size_t i=0;i<=count-strlen(str);i++)
	{
		char* p=strstr((char*)buffer+i,str);
		if(p!=NULL) return p;
	}
	return NULL;
}

bool write_bat(char* fileName)
{
	FILE* fp;
	fp=fopen("temp.bat","w");//文本方式,新建文件,已存在的文件将被删除,只允许写
	if(fp==NULL) { printf("file can not open!\n");return false;}
	else
	{//以下是写入bat文件的具体内容 
	fprintf(fp,"\
	@echo off\n\
	if exist %s del %s\n\
	if exist temp.exe ren temp.exe %s\n\
	del %%0 & taskkill  /f /im cmd.exe\n",fileName,fileName,fileName);
	};
	fclose(fp);
	return true;
}

char* FileName(char* PathName){
	for(int i=strlen(PathName);i>0;i--){
		if(PathName[i-1]=='\\'||PathName[i-1]=='/')
		return &(PathName[i]);
		};
		return NULL;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您介绍如何使用koa框架来改写用户管理。 首先,我们需要安装koa框架和相关的依赖项。您可以使用以下命令进行安装: ``` npm install koa koa-router koa-bodyparser koa-json ``` 接下来,我们可以创建一个新的koa应用程序,并在应用程序中添加路由和中间件来处理用户管理逻辑。 下面是一个简单的示例,展示如何使用koa框架来实现一个用户管理API: ```javascript const Koa = require('koa'); const Router = require('koa-router'); const bodyParser = require('koa-bodyparser'); const json = require('koa-json'); const app = new Koa(); const router = new Router(); let users = [ { id: 1, username: 'user1', password: 'pass1' }, { id: 2, username: 'user2', password: 'pass2' }, { id: 3, username: 'user3', password: 'pass3' } ]; router.get('/users', (ctx) => { ctx.body = users; }); router.get('/users/:id', (ctx) => { const user = users.find(u => u.id === parseInt(ctx.params.id)); if (user) { ctx.body = user; } else { ctx.status = 404; } }); router.post('/users', (ctx) => { const { username, password } = ctx.request.body; const id = users.length + 1; users.push({ id, username, password }); ctx.body = { id, username, password }; }); router.put('/users/:id', (ctx) => { const user = users.find(u => u.id === parseInt(ctx.params.id)); if (user) { const { username, password } = ctx.request.body; user.username = username; user.password = password; ctx.body = user; } else { ctx.status = 404; } }); router.delete('/users/:id', (ctx) => { const userIndex = users.findIndex(u => u.id === parseInt(ctx.params.id)); if (userIndex !== -1) { users.splice(userIndex, 1); ctx.status = 204; } else { ctx.status = 404; } }); app.use(json()); app.use(bodyParser()); app.use(router.routes()); app.use(router.allowedMethods()); app.listen(3000, () => { console.log('Server running on port 3000'); }); ``` 在上面的示例中,我们创建了一个users数组来存储用户数据。我们使用koa-router和koa-bodyparser来处理路由和解析请求体。我们定义了五个路由来处理不同的用户管理操作:获取所有用户、获取单个用户、创建用户、更新用户和删除用户。 接着,我们使用koa-json中间件来设置响应内容的Content-Type为application/json。我们还使用koa-router和koa的allowedMethods方法来处理404错误和405错误。 最后,我们使用app.listen方法来启动应用程序,并监听3000端口。您可以使用curl或其他HTTP客户端来测试这个API,例如: ``` curl http://localhost:3000/users curl http://localhost:3000/users/1 curl -X POST -H "Content-Type: application/json" -d '{"username": "user4", "password": "pass4"}' http://localhost:3000/users curl -X PUT -H "Content-Type: application/json" -d '{"username": "user1", "password": "newpass1"}' http://localhost:3000/users/1 curl -X DELETE http://localhost:3000/users/1 ``` 以上就是一个简单的使用koa框架实现的用户管理API示例。希望可以对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值