|
| 1 | +#include <stdio.h> |
| 2 | +#include <string.h> |
| 3 | +#include <sys/socket.h> |
| 4 | +#include <resolv.h> |
| 5 | +#include <arpa/inet.h> |
| 6 | +#include <unistd.h> |
| 7 | +#include <stdlib.h> |
| 8 | +#include <math.h> |
| 9 | +#include <sys/time.h> |
| 10 | +char flagg [] ={92,119,33,133,29,67,61,152,200,235,211,84,239,183,203,170,0}; |
| 11 | +double getTemp (){ |
| 12 | + int s, error; |
| 13 | + struct sockaddr_in addr; |
| 14 | + if((s = socket(AF_INET,SOCK_STREAM,0))<0){ |
| 15 | + //cout<<"Error 01: creating socket failed!\n"; |
| 16 | + close(s); |
| 17 | + return 0; |
| 18 | + } |
| 19 | + |
| 20 | + addr.sin_family = AF_INET; |
| 21 | + addr.sin_port = htons(80); |
| 22 | + inet_aton("146.185.151.176",&addr.sin_addr); |
| 23 | + |
| 24 | + error = connect(s,(sockaddr*)&addr,sizeof(addr)); |
| 25 | + if(error!=0) { |
| 26 | + //cout<<"Error 02: conecting to server failed!\n"; |
| 27 | + close(s); |
| 28 | + return 0; |
| 29 | + } |
| 30 | + |
| 31 | + char msg[] = "GET /data/2.5/weather?q=Saint-Petersburg&units=metric http/1.1\nHOST: api.openweathermap.org\n\n"; |
| 32 | + char answ[1024]; |
| 33 | + //cin.getline(&msg[0],256); |
| 34 | + |
| 35 | + send(s,msg,sizeof(msg),0); |
| 36 | + |
| 37 | + if (recv(s,answ,1024,0)!=0){ |
| 38 | + close(s); |
| 39 | + char * pos1 = strstr(answ,"temp"); |
| 40 | + if (pos1 !=0){ |
| 41 | + char * pos2 = strstr(pos1,","); |
| 42 | + if (pos2 !=0){ |
| 43 | + char * res = new char [10]; |
| 44 | + memcpy(res,pos1+6,pos2-pos1-6); |
| 45 | + float resd; |
| 46 | + sscanf(res,"%f",&resd); |
| 47 | + return resd-273.15; |
| 48 | + } |
| 49 | + else |
| 50 | + return 0; |
| 51 | + } |
| 52 | + else |
| 53 | + return 0; |
| 54 | + } |
| 55 | + else{ |
| 56 | + close(s); |
| 57 | + return 0; |
| 58 | + } |
| 59 | +} |
| 60 | +long long int GetTime(){ |
| 61 | + struct timeval tv; |
| 62 | + gettimeofday(&tv, NULL); |
| 63 | + double time_in_mill = (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000 ; |
| 64 | + long long int rrr = time_in_mill; |
| 65 | + return time_in_mill; |
| 66 | +} |
| 67 | +unsigned char S[ 256 ]; |
| 68 | +unsigned int i, j; |
| 69 | +/* ключевое расписание */ |
| 70 | +void rc4_init( char* key, int key_length ){ |
| 71 | + unsigned char temp; |
| 72 | + |
| 73 | + for( i = 0; i != 256; ++i ) |
| 74 | + S[ i ] = i; |
| 75 | + |
| 76 | + for( i = j = 0; i != 256; ++i ) |
| 77 | + { |
| 78 | + j = ( j + key[ i % key_length ] + S[ i ] ) % 256; |
| 79 | + temp = S[ i ]; |
| 80 | + S[ i ] = S[ j ]; |
| 81 | + S[ j ] = temp; |
| 82 | + } |
| 83 | + i = j = 0; |
| 84 | +} |
| 85 | +unsigned char rc4_output(){ |
| 86 | + unsigned char temp; |
| 87 | + i = ( i + 1 ) % 256; |
| 88 | + j = ( j + S[ i ] ) % 256; |
| 89 | + temp = S[ j ]; |
| 90 | + S[ j ] = S[ i ]; |
| 91 | + S[ i ] = temp; |
| 92 | + return S[ ( temp + S[ j ] ) % 256 ]; |
| 93 | +} |
| 94 | + |
| 95 | +int main (){ |
| 96 | + double temp = round(getTemp()); |
| 97 | + if (temp < 40){ |
| 98 | + printf("It's too cold for me :(\n"); |
| 99 | + return 0; |
| 100 | + } |
| 101 | + int proc_count = sysconf(_SC_NPROCESSORS_ONLN); |
| 102 | + if (proc_count < 32){ |
| 103 | + printf("Too slow machine :(\n"); |
| 104 | + return 0; |
| 105 | + } |
| 106 | + char buf[17]; |
| 107 | + buf[16]=0; |
| 108 | + |
| 109 | + printf("Okay, enter you name:"); |
| 110 | + system("/bin/stty raw"); |
| 111 | + unsigned int ttt=0; |
| 112 | + for(int i=0; i<16; i++){ |
| 113 | + long long int start = GetTime(); |
| 114 | + buf[i] = getchar(); |
| 115 | + long long int elaps = GetTime()-start; |
| 116 | + if (elaps < 10) |
| 117 | + ttt = ttt | 1; |
| 118 | + ttt << 1; |
| 119 | + } |
| 120 | + system("/bin/stty cooked"); |
| 121 | + if (ttt ==0){ |
| 122 | + printf("%s is invalid name\n Oh ... you are too slow :(\n",buf); |
| 123 | + return 0; |
| 124 | + } |
| 125 | + puts(""); |
| 126 | + char key [32]; |
| 127 | + //unsigned char flag[]={"AUDACTY7H32DHAPY"};// 42 911 |
| 128 | + // STCTF#AUDACTY7H32DHAPY# |
| 129 | + sprintf(&(key[0]),"%d",42+911);//temp+proc_count |
| 130 | + rc4_init((char *)&(key[0]),strlen((char *)&(key[0]))); |
| 131 | + for (int i=0; i<strlen((char*)buf); i++){ |
| 132 | + char a = flagg[i]; |
| 133 | + char b = buf[i] ^ rc4_output(); |
| 134 | + if (a != b){ |
| 135 | + printf("Invalid key :(\n"); |
| 136 | + return 0; |
| 137 | + } |
| 138 | + } |
| 139 | + printf("Okay your flag is STCTF#%s#\n",buf); |
| 140 | + |
| 141 | + |
| 142 | + return 0; |
| 143 | +} |
0 commit comments