前不久,我同学在使用多线程处理数据时,将以前的代码复制,粘贴,使用过程中出现了以前没有碰到过的报错情况,经过debug调试多次后,在一处看似寻常的if条件语句中找到了这场闹剧的罪魁祸首。下面将通过展示我在作弊程序中的思路来体现。
题意如下:
1.从N个人中抽出M个中奖
2.抽奖结果要随机
3.不能重复获奖
4.在完成了上述要求后,设法在程序中作弊,使得某些人获奖的概率更高
5.在4的基础上,尽可能隐藏你的作弊,从源代码上看不出来你作弊了
下面附上我处理这个问题的思路模型:
#define _TIMESPEC_DEFINED
#include <iostream>
#include <pthread.h>
#include <semaphore.h>
#include <windows.h>
using namespace std;
bool boom = true;
const int people = 5;
int op_master;
void *game(void *arg)
{
/*int number = (int)arg;*/
boom = true;
int circle = 0;
printf("Please enter an operation method :\n");
cin >> op_master;
if (boom)
{
printf("The selected person is No. ");
while (circle < op_master)
{
printf(".");
Sleep(1000);
circle++;
}
printf("\n");
if (!boom)
{
printf("Oh, you lost!\n");
Sleep(1500);
return NULL;
}
printf("Boom! You win!\n");
Sleep(1500);
}
fflush(stdin);
return NULL;
}
void *trick(void *arg)
{
while (true)
{
Sleep(1000);
boom = !boom;
if (op_master == -1) //监听操作按键
{
break;
}
}
return NULL;
}
int main()
{
pthread_attr_t attr;
pthread_t *threads = (pthread_t *)malloc(sizeof(pthread_t)* people);
pthread_t *trickThread = (pthread_t *)malloc(sizeof(pthread_t));
pthread_t *listenThread = (pthread_t *)malloc(sizeof(pthread_t));
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_create(trickThread, NULL, trick, NULL);
for (int i = 0; i < people; i++)
{
pthread_create(&threads[i], NULL, game, NULL);
/*pthread_join(threads[i], NULL);*/
}
for (int j = 0; j < people; j++)
{
pthread_join(threads[j], NULL);
}
/*pthread_join(*trickThread, NULL);*/
free(trickThread);
trickThread = NULL;
free(threads);
threads = NULL;
free(listenThread);
listenThread = NULL;
}
不难发现,这里使用的就是多线程,以及线程之间对全局变量的修改,这种作弊思路的核心就在于对两个线程的延迟时间上,目前trick函数和game函数中的延迟时间相同,实际效果表现为奇数号码不行,偶数号码能够中奖。如果要实现随机性,只需要将trick函数中的延迟去掉或者减为足够小就能做到几乎随机。
当然,还有好几种比较好的作弊思路,这里马克几种:
我们知道,python编程中import语句使用的库名没有路径限制,所以只需要写入需要导入的库名就能自动寻找相应库中对应的方法,所以我们只需要在当前路径的目录中添加自己的random库文件,然后导入自己的库文件,就可以在代码上看不出来我导入的是官方库还是作弊了,233333.
另一种也是将java包中的random类中的nextInt()方法,修改返回next integer返回值的取法,改为一个根据输入的抽奖号码范围和对应的号码范围中间值的分布函数,比如正态分布
当然,还有比如设计抽奖网站漏洞,实现xml注入balabala的,如果还有好的提议,欢迎交流讨论。