| | 144 | |
|---|
| | 145 | // calculate HMAC |
|---|
| | 146 | char *hmac(u_char **msg, ssize_t len) { |
|---|
| | 147 | u_char *inner, *outer; |
|---|
| | 148 | |
|---|
| | 149 | // append inner padding to message |
|---|
| | 150 | if ((*msg = realloc(*msg, len+HMAC_BLOCK_SIZE)) == NULL) { |
|---|
| | 151 | fprintf(stderr, "Error - Unable to allocate memory: %s.\n", strerror(errno)); |
|---|
| | 152 | return(NULL); |
|---|
| | 153 | } |
|---|
| | 154 | memcpy(*msg+len, k_ipad, HMAC_BLOCK_SIZE); |
|---|
| | 155 | |
|---|
| | 156 | // compute inner hash |
|---|
| | 157 | if ((inner = (u_char *) mem_sha512sum(*msg, len+HMAC_BLOCK_SIZE)) == NULL) { |
|---|
| | 158 | fprintf(stderr, "Error - Unable to compute inner HMAC SHA512 hash.\n"); |
|---|
| | 159 | return(NULL); |
|---|
| | 160 | } |
|---|
| | 161 | |
|---|
| | 162 | // append outer padding to iner hash |
|---|
| | 163 | if ((inner = realloc(inner, HMAC_HASH_SIZE+HMAC_BLOCK_SIZE)) == NULL) { |
|---|
| | 164 | fprintf(stderr, "Error - Unable to allocate memory: %s.\n", strerror(errno)); |
|---|
| | 165 | free(inner); |
|---|
| | 166 | return(NULL); |
|---|
| | 167 | } |
|---|
| | 168 | memcpy(&inner[HMAC_HASH_SIZE], k_opad, HMAC_BLOCK_SIZE); |
|---|
| | 169 | |
|---|
| | 170 | // compute outer hash |
|---|
| | 171 | if ((outer = (u_char *) mem_sha512sum(inner, HMAC_HASH_SIZE+HMAC_BLOCK_SIZE)) == NULL) |
|---|
| | 172 | fprintf(stderr, "Error - Unable to compute outer HMAC SHA512 hash.\n"); |
|---|
| | 173 | |
|---|
| | 174 | free(inner); |
|---|
| | 175 | return((char *) outer); |
|---|
| | 176 | } |
|---|
| | 177 | |
|---|
| | 178 | |
|---|
| | 179 | // submit attack to a Nebula server |
|---|
| 168 | | // get random number |
|---|
| 169 | | srand(time(0)); |
|---|
| 170 | | rand_no = (u_int32_t) (RAND_MAX * (rand() / (RAND_MAX + 1.0))); |
|---|
| 171 | | |
|---|
| 172 | | // hash secret with random number |
|---|
| | 232 | // get nonce from server |
|---|
| | 233 | FD_ZERO(&rfds); |
|---|
| | 234 | FD_SET(sigpipe[0], &rfds); |
|---|
| | 235 | FD_SET(sock_fd, &rfds); |
|---|
| | 236 | |
|---|
| | 237 | r_timeout.tv_sec = 10; |
|---|
| | 238 | r_timeout.tv_usec = 0; |
|---|
| | 239 | |
|---|
| | 240 | /* wait for incoming data, close connection on timeout */ |
|---|
| | 241 | logmsg(LOG_DEBUG, 1, "SubmitNebula - Waiting for nonce, timeout is %d seconds.\n", |
|---|
| | 242 | (u_int16_t) r_timeout.tv_sec); |
|---|
| | 243 | |
|---|
| | 244 | switch (select(MAX(sigpipe[0], sock_fd) + 1, &rfds, NULL, NULL, &r_timeout)) { |
|---|
| | 245 | case -1: |
|---|
| | 246 | if (errno == EINTR) { |
|---|
| | 247 | if (check_sigpipe() == -1) exit(EXIT_FAILURE); |
|---|
| | 248 | break; |
|---|
| | 249 | } |
|---|
| | 250 | logmsg(LOG_ERR, 1, "SubmitNebula Error - Select failed: %m.\n"); |
|---|
| | 251 | close(sock_fd); |
|---|
| | 252 | return(-1); |
|---|
| | 253 | case 0: |
|---|
| | 254 | logmsg(LOG_ERR, 1, "SubmitNebula Warning - Did not receive nonce within %u seconds.\n", (unsigned int) r_timeout.tv_sec); |
|---|
| | 255 | close(sock_fd); |
|---|
| | 256 | return(-1); |
|---|
| | 257 | default: |
|---|
| | 258 | if (FD_ISSET(sigpipe[0], &rfds) && (check_sigpipe() == -1)) exit(EXIT_FAILURE); |
|---|
| | 259 | if (FD_ISSET(sock_fd, &rfds)) { |
|---|
| | 260 | logmsg(LOG_DEBUG, 1, "SubmitNebula - Reading nonce.\n"); |
|---|
| | 261 | for (bytes_read = 1, total_bytes = 0; bytes_read && total_bytes < 4; total_bytes += bytes_read) |
|---|
| | 262 | bytes_read = read(sock_fd, &nonce+total_bytes, 4); |
|---|
| | 263 | |
|---|
| | 264 | if (bytes_read < 0) { |
|---|
| | 265 | logmsg(LOG_ERR, 1, "SubmitNebula Error - Unable to read from socket: %m.\n"); |
|---|
| | 266 | close(sock_fd); |
|---|
| | 267 | return(-1); |
|---|
| | 268 | } |
|---|
| | 269 | logmsg(LOG_DEBUG, 1, "SubmitNebula - Nonce received.\n"); |
|---|
| | 270 | } |
|---|
| | 271 | } |
|---|
| | 272 | |
|---|
| | 273 | |
|---|
| | 274 | // hash secret with nonce |
|---|
| | 378 | // append protocol and port to cattack for HMAC calculation |
|---|
| | 379 | if ((cbuf = realloc(cbuf, cbuf_len+3)) == NULL) { |
|---|
| | 380 | logmsg(LOG_ERR, 1, "SubmitNebula Error - Unable to allocate memory: %m.\n"); |
|---|
| | 381 | close(sock_fd); |
|---|
| | 382 | return(-1); |
|---|
| | 383 | } |
|---|
| | 384 | memcpy(cbuf+cbuf_len, &attack->a_conn.protocol, 1); |
|---|
| | 385 | memcpy(cbuf+cbuf_len+1, &attack->a_conn.l_port, 2); |
|---|
| | 386 | |
|---|
| | 387 | sha512sum = hmac(&cbuf, cbuf_len+3); |
|---|
| | 388 | free(cbuf); |
|---|
| | 389 | |
|---|
| | 390 | // send length of HMAC |
|---|
| | 391 | hmac_len = strlen(sha512sum); |
|---|
| | 392 | if (write(sock_fd, &hmac_len, sizeof(hmac_len)) == -1) { |
|---|
| | 393 | logmsg(LOG_ERR, 1, "SubmitNebula Error - Writing to socket failed: %m.\n"); |
|---|
| | 394 | close(sock_fd); |
|---|
| | 395 | return(-1); |
|---|
| | 396 | } |
|---|
| | 397 | |
|---|
| | 398 | // send HMAC |
|---|
| | 399 | if (write(sock_fd, sha512sum, strlen(sha512sum)) == -1) { |
|---|
| | 400 | logmsg(LOG_ERR, 1, "SubmitNebula Error - Writing to socket failed: %m.\n"); |
|---|
| | 401 | close(sock_fd); |
|---|
| | 402 | return(-1); |
|---|
| | 403 | } |
|---|
| | 404 | free(sha512sum); |
|---|
| | 405 | |
|---|