Skip to content

Instantly share code, notes, and snippets.

@cybojanek
Last active December 15, 2015 16:19
Show Gist options
  • Save cybojanek/5288017 to your computer and use it in GitHub Desktop.
Save cybojanek/5288017 to your computer and use it in GitHub Desktop.
OMP Cracker
#include <omp.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "SHA3api_ref.h"
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define ALEN 52
#define BLOCK_SIZE 1000000
#define BLOCK_FINISHED 10
#define NEW_BLOCK 20
#define BUFSIZE 1024
char alphabet[]="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
struct result {
int minoff;
char data[1024];
char hash[1024];
char best[1024];
};
void updatepw(char *ret, unsigned long curpw) {
unsigned long r = curpw;
char *s = ret;
while(r>0) {
*(s++)=alphabet[r%ALEN];
r = r / ALEN;
}
*(s++) = 0;
}
int main(int argc, char **argv) {
// Get number of threads
int num_threads = omp_get_max_threads();
// Thread id, bitsoff
int tid, bitsoff;
// Counter
unsigned long i, b;
// Result destinations
struct result r[num_threads];
// Start block
unsigned long block = 0;
unsigned long block_size = 0;
// Min
int minoff = 1024;
char best_min[1024];
unsigned char target[] = "\x5b\x4d\xa9\x5f\x5f\xa0\x82\x80\xfc\x98\x79\xdf\x44\xf4\x18\xc8\xf9\xf1\x2b\xa4\x24\xb7\x75\x7d\xe0\x2b\xbd\xfb\xae\x0d\x4c\x4f\xdf\x93\x17\xc8\x0c\xc5\xfe\x04\xc6\x42\x90\x73\x46\x6c\xf2\x97\x06\xb8\xc2\x59\x99\xdd\xd2\xf6\x54\x0d\x44\x75\xcc\x97\x7b\x87\xf4\x75\x7b\xe0\x23\xf1\x9b\x8f\x40\x35\xd7\x72\x28\x86\xb7\x88\x69\x82\x6d\xe9\x16\xa7\x9c\xf9\xc9\x4c\xc7\x9c\xd4\x34\x7d\x24\xb5\x67\xaa\x3e\x23\x90\xa5\x73\xa3\x73\xa4\x8a\x5e\x67\x66\x40\xc7\x9c\xc7\x01\x97\xe1\xc5\xe7\xf9\x02\xfb\x53\xca\x18\x58\xb6";
int sockfd, n;
struct sockaddr_in serveraddr;
struct hostent *server;
char *hostname = "cybojanek.net";
int portno = 8000;
char buf[BUFSIZE];
char uuid[100];
// create socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0) {
perror("Error openening socket");
exit(0);
}
server = gethostbyname(hostname);
if(server == NULL) {
fprintf(stderr, "ERROR, no such host as %s\n", hostname);
exit(0);
}
/* build the server's Internet address */
bzero((char *) &serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serveraddr.sin_addr.s_addr, server->h_length);
serveraddr.sin_port = htons(portno);
/* connect: create a connection with the server */
if (connect(sockfd, &serveraddr, sizeof(serveraddr)) < 0)
perror("ERROR connecting");
bzero(buf, BUFSIZE);
//buf[0] = '';
// buf[1] = '\n';
buf[0] = 13;
buf[1] = 10;
printf("Sending...\n");
n = write(sockfd, buf, strlen(buf));
printf("Sent...%d\n", n);
bzero(buf, BUFSIZE);
n = read(sockfd, buf, BUFSIZE);
sscanf(buf, "%s %ld %ld", uuid, &block, &block_size);
printf("UUID: %s\nBlock: %ld\nSize: %ld\n", uuid, block, block_size);
printf("Number of threads: %d\n", num_threads);
// Initialize mins
for(i = 0; i < num_threads; i++) {
r[i].minoff = minoff;
}
while(1) {
printf("Working on block %ld.\n", block);
//////////////////////////////////////////////////////////////////////
#pragma omp parallel for shared(r, target, minoff) private (i, tid, bitsoff)
for(b = block; b < (block+block_size); b++) {
tid = omp_get_thread_num();
// printf("Thread: %d\n", tid);
updatepw(r[tid].data, b);
//data[strlen(data)-1]=0; // newline
Hash(1024, r[tid].data, strlen(r[tid].data)*8, r[tid].hash);
bitsoff = 0;
for(i = 0;i < 128; i++) {
if((r[tid].hash[i] & 0x80) != (target[i] & 0x80)) bitsoff++;
if((r[tid].hash[i] & 0x40) != (target[i] & 0x40)) bitsoff++;
if((r[tid].hash[i] & 0x20) != (target[i] & 0x20)) bitsoff++;
if((r[tid].hash[i] & 0x10) != (target[i] & 0x10)) bitsoff++;
if((r[tid].hash[i] & 0x08) != (target[i] & 0x08)) bitsoff++;
if((r[tid].hash[i] & 0x04) != (target[i] & 0x04)) bitsoff++;
if((r[tid].hash[i] & 0x02) != (target[i] & 0x02)) bitsoff++;
if((r[tid].hash[i] & 0x01) != (target[i] & 0x01)) bitsoff++;
}
if(bitsoff<r[tid].minoff) {
// populate the result struct
r[tid].minoff= bitsoff;
strcpy(r[tid].best, r[tid].data);
}
}
//////////////////////////////////////////////////////////////////////
#pragma omp master
// Block Finished
// Find best
for(i = 0; i < num_threads; i++) {
if(r[i].minoff < minoff) {
minoff = r[i].minoff;
strcpy(best_min, r[i].best);
}
}
// Update all threads with best
for(i = 0; i < num_threads; i++) {
r[i].minoff = minoff;
strcpy(r[i].best, best_min);
}
printf("Best Result: %s, %d\n", r[0].best, r[0].minoff);
bzero(buf, BUFSIZE);
sprintf(buf, "%s %ld %d %s\r\n", uuid, block, r[0].minoff, r[0].best);
printf("SENDING: %s\n", buf);
n = write(sockfd, buf, strlen(buf));
printf("WROTE: %d\n", n);
n = read(sockfd, buf, BUFSIZE);
sscanf(buf, "%ld %ld", &block, &block_size);
}
}
@cybojanek
Copy link
Author

Thanks tony for the base!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment