Exemplo n.º 1
0
// полная атака
void generation_full(std::map<int, std::string> &hashes) {
	std::string buf;
	for (int j = 0; j < 12; ++j) {
		buf.push_back(char(-127));
	}
	int pos = buf.length()-1;
	unsigned long long int collision = 0;
	while (hashes.size() < 2147483648) {
		/*if ( hashes.size() % 42946 == 0 ) {
			std::cout << hashes.size() << std::endl;
		}*/
		if ( buf[pos] == 127) {
			int b_pos = pos;
			while (buf[b_pos] == 127 && b_pos >= 0) {
				buf[b_pos] = char(-127);
				b_pos -= 1;
			}
			buf[b_pos] = char((int(buf[b_pos]) + 1));;
		}
		buf[pos] = char((int(buf[pos]) + 1));
		int hash = HashH37(buf.c_str());
		if (hashes.find(hash) != hashes.end()) {
			++collision;
		}
		hashes[hash] = buf;
	}
	std::cout <<"Collisions: " << collision << std::endl;
}
Exemplo n.º 2
0
// атака на коллизии
void generation(std::map<int, std::string> &hashes) {
	std::string buf;
	for (int i = 0; i < 3; ++i) {
		buf.push_back(char(-127));
	}
	int pos = buf.length()-1;
	int collision = 0;
	while (hashes.size() < 65536) {
		if ( buf[pos] == 127) {
			int b_pos = pos;
			while (buf[b_pos] == 127 && b_pos >= 0) {
				buf[b_pos] = char(-127);
				b_pos -= 1;
			}
			buf[b_pos] = char((int(buf[b_pos]) + 1));;
		}
		buf[pos] = char((int(buf[pos]) + 1));
		int hash = HashH37(buf.c_str());
		if (hashes.find(hash) != hashes.end()) {
			++collision;
		}
		hashes[hash] = buf;
		
	}
	std::cout <<"Collisions: " << collision << std::endl;
}
Exemplo n.º 3
0
void test2() {
	std::map<int, std::string> hashes;
	std::ifstream fin("english.txt");	
	std::string pswd;
	unsigned int collision = 0;
	unsigned int not_found = 0;
	std::cout << "Start testing on the dictionary" << std::endl;
	char buf[100];
	auto start = std::chrono::steady_clock::now();
	while (fin.getline(buf, 100)) {

		int hash = HashH37(buf);
		if (hashes.find(hash) != hashes.end()) {
			++collision;
		} else {
			hashes[hash] = buf;
			++not_found;
		}
	}
	auto stop = std::chrono::steady_clock::now();
	std::cout << "Finished" << std::endl;
	std::cout <<"Dictionary test time" << std::endl;
	std::cout << "hours:" << std::chrono::duration_cast<std::chrono::hours>(stop - start).count() << std::endl
		<< "minutes:" << std::chrono::duration_cast<std::chrono::minutes>(stop - start).count() % 60 << std::endl
		<< "seconds:" << std::chrono::duration_cast<std::chrono::seconds>(stop - start).count() % 60 << std::endl
		<< "milliseconds:" << std::chrono::duration_cast<std::chrono::milliseconds>(stop - start).count() % 1000 << std::endl
		<< "microseconds:" << std::chrono::duration_cast<std::chrono::microseconds>(stop - start).count() % 1000 << std::endl
		<< "nanoseconds:" << std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start).count() % 1000 << std::endl;
	
	std::cout << "Collisions:" << collision << std::endl;
	std::cout << "Original:" << not_found << std::endl;

}
Exemplo n.º 4
0
DWORD WINAPI myThread1(LPVOID lpParameter)
{
	SOCKET new_socket = *((SOCKET*)lpParameter);
    char hashsend[256] = "1q2w3e4r";//Случайная последовательность

    char answer[1024];
    char buffer[256];
    int n;
    //Аутентификация пользователя
    int is_auntentity_ok = 0;
    char savebuf[256];
     //do
    // {
         n = send(new_socket,"Please, give me your username and password via blank:",255,0);
       //  if (n < 0)
       //      error("ERROR writing to socket");

         memset(buffer,0,256);//Взятие данных от пользователя
         n = recv(new_socket,buffer,256,0);
         Sleep(5);
        // if (n < 0)
        //     error("ERROR reading from socket");
         printf("Here is the message: %s\n",buffer);
         char temp[256];
         memset(temp,0,256);
         memset(savebuf,0,256);
         strncpy_s(temp,buffer,strlen(buffer));
         struct ArrFields arrfields = readFile();
         int i = 0;
         int yes = 0;
         for(i = 0; i < arrfields.size; i++)
         {
             char usrpassw [256];
             memset(usrpassw,0,256);
             strcat_s(usrpassw,arrfields.arr[i].username);
             strcat_s(usrpassw," ");
             strcat_s(usrpassw,arrfields.arr[i].password);
             if(strcmp(temp,usrpassw) == 0)
             {
                 yes = 1;
                 strncpy_s(savebuf,temp,strlen(temp));
             }
         }
         printf("%i\n",yes);
         if(yes == 1)
         {
             is_auntentity_ok = 1;
             printf("%s\n",hashsend);
             printf("%i\n",strlen(hashsend));
             n = send(new_socket,hashsend,strlen(hashsend),0);
       //      if (n < 0)
       //          error("ERROR writing to socket");
         }
         else
         {
             n = send(new_socket,"Polzovatel' and Parol' neverny!",255,0);
      //       if (n < 0)
      //           error("ERROR writing to socket");
             //close(newsockfd);
             //killclient();
         }
   //  } while(is_auntentity_ok != 1);
     is_auntentity_ok = 0;
    // do
    // {
          char hashbuf[256];
          memset(hashbuf,0,256);
          strcat_s(hashbuf,savebuf);
          strcat_s(hashbuf,hashsend);
         printf("%s\n",hashbuf);
         printf("%i\n",strlen(hashbuf));
         unsigned int res = HashH37(hashbuf);//Вычисление хэш-функции
         printf("%i\n",res);
         char ans [256];
         memset(ans,0,256);
         memset(buffer,0,256);
         sprintf_s(buffer, "%d", res);
         strcpy_s(ans,buffer);
         printf("Подсчитанный хеш: %s\n",ans);
         printf("Подсчитанный хеш длина: %i\n",strlen(ans));
         memset(buffer,0,256);
         n = recv(new_socket,buffer,256,0);
         printf("Полученный хеш: %s\n",buffer);
         printf("Полученный хеш длина: %i\n",strlen(buffer));
      //   if (n < 0)
      //       error("ERROR reading from socket");
         if (strcmp(ans,buffer) == 0)
         {
             printf("Hash ok\n");
             n = send(new_socket,"Hello!",255,0);
      //       if (n < 0)
      //           error("ERROR writing to socket");
             is_auntentity_ok = 1;
            }
         else
         {
             printf("Hash not ok\n");
             n = send(new_socket,"Hash neveren",255,0);
       //      if (n < 0)
        //         error("ERROR writing to socket");
             killclient();
            }
    // } while(is_auntentity_ok != 1);
     while(1)
     {
         memset(buffer,0,256);
         memset(answer,0,256);
         n = recv(new_socket,buffer,256,0);
       //  if (n < 0)
       //      error("ERROR reading from socket");
         printf("Here is the message: %s\n",buffer);
         char tempbuf[256];
         memset(tempbuf,0,256);
         strncpy_s(tempbuf,buffer,strlen(buffer)-1);
         printf("%s\n",tempbuf);
         if(strcmp(tempbuf,"logout") == 0)
         {
             printf("logout yes\n");
             n = send(new_socket,"Good Buy!",255,0);
    //         if (n < 0)
    //             error("ERROR writing to socket");
             char path[256];
             memset(path,0,256);
             closesocket(new_socket);
         }
         else if(strcmp(tempbuf,"who") == 0)
         {
             printf("who yes\n");
             char whomessage[1024];
             memset(whomessage,0,1024);
             chDirectory(arrfields, "/home/anton/workspace/tcpproj", "Anton");
             strcpy_s(whomessage,createWhoMessage(arrfields));
             n = send(new_socket,whomessage,255,0);
    //         if (n < 0)
     //            error("ERROR writing to socket");
         }

         else
         {
             printf("%s\n",buffer);


             FILE *fp;
             int status;
             char path[256];
             memset(path,0,256);
             fp = _popen(buffer, "r");
             if (fp == NULL)
                 error("Failed to execute a command in the terminal\n");
             char prov[256];
             memset(prov,0,256);
             strncpy_s(prov,buffer,strlen(buffer)-1);
             if((strcmp(prov,"cd") == 0) || (strcmp(prov,"cd ..") == 0))
             {
                 printf("cdok");
                 strcpy_s(answer,"cdok");
             }
             else {
                 while (fgets(path, 256, fp) != NULL)
                 {
                     printf("%s", path);
                     memset(buffer,0,256);
                     strncpy_s(buffer,path,strlen(path));
                     strcat_s(answer,buffer);
                 }
             }
             n = send(new_socket,answer,strlen(answer),0);
    //         if (n < 0)
    //             error("ERROR writing to socket");
             status = _pclose(fp);
             if (status == -1) {
                 error("Error with executing of command\n");
             }
         }
     }
     closesocket(new_socket);
	return 0;
}
Exemplo n.º 5
0
int main(int argc, char *argv[])
{
	int pid = getpid();
	 int sockfd, portno, n;//Дескриптор сокета
	 struct sockaddr_in serv_addr;//Адрес сервера
	 struct hostent *server;//IP-address

	 char buffer[256];//Полученное сообщение
	 if (argc < 3) {//Ввести имя хоста, если не введено
	     fprintf(stderr,"usage %s hostname port\n", argv[0]);
	     exit(0);
	 }
	 portno = atoi(argv[2]);//Получение номера порта сервера
	 sockfd = socket(AF_INET, SOCK_STREAM, 0);//Получение дескриптора сокета
	 if (sockfd < 0)//Если получить дескриптор не удалось
	     error("ERROR opening socket");
	 server = gethostbyname(argv[1]);//Взятие IP-адреса сервера
	 if (server == NULL) {//Если такого имени не существует
	     fprintf(stderr,"ERROR, no such host\n");
	     exit(0);
	 }
	 bzero((char *) &serv_addr, sizeof(serv_addr));//инициализация сервера
	 serv_addr.sin_family = AF_INET;//Тип используемого транспортного протокола
	 bcopy((char *)server->h_addr,(char *)&serv_addr.sin_addr.s_addr,//Адрес сервера
	         server->h_length);
	 serv_addr.sin_port = htons(portno);//Порт сервера
	 //printf("h_addr: %s\n", inet_ntoa(serv_addr.sin_addr));
	 if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)//Подсоединение к серверу
	      error("ERROR connecting");

     char passbuf[256];
     int is_auntentity_ok = 0;
     //do
     //{
    	 bzero(buffer,256);
    	 n = read(sockfd,buffer,255);//Чтение полученного сообщения
    	 printf("%s\n",buffer);
    	 printf("Please enter the message: ");//Введите сообщение
    	 bzero(buffer,256);
    	 bzero(passbuf,256);
    	 fgets(buffer,256,stdin);//Запись сообщения от пользователоя в массив
    	 n = write(sockfd,buffer,strlen(buffer)-1);//Отправка сообщения на сервер
    	 strncpy(passbuf,buffer,strlen(buffer)-1);
    	 if (n < 0)
    		 error("ERROR writing to socket");
    	 bzero(buffer,256);
    	 n = read(sockfd,buffer,255);//Чтение полученного сообщения
    	 printf("%s\n",buffer);
		 printf("%i\n",strlen(buffer));
    	 if(strcmp(buffer,"Polzovatel' and Parol' neverny!") == 0)
    	 {
    		 is_auntentity_ok = 1;
    	 }
    // }while(is_auntentity_ok != 1);
     is_auntentity_ok = 0;
     //do
     //{
     char hashbuf[256];
     bzero(hashbuf,256);
     strcat(hashbuf,passbuf);
     strcat(hashbuf,buffer);
	 printf("%s\n",hashbuf);
	 printf("%i\n",strlen(hashbuf));
    	 unsigned int res = HashH37(hashbuf);
    	 printf("%i\n",res);
    	 bzero(buffer,256);
    	 sprintf(buffer, "%d", res);
    	 n = write(sockfd,buffer,256);//Отправка сообщения на сервер
    	 if (n < 0)
    		 error("ERROR writing to socket");
    	 bzero(buffer,256);
    	 n = read(sockfd,buffer,255);//Чтение полученного сообщения
    	 if (n < 0)
    		 error("ERROR reading from socket");
    	 printf("%s\n",buffer);//Выведение сообщения на экран
    	 if(strcmp(buffer,"Hello!") == 0)
    		 is_auntentity_ok = 1;

    // }
    // while(is_auntentity_ok != 1);
	 int i = 0;
	 for(i = 0; i < 10; i++)
	 {
		 printf("Please enter the message: ");//Введите сообщение
		 bzero(buffer,256);
		 fgets(buffer,256,stdin);//Запись сообщения от пользователоя в массив
		 n = write(sockfd,buffer,strlen(buffer));//Отправка сообщения на сервер
		 if (n < 0)
		     error("ERROR writing to socket");
		 char tmp[256];
		 bzero(tmp,256);
		 strncpy(tmp,buffer,strlen(buffer)-1);
		 if(strcmp(tmp,"logout") == 0)
		 {
			 printf("sovpalo\n");
        	 FILE *fp;
        	 int status;
        	 char path[256];
        	 bzero(path,256);
        	 bzero(buffer,256);
        	 char command[256];
        	 bzero(command,256);
        	 strcat(command,"kill -s 9 ");
        	 char n_str[10];
        	 bzero(n_str,10);
        	 sprintf(n_str, "%d", pid);
        	 strcat(command,n_str);
        	 fp = popen(command, "r");
        	 if (fp == NULL)
        		 error("Failed to execute a command in the terminal\n");
        	 status = pclose(fp);
        	 if (status == -1) {
        		 error("Error with executing of command\n");
        	 }
		 }
		 bzero(buffer,256);
		 n = read(sockfd,buffer,255);//Чтение полученного сообщения
		 if (n < 0)
		     error("ERROR reading from socket");

		 printf("%s\n",buffer);//Выведение сообщения на экран
	  }
      close(sockfd);//Закрытие сокета
	  return 0;
}
Exemplo n.º 6
0
int main(int argc , char *argv[])
{  
	setlocale(LC_ALL,"Russian");
	
	DWORD myThreadID;
	WSADATA wsa;
    SOCKET sockfd, new_socket;
	int portno;
    char hashsend[256] = "1q2w3e4r";
    int clilen;
    struct sockaddr_in serv_addr, cli_addr;

	printf("\nInitialising Winsock...");
    if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
    {
        printf("Failed. Error Code : %d",WSAGetLastError());
        return 1;
    }
      
    printf("Initialised.\n");
      
    //Create a socket
    if((sockfd = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
    {
        printf("Could not create socket : %d" , WSAGetLastError());
    }
  
    printf("Socket created.\n");
    
    memset((char *) &serv_addr,0, sizeof(serv_addr));
    portno = 8888;
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(portno);
     //Bind
    if( bind(sockfd ,(struct sockaddr *)&serv_addr , sizeof(serv_addr)) == SOCKET_ERROR)
    {
        printf("Bind failed with error code : %d" , WSAGetLastError());
    }
      
    puts("Bind done");
  
    //Listen to incoming connections
    listen(sockfd , 5);
      
    //Accept and incoming connection
    puts("Waiting for incoming connections...");
	clilen = sizeof(struct sockaddr_in);
		new_socket = accept(sockfd , (struct sockaddr *)&cli_addr, &clilen);
		if (new_socket == INVALID_SOCKET)
		{
			printf("accept failed with error code : %d" , WSAGetLastError());
		}

    char answer[1024];
    char buffer[256];
    int n;
    //Аутентификация пользователя
    int is_auntentity_ok = 0;
    char savebuf[256];
     //do
    // {
         n = send(new_socket,"Please, give me your username and password via blank:",255,0);
       //  if (n < 0)
       //      error("ERROR writing to socket");

         memset(buffer,0,256);//Взятие данных от пользователя
         n = recv(new_socket,buffer,256,0);
        // if (n < 0)
        //     error("ERROR reading from socket");
         printf("Here is the message: %s\n",buffer);
         char temp[256];
         memset(temp,0,256);
         memset(savebuf,0,256);
         strncpy_s(temp,buffer,strlen(buffer));
         struct ArrFields arrfields = readFile();
         int i = 0;
         int yes = 0;
         for(i = 0; i < arrfields.size; i++)
         {
             char usrpassw [256];
             memset(usrpassw,0,256);
             strcat_s(usrpassw,arrfields.arr[i].username);
             strcat_s(usrpassw," ");
             strcat_s(usrpassw,arrfields.arr[i].password);
             if(strcmp(temp,usrpassw) == 0)
             {
                 yes = 1;
                 strncpy_s(savebuf,temp,strlen(temp));
             }
         }
         printf("%i\n",yes);
         if(yes == 1)
         {
             is_auntentity_ok = 1;
             printf("%s\n",hashsend);
             printf("%i\n",strlen(hashsend));
             n = send(new_socket,hashsend,strlen(hashsend),0);
       //      if (n < 0)
       //          error("ERROR writing to socket");
         }
         else
         {
             n = send(new_socket,"Polzovatel' and Parol' neverny!",255,0);
      //       if (n < 0)
      //           error("ERROR writing to socket");
             //close(newsockfd);
             //killclient();
         }
   //  } while(is_auntentity_ok != 1);
     is_auntentity_ok = 0;
    // do
    // {
          char hashbuf[256];
          memset(hashbuf,0,256);
          strcat_s(hashbuf,savebuf);
          strcat_s(hashbuf,hashsend);
         printf("%s\n",hashbuf);
         printf("%i\n",strlen(hashbuf));
         unsigned int res = HashH37(hashbuf);//Вычисление хэш-функции
         printf("%i\n",res);
         char ans [256];
         memset(ans,0,256);
         memset(buffer,0,256);
         sprintf_s(buffer, "%d", res);
         strcpy_s(ans,buffer);
         printf("Подсчитанный хеш: %s\n",ans);
         printf("Подсчитанный хеш длина: %i\n",strlen(ans));
         memset(buffer,0,256);
         n = recv(new_socket,buffer,256,0);
         printf("Полученный хеш: %s\n",buffer);
         printf("Полученный хеш длина: %i\n",strlen(buffer));
      //   if (n < 0)
      //       error("ERROR reading from socket");
         if (strcmp(ans,buffer) == 0)
         {
             printf("Hash ok\n");
             n = send(new_socket,"Hello!",255,0);
      //       if (n < 0)
      //           error("ERROR writing to socket");
             is_auntentity_ok = 1;
            }
         else
         {
             printf("Hash not ok\n");
             n = send(new_socket,"Hash neveren",255,0);
       //      if (n < 0)
        //         error("ERROR writing to socket");
             killclient();
            }
    // } while(is_auntentity_ok != 1);
     while(1)
     {
         memset(buffer,0,256);
         memset(answer,0,256);
         n = recv(new_socket,buffer,256,0);
       //  if (n < 0)
       //      error("ERROR reading from socket");
         printf("Here is the message: %s\n",buffer);
         char tempbuf[256];
         memset(tempbuf,0,256);
         strncpy_s(tempbuf,buffer,strlen(buffer)-1);
         printf("%s\n",tempbuf);
         if(strcmp(tempbuf,"logout") == 0)
         {
             printf("logout yes\n");
             n = send(new_socket,"Good Buy!",255,0);
    //         if (n < 0)
    //             error("ERROR writing to socket");
             char path[256];
             memset(path,0,256);
             closesocket(new_socket);
         }
         else if(strcmp(tempbuf,"who") == 0)
         {
             printf("who yes\n");
             char whomessage[1024];
             memset(whomessage,0,1024);
             chDirectory(arrfields, "/home/anton/workspace/tcpproj", "Anton");
             strcpy_s(whomessage,createWhoMessage(arrfields));
             n = send(new_socket,whomessage,255,0);
    //         if (n < 0)
     //            error("ERROR writing to socket");
         }

         else
         {
             printf("%s\n",buffer);


             FILE *fp;
             int status;
             char path[256];
             memset(path,0,256);
             fp = _popen(buffer, "r");
             if (fp == NULL)
                 error("Failed to execute a command in the terminal\n");
             char prov[256];
             memset(prov,0,256);
             strncpy_s(prov,buffer,strlen(buffer)-1);
             if((strcmp(prov,"cd") == 0) || (strcmp(prov,"cd ..") == 0))
             {
                 printf("cdok");
                 strcpy_s(answer,"cdok");
             }
             else {
                 while (fgets(path, 256, fp) != NULL)
                 {
                     printf("%s", path);
                     memset(buffer,0,256);
                     strncpy_s(buffer,path,strlen(path));
                     strcat_s(answer,buffer);
                 }
             }
             n = send(new_socket,answer,strlen(answer),0);
    //         if (n < 0)
    //             error("ERROR writing to socket");
             status = _pclose(fp);
             if (status == -1) {
                 error("Error with executing of command\n");
             }
         }
     }

  /*  while(1)
    {
		clilen = sizeof(struct sockaddr_in);
		printf("%i\n",clilen);
		newsockfd = accept(sockfd , (struct sockaddr *)&cli_addr, &clilen);
		if (newsockfd == INVALID_SOCKET)
		{
			printf("accept failed with error code : %d" , WSAGetLastError());
		}
		printf("Est' soedineniye\n");

		HANDLE myHandle1 = CreateThread(0, 0, myThread1,&newsockfd, 0, &myThreadID);
		//CloseHandle(myHandle1);
    }*/
    closesocket(sockfd);
    WSACleanup();
    return 0;
}