-
Notifications
You must be signed in to change notification settings - Fork 1
/
cypher.pl
executable file
·596 lines (435 loc) · 12.6 KB
/
cypher.pl
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
#!/usr/bin/perl
use v5.10;
use warnings;
use strict;
use autodie;
use POSIX qw(strftime);
use Data::Dumper;
$Data::Dumper::Sortkeys = 1;
use Switch;
use Carp;
use Crypt::Rijndael;
use File::Basename qw(basename);
use File::Slurp qw(read_file write_file);
use Term::ReadLine;
use Getopt::Long;
use Pod::Usage;
my $DEF_CYPHER_VERSION = 2;
my $STORE_VER_3 = 3;
my $STORE_VER_4 = 4;
my $DEF_STORE_VERSION = $STORE_VER_4;
my $ENCRYPTED_FILE_VER_1 = 5;
my $STANDBY_TIMEOUT = 300;
my $last_user_active :shared = time();
my $term;
my $filename;
my $cypher;
my $data;
# This is a weird hack to make this file testable.
# If it is included from the test, it will do nothing.
# I don't want to extract all the logic into modules
# to avoid the need of installing them into the system.
# This is a simple script that contains all the logic in it.
if (basename($0) eq 'cypher.pl') {
cypher();
}
sub cypher {
$term = Term::ReadLine->new('Cypher')
or croak "Term Readline error";
GetOptions(
'encrypt|enc|e' => sub {enc_cmd(\&encrypt_file)},
'decrypt|dec|d' => sub {enc_cmd(\&decrypt_file)},
'help' => sub {pod2usage(-verbose => 2)},
) or pod2usage();
$filename = shift(@ARGV)
or print STDERR "File parameter is not found\n"
and pod2usage();
$cypher = mk_cypher($term, $filename);
$data = load_data($cypher, $filename);
while(1) {
my $line = $term->readline('cypher > ');
last if !defined $line;
last if (time() - $last_user_active > $STANDBY_TIMEOUT);
$last_user_active = time();
$line = trim($line);
next unless $line;
my ($cmd,@args) = split(/\s+/,$line,3);
switch($cmd) {
case "search" { search(@args) }
case "get" { get(0, @args) }
case "history" {get(1, @args)}
case "put" { put(@args) }
case "del" { del(@args)}
case "rm" { del(@args)}
case "help" {help(@args)}
else { say "No such command '$cmd'\n" }
}
}
system('clear');
exit 0;
}
########## Data Storage functions #########################
sub help {
pod2usage(
-verbose => 99,
-exitval => "NOEXIT",
-sections => ["USER COMMANDS", "STORAGE MODE FEATURES"],
-noperldoc => 1,
);
}
sub put {
my $key = shift;
my $val = shift;
unless ($key && $val) {
say "syntax: put KEY VAL";
return;
}
push @{$data->{$key}}, [$val, int(time)];
say "$key stored";
store_data($cypher, $data, $filename);
}
sub get {
my $get_history = shift || 0;
my $re = shift;
unless($re) {
say "syntax: get REGEXP";
return;
}
my @keys = grep{$_ =~ /^$re$/}(sort keys %$data);
unless(scalar(@keys)) {
say "No keys matching '$re' found!";
return;
}
if ($get_history) {
if (scalar(keys @keys) > 1) {
say "more than one elements found: ". join(' ', @keys);
return;
}
my $key = $keys[0];
for my $val (@{$data->{$key}}) {
my $val_time = strftime("%Y-%m-%d %H:%M:%S",localtime($val->[1]));
say "[$val_time]: $val->[0]";
}
} else {
foreach my $key(@keys) {
say "$key: " . $data->{$key}->[-1]->[0];
}
}
}
sub del {
my $key = shift;
my $val = $data->{$key};
if ($data->{$key}) {
delete $data->{$key};
say "$key: $val deleted";
store_data($cypher, $data, $filename);
} else {
say "No such key '$key' found";
}
}
sub search {
my $re = shift || '';
my @keys = grep{$_ =~ /$re/}(sort keys %$data);
say join("\n", @keys);
}
########### Encryption functions #############################
sub mk_cypher {
my $term = shift;
my $filename = shift;
my $key = read_password($term, $filename);
my $cypher = Crypt::Rijndael->new( $key, Crypt::Rijndael::MODE_CBC() );
return $cypher;
}
sub enc_cmd {
my $func = shift;
my $filename = shift @ARGV;
defined $filename && -f $filename
or pod2usage(-verbose => 99, -sections=>["SYNOPSIS", "ARGUMENTS"]);
my $cypher = mk_cypher($term, $filename);
$data = &$func($cypher, $filename);
binmode(STDOUT);
print $data;
exit(0);
}
sub encrypt_file {
my $cypher = shift;
my $filename = shift;
my $out_filename = shift || "-";
open(my $file, "<", $filename);
binmode($file);
my $outfile;
if ($out_filename eq "-") {
$outfile = *STDOUT;
} else {
open($outfile, ">", $out_filename);
}
binmode($outfile);
my $version = pack('n', $ENCRYPTED_FILE_VER_1);
syswrite($outfile, $version);
my $pad_length = 0;
while($pad_length == 0) {
my $data = '';
my $bytes_read = sysread($file, $data, 4096); # read by 4k blocks
if (!defined $bytes_read) {
croak("can't read file $file: $!");
} elsif (!$bytes_read) {
last;
}
if (($bytes_read % 16) != 0) {
$pad_length = 16 - ($bytes_read % 16);
}
if ($pad_length != 0) {
my $pad = '~' x $pad_length;
$data .= $pad;
}
$data = $cypher->encrypt($data);
syswrite($outfile, $data);
}
close($file);
syswrite($outfile, pack("C", $pad_length));
if ($out_filename eq "-") {
close($out_filename);
}
}
sub decrypt_file {
my $cypher = shift;
my $filename = shift;
my $out_filename = shift || "-";
open(my $file, "<", $filename);
binmode($file);
my $outfile;
if ($out_filename eq "-") {
$outfile = *STDOUT;
} else {
open($outfile, ">", $out_filename);
}
binmode($outfile);
my $version;
my $bytes_read = sysread($file, $version, 2);
($version) = unpack('n', $version);
if (int($version) == $ENCRYPTED_FILE_VER_1) {
my $last_block = '';
$bytes_read = sysread($file, $last_block, 4096);
while($bytes_read == 4096) {
syswrite($outfile, $cypher->decrypt($last_block));
$last_block = '';
$bytes_read = sysread($file, $last_block, 4096);
}
# decrypt last block
if (($bytes_read - 1) % 16 != 0) {
croak("Unexpected end of file, bytes_read = $bytes_read");
}
my $pad_length = unpack('C', substr($last_block,-1));
my $last_decrypted = $cypher->decrypt(substr($last_block, 0, -1));
if ($pad_length > 0) {
$last_decrypted = substr($last_decrypted, 0, -1*$pad_length);
}
syswrite($outfile, $last_decrypted);
} else {
croak("Unknown file encryption format");
}
if ($out_filename eq "-") {
close($out_filename);
}
}
sub encrypt {
my $cypher = shift;
my $data = shift;
my $version = pack('n', $DEF_CYPHER_VERSION);
my $pad = '~' x (16 - (length($data) % 16));
$data .= $pad;
return $version . pack('Ca*', length($pad), $cypher->encrypt($data));
}
sub decrypt {
my $cypher = shift;
my $data = shift;
(my $version, $data) = unpack('na*', $data);
if ($version == $DEF_CYPHER_VERSION) {
(my $pad_length, $data) = unpack('Ca*', $data);
$data = $cypher->decrypt($data);
$data = substr($data, 0, -1*$pad_length);
}
return $data;
}
sub read_password {
my $term = shift;
my $filename = shift;
my $term_attribs = $term->Attribs;
$term_attribs->{redisplay_function} = $term_attribs->{shadow_redisplay};
my $key = $term->readline("Enter Password for $filename: ");
$term->remove_history($term->where_history);
$term_attribs->{redisplay_function} = undef;
$key .= '~' x (32 - length($key));
$term_attribs->{completion_function} = \&autocomplete;
return $key;
}
########### Passwords file serialization functions ##############################
sub deserialize {
my $str = shift;
my $result = {};
(my $store_version, $str) = unpack('na*', $str);
# define closure to get next element
my $get_next_elem_sub;
if ($store_version == $STORE_VER_3) {
$get_next_elem_sub = sub {
(my $k, my $v, $str) = unpack('n/a* N/a* a*', $str);
return ($k, $v, 0);
};
} elsif($store_version == $STORE_VER_4) {
$get_next_elem_sub = sub {
(my $k, my $v, my $t, $str) = unpack('n/a* N/a* N a*', $str);
return ($k, $v, $t);
};
}
# loop to read all elements
if ($store_version == $STORE_VER_3 || $store_version == $STORE_VER_4) {
(my $elements, $str) = unpack('Na*', $str);
my $elements_read = 0;
while($str) {
my ($k, $v, $t) = $get_next_elem_sub->();
if (defined $k) {
$elements_read++;
push @{$result->{$k}}, [$v, $t];
}
}
if ($elements != $elements_read) {
die "File is corrupted";
}
} else {
die "File format is not supported";
}
foreach my $key(keys %$result) {
$result->{$key} = [sort{$a->[1] <=> $b->[1]}(@{$result->{$key}})];
}
return $result;
}
sub serialize {
my $data = shift;
my $elements_count = 0;
my $body = '';
keys %$data;
while (my ($k,$vals_arr) = each %$data) {
foreach my $val (@$vals_arr) {
my ($v,$t) = @$val;
$t ||= int(time);
if ($DEF_STORE_VERSION == $STORE_VER_3) {
$body .= pack('n/a* N/a*', $k, $v);
} elsif ($DEF_STORE_VERSION == $STORE_VER_4) {
$body .= pack('n/a* N/a* N', $k, $v, $t);
}
$elements_count++;
}
}
my $header = pack('n', $DEF_STORE_VERSION);
$header .= pack('N', $elements_count);
return $header.$body;
}
########### Data load functions ##############################
sub load_data {
my $cypher = shift;
my $filename = shift;
unless(-f $filename) {
return {};
}
my $data = read_file($filename, { binmode => ':raw' });
$data = decrypt($cypher,$data);
return deserialize($data);
}
sub store_data {
my $cypher = shift;
my $data = shift;
my $filename = shift;
my $ice = encrypt($cypher,serialize($data));
write_file($filename, {binmode => ':raw'}, $ice);
return 1;
}
############# Auto completion ###############################
sub autocomplete {
my $text = shift;
my $line = shift;
my $start = shift;
my $end = shift;
$line = trim($line);
my ($cmd,@args) = split(/\s+/, $line);
$cmd ||= '';
push @args, $text if $text eq '';
# print "ac: $text,$line\t$cmd,'".scalar(@args)."'\n";
if (@args) {
if ($cmd =~ /^(search|get|history|del|rm|put|help)$/) {
return undef if @args > 1;
return $term->completion_matches($text,\&keyword);
}
} else {
my @all_commands = qw(put get history search del help);
return grep { /^\Q$text/ } (sort @all_commands);
}
return undef;
}
{
my @words = ();
my $i;
sub keyword {
my ($text, $state) = @_;
# return unless $text;
if($state) {
$i++;
} else { # first call
$i = 0;
@words = sort keys %$data;
}
for (; $i < scalar(@words); $i++) {
return $words[$i] if $words[$i] =~ /^\Q$text/;
};
return undef;
}
};
sub trim {
my $s = shift;
$s =~ s/^\s+|\s+$//g;
return $s;
}
1;
=pod
=head1 NAME
=over
Cypher - command line cypher tool to work with encrypted key-value storage. Can be used to encrypt and decrypt whole files
=back
=head1 SYNOPSIS
=over
./cypher [-ed] I<filename>
By default goes into Secure Storage Mode - loads encrypted file and provides interface to manipulate it. See L</USER COMMANDS>. If -e or -d options provided, cypher works as encrypter or decrypter accordingly, dumps its result to STDOUT and exits immediately.
=back
=head1 ARGUMENTS
=over
=item -e I<filename>
Read unencrypted I<filename> and dump it encrypted to STDOUT
=back
=over
=item -d I<filename>
Read encrypted I<filename> and dump it decrypted to STDOUT
=back
=head1 USER COMMANDS
Following commands available in Secure Storage Mode:
=over
=item put KEY VAL
Puts a pair KEY:VAL into storage
=back
=over
=item get REGEXP
Dumps all the data for keys matching perl regexp /^REGEXP\$/
=back
=over
=item history KEY
Dumps history of changes of the given KEY
=back
=over
=item search REGEXP
Searches and shows all the keys, matching the provided REGEXP
=back
=over
=item del|rm KEY
Deletes key KEY from storage.
=back
=head1 STORAGE MODE FEATURES
You can use <TAB> for autocompletion for commands and data keys
=cut