-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
138 lines (120 loc) · 3.38 KB
/
client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "lsp.h"
#include "lspmessage.pb-c.h"
#include <stdint.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
/*
* server parameter
*/
#define SERVER_PORT 5401
#define BUFFER_LENGTH 100
#define FALSE 0
#define SERVER_NAME "127.0.0.1"
#define NETDB_MAX_HOST_NAME_LENGTH 256
#define HASH_PWD_SIZE 128
/*
* 1.read argv[] from command line
* argv[1]: port argv[2]: hostName/address
*/
int main(int argc, char *argv[])
{
srand(12345);
if(argc<4)
{
printf("Input format should be host:port hash pswd_len\n");
exit(1);
}
char* arg_buf = malloc(128);
strcpy(arg_buf,argv[1]);
char* host = malloc(64);
char* phost = host;
char* str_port = malloc(64);
char* p = arg_buf;
while(*p != ':')
*phost++ = *p++;
*phost = '\0';
strcpy(str_port,strstr(arg_buf,":")+1);
int port = atoi(str_port);
char* hash = malloc(64);
strcpy(hash,argv[2]);
int len = atoi(argv[3]);
lsp_set_epoch_lth(2);
lsp_set_epoch_cnt(5);
lsp_set_drop_rate(0.01);
/////////
//int port = SERVER_PORT;
//////////////
lsp_client* client = lsp_client_create(host, port);
if(!client)
{
printf("Can't connect to server.\n");
return 1;
}
uint8_t buf[1024] = {0};
for(int i=0;i<5;i++)
{
sprintf(buf,"ca\n%d\n%s", len, hash);
if(!lsp_client_write(client, buf, strlen(buf)))
{
printf("Can't send message.\n");
lsp_client_close(client);
client = lsp_client_create(host, port);
continue;
}
else
{
uint8_t* result = malloc(MAX_MSG_SIZE);
if(lsp_client_read(client, result)==-1)
{
printf("Can't recv message.\n");
lsp_client_close(client);
client = lsp_client_create(SERVER_NAME, port);
if(!client)
{
printf("Can't connect to server.\n");
return 1;
}
continue;
}
if ( strlen(result) > HASH_PWD_SIZE )
{
printf("Error: Message Exceed Max Buffer Size!");
continue;
}
if(result[0] == 'f')
{
char* pos1 = strstr(result,"\n");
char* pos2 = strstr(pos1 + 1,"\n");
char* pos3 = strstr(pos2 + 1,"\n");
char* hash = malloc(40);
char* pwd = malloc(16);
memset(pwd,0,16);
memset(hash,0,40);
memcpy(hash, pos2 + 1, (pos3-1) - (pos2+1) +1);
memcpy(pwd,pos3 + 1,len);
pwd[len] = '\0';
printf("Found: %s \n",pwd);
return 0;
}
else if(result[0] == 'x')
{
printf("Not Found\n",result);
return;
}
else if(result[0] == 'n')
{
printf("Server is not ready! Please retry later!\n");
return;
}
}
}
printf("Crack fail. Please retry! \n");
}