-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.cpp
146 lines (125 loc) · 4.55 KB
/
worker.cpp
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
136
137
138
139
140
141
142
143
144
145
146
// John Keech, UIN 819000713, CSCE 438 HW2
#include "lsp_client.h"
#include <openssl/sha.h>
void getNext(char *pass, int len){
// given a current password, compute the next one to try and
// store it in place
// Ex. aaaa -> aaab; aaaz -> aaba;
for(int pos = len-1; pos >= 0; pos--){
if(pass[pos] != 'z'){
pass[pos]++;
break;
} else {
pass[pos] = 'a';
}
}
}
char* crack(char *hash, char *start, char *end){
// attempts to crack the password with brute force
// returns NULL if no match
printf("attempting to crack hash %s (%s-%s)\n",hash,start,end);
char *cur = start;
int len = strlen(cur);
unsigned char tmp[20];
char computedHash[41];
while(true){
// hash the current one and compare it
SHA1((const unsigned char*)cur,len,tmp);
for(int i = 0; i < 20; i++){
sprintf(&(computedHash[i*2]),"%02x",tmp[i]);
}
//printf("%s: %s\n",cur,computedHash);
if(memcmp(hash,&computedHash,40) == 0){
// found a match
return cur;
}
if(memcmp(cur,end,len) == 0) {
// we just tried the last one, so return
return NULL;
}
getNext(cur,len);
}
}
int main(int argc, char* argv[]){
// randomization seed requested by Dr. Stoleru
srand(12345);
if(argc < 2) {
printf("Usage: ./worker host:port\n");
return -1;
}
if(argc == 4) {
// manual command line testing for debugging
// expects to be called as: ./worker hash start end
// copy the 'start' sequence at the beginning because
// it is overwritten during the cracking process and we
// want to print it out at the end
char *start = new char[strlen(argv[2])];
memcpy(start,argv[2],strlen(argv[2]));
start[strlen(argv[2])] = '\0';
char *pass = crack(argv[1],argv[2],argv[3]);
if(pass)
printf("cracked hash %s (%s-%s): %s\n",argv[1],start,argv[3],pass);
else
printf("failed to crack hash %s (%s-%s)\n",argv[1],start,argv[3]);
delete start;
return 0;
}
// parse the port from the input args
char* portstr = strstr(argv[1],":");
int port;
if(portstr != NULL) {
port = atoi(portstr+1);
} else {
printf("Usage: ./worker host:port\n");
return -1;
}
//portstr[0] = NULL;
portstr[0]=0;
char* hash = argv[2];
int len = atoi(argv[3]);
//lsp_set_drop_rate(0.2); // 20% of packets dropped
//lsp_set_epoch_lth(0.1); // 100 ms per epoch = fast resends on failure
//lsp_set_epoch_cnt(20); // 20 epochs (2 seconds) with no response
lsp_client *client = lsp_client_create(argv[1], port);
if(!client){
// the connection to the server could not be made
printf("The connection to the server failed. Exiting...\n");
return -1;
}
printf("The connection to the server has been established\n");
// send join message to the server
char buffer[1024];
sprintf(buffer,"j");
lsp_client_write(client,(uint8_t*)buffer,2);
// wait for request chunks and then processes them
while(int bytes_read = lsp_client_read(client,(uint8_t*)buffer)){
// we have received a crack request. let's parse it
if(buffer[0] == 'c'){
char *hash = strtok(buffer+2," ");
int bufLen = 0;
// verify hash is valid
if(strlen(hash) != 40) {
printf("Invalid hash: %s\n",hash);
bufLen = sprintf(buffer,"x"); // send "not found" back to server
lsp_client_write(client,(uint8_t*)buffer,bufLen+1);
continue;
}
// grab start and end sequences
char *start = strtok(NULL, " ");
char *end = strtok(NULL, " ");
char *pass = crack(hash,start,end);
// build response message
if(pass)
bufLen = sprintf(buffer,"f %s",pass);
else
bufLen = sprintf(buffer, "x");
// send the response back to the server
lsp_client_write(client,(uint8_t*)buffer,bufLen+1);
} else {
printf("Unknown message format: %s\n",buffer);
}
}
// the connection to the server was lost
lsp_client_close(client);
return 0;
}