generator source code

来源:互联网 发布:迅龙数据恢复软件官网 编辑:程序博客网 时间:2024/05/20 07:58
#define _XOPEN_SOURCE
#ifndef __USE_GNU
#define __USE_GNU
#endif
#define _GNU_SOURCE
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <crypt.h>
#include <sched.h>

#define CHARS_LEN 68

static char orig_str[256] = { 0 };
static char salt[56] = { 0 };

pthread_mutex_t rwlock;
static unsigned long long  cpu_count = 0;

void init(char *characters)
{
    int i;

    for (i = 'a'; i <= 'z'; i++) {
        characters[strlen(characters)] = i;
    }

    for (i = 'A'; i <= 'Z'; i++) {
        characters[strlen(characters)] = i;
    }

    for (i = '0'; i <= '9'; i++) {
        characters[strlen(characters)] = i;
    }

    characters[strlen(characters)] = '!';
    characters[strlen(characters)] = '@';
    characters[strlen(characters)] = '#';
    characters[strlen(characters)] = '$';
    characters[strlen(characters)] = '%';
    characters[strlen(characters)] = '&';
}

int crack(const char *key)
{
    char result[256] = { 0 };
    char lstr[256] = { 0 };
    char lkey[64] = { 0 };
    char lsalt[56] = { 0 };

    strcpy(lstr, orig_str);
    strncpy(lkey, key, 64);
    strncpy(lsalt, salt, 56);
    sprintf(result, "%s", crypt(lkey, lsalt));
//    printf("key:%s,result:%s\n", lkey, result);
    if (0 == strcmp(lstr, result)) {
        return 0;
    } else {
        return 1;
    }
    return 1;
}

void one(const char *chars, char *password)
{
    char key[64] = { 0 };
    int i, pos = strlen(password);

    strcpy(key, password);
    for (i = 0; i < CHARS_LEN; i++) {
        sprintf(key + pos, "%c", chars[i]);
        crack(key);
    }
}

void two(const char *chars, char *password)
{
    char key[64] = { 0 };
    int i, pos = strlen(password);

    strcpy(key, password);
    for (i = 0; i < CHARS_LEN; i++) {
        sprintf(key + pos, "%c", chars[i]);
        one(chars, key);
    }
}

void three(const char *chars, char *password)
{
    char key[64] = { 0 };
    int pos = strlen(password), i;

    strcpy(key, password);
    for (i = 0; i < CHARS_LEN; i++) {
        sprintf(key + pos, "%c", chars[i]);
        two(chars, key);
    }
}

void four(const char *chars, char *password)
{
    int i, pos = strlen(password);
    for (i = 0; i < CHARS_LEN; i++) {
        sprintf(password + pos, "%c", chars[i]);
        three(chars, password);
    }
}

void five(const char *chars, char *password)
{
    int i, pos = strlen(password);
    for (i = 0; i < CHARS_LEN; i++) {
        sprintf(password + pos, "%c", chars[i]);
        four(chars, password);
    }
}

void six(const char *chars, char *password)
{
    int i, pos = strlen(password);
    for (i = 0; i < CHARS_LEN; i++) {
        sprintf(password + pos, "%c", chars[i]);
        five(chars, password);
    }
}

void seven(const char *chars, char *password)
{
    int i, pos = strlen(password);
    for (i = 0; i < CHARS_LEN; i++) {
        sprintf(password + pos, "%c", chars[i]);
        six(chars, password);
    }
}

void eight(const char *chars, char *password)
{
    int i, pos = strlen(password);
    for (i = 0; i < CHARS_LEN; i++) {
        sprintf(password + pos, "%c", chars[i]);
        seven(chars, password);
    }
}

void nine(const char *chars, char *password)
{
    int i, pos = strlen(password);
    for (i = 0; i < CHARS_LEN; i++) {
        sprintf(password + pos, "%c", chars[i]);
        eight(chars, password);
    }
}

void ten(const char *chars, char *password)
{
    int i, pos = strlen(password);
    for (i = 0; i < CHARS_LEN; i++) {
        sprintf(password + pos, "%c", chars[i]);
        nine(chars, password);
    }
}

void eleven(const char *chars, char *password)
{
    int i, pos = strlen(password);
    for (i = 0; i < CHARS_LEN; i++) {
        sprintf(password + pos, "%c", chars[i]);
        ten(chars, password);
    }
}

void twelve(const char *chars, char *password)
{
    int i, pos = strlen(password);
    for (i = 0; i < CHARS_LEN; i++) {
        sprintf(password + pos, "%c", chars[i]);
        eleven(chars, password);
    }
}

struct tdata {
    char chars[256];
    char password[256];
};

#define CPU_NUM 8

void *three_thread(void *arg)
{
    struct tdata *ptr = (struct tdata *)arg;
    char key[64] = { 0 };
    char chars[256] = { 0 };
    int i;
    unsigned long long tmp;
    cpu_set_t cpuset;

    strcpy(key, ptr->password);
    strcpy(chars, ptr->chars);

    pthread_mutex_lock(&rwlock);
    for (i=1; i<CPU_NUM; i++) {
        tmp = 1 << i;
        if (0 == (cpu_count & tmp)) {
            cpu_count |= tmp;
            break;
        }
    }
    pthread_mutex_unlock(&rwlock);
    CPU_ZERO(&cpuset);
    CPU_SET(i, &cpuset);

    int s = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
    if (s != 0) {
        printf("pthread_setaffinity_np error:%d\n", s);
    }

    /* Check the actual affinity mask assigned to the thread */
    s = pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
    if (s != 0) {
            printf("pthread_getaffinity_np:%d\n", s);
    }

    printf("Set returned by pthread_getaffinity_np() contained:\n");
    for (i = 1; i < CPU_NUM; i++)
    if (CPU_ISSET(i, &cpuset)) {
        printf("    CPU %d\n", i);
    }

    three(chars, key);

    pthread_mutex_lock(&rwlock);
    cpu_count &= ~tmp;
    pthread_mutex_unlock(&rwlock);

    pthread_exit(0);
}

void four_thread(const char *chars, char *password)
{
    pthread_t tid[CHARS_LEN];
    struct tdata data[CHARS_LEN];
    int i, pos = strlen(password);
    unsigned long long cur_cpu_count;
    char key[64] = { 0 };

    strcpy(key, password);
    for (i = 0; i < CHARS_LEN; i++) {
        pthread_mutex_lock(&rwlock);
        cur_cpu_count = cpu_count;
        pthread_mutex_unlock(&rwlock);

        printf("cur:%llX:%d\n", cur_cpu_count, i);
        if (cur_cpu_count >= ((1<<CPU_NUM)-2)) {
            i--;
            sleep(10);
            continue;
        }

        sprintf(key + pos, "%c", chars[i]);
        printf("four key:%s\n", key);
        memset(&data[i], 0, sizeof(struct tdata));
        strcpy(data[i].chars, chars);
        strcpy(data[i].password, key);
        pthread_create(&tid[i], NULL, &three_thread, &data[i]);

        pthread_detach(tid[i]);
        sleep(3);
    }

    pthread_mutex_lock(&rwlock);
    cur_cpu_count = cpu_count;
    pthread_mutex_unlock(&rwlock);

    while (cur_cpu_count > 0) {
        printf("cur:%lld\n", cur_cpu_count);
        sleep(5);
        pthread_mutex_lock(&rwlock);
        cur_cpu_count = cpu_count;
        pthread_mutex_unlock(&rwlock);
    }
}

void main_work(char *chars, int jump)
{
    char password[128] = { 0 };
    int i[13];

    if (jump <= 0) {
        printf("exit!\n");
        return;
    }

    switch (jump) {
    case 1:
        one(chars, password);
        return;
    case 2:
        two(chars, password);
        return;
    case 3:
        three(chars, password);
        return;
    case 4:
//              four(chars, password);
        four_thread(chars, password);
        return;
    case 5:
        five(chars, password);
        return;
    case 6:
        six(chars, password);
        return;
    case 7:
        seven(chars, password);
        return;
    case 8:
        eight(chars, password);
        return;
    case 9:
        nine(chars, password);
        return;
    case 10:
        ten(chars, password);
        return;
    case 11:
        eleven(chars, password);
        return;
    case 12:
        twelve(chars, password);
        return;
    default:
        printf("12 is all!\n");
        return;
    }
}

int main(int argc, char **argv)
{
    char chars[256] = { };
    int jump;

    if (argc < 2) {
        printf("error!\n");
    }
    jump = atoi(argv[1]);
    init(chars);

    strcpy(orig_str,
           "$6$c.brj1$M9ambFoJ7FIguxlXuxruLDWEv44MDGo.BuGG.v4z3okLhEi4blw3wzqM1");
    strcpy(salt, "$6$c.brj1$");

    if (pthread_mutex_init(&rwlock, NULL) != 0) {
        printf("\n mutex init failed\n");
        return 1;
    }

    cpu_count = 0;
    main_work(chars, jump);

    pthread_mutex_destroy(&rwlock);

    return 1;
}


all: linux-cr.c
    gcc -o cr linux-cr.c -lpthread -lcrypt
clean::
    rm -f cr


0 0
原创粉丝点击