第二关,提供了2种通关方式。
//a little fun brought to you by bla
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <setjmp.h>
void catcher(int a)
{
setresuid(geteuid(),geteuid(),geteuid());
printf("WIN!\n");
system("/bin/sh");
exit(0);
}
int main(int argc, char **argv)
{
puts("source code is available in level02.c\n");
if (argc != 3 || !atoi(argv[2]))
return 1;
signal(SIGFPE, catcher);
return abs(atoi(argv[1])) / atoi(argv[2]);
}
需要提供2个数字进行除法,并触发SIGFPE,其中除数还不能为0。
这个地方有2个解法,通过kill的方式,手动发送SIGFPE信号触发。另外就是利用最大的负数除以-1的溢出也会导致SIGFPE。
第二个通关方式:
/* submitted by noname */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define answer 3.141593
void main(int argc, char **argv) {
float a = (argc - 2)?: strtod(argv[1], 0);
printf("You provided the number %f which is too ", a);
if(a < answer)
puts("low");
else if(a > answer)
puts("high");
else
execl("/bin/sh", "sh", "-p", NULL);
}
这个的解法有点儿意思,完全没有想到,以为通过有效位去实验能完全匹配上answer。
后来发现这个题目的解法非常简单,传入NAN即可。NAN这个特殊的数字,会导致参与计算的表达式的值都为NAN。