This repository has been archived by the owner on Apr 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
bashrc_functions_crypt_external
477 lines (442 loc) · 19.2 KB
/
bashrc_functions_crypt_external
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
#!/bin/bash
#
# Copyright (C) 2016-2019, Andrew Kroshko, all rights reserved.
#
# Author: Andrew Kroshko
# Maintainer: Andrew Kroshko <akroshko.public+devel@gmail.com>
# Created: Tue May 25, 2016
# Version: 20191209
# URL: https://github.com/akroshko/crypt-profiles
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.
################################################################################
# functions useful for external use without all the other crypt_profiles functionality
ssh-key-info () {
# get information on an SSH key
ssh-keygen -lf "$1"
}
kill-freespace-random-file () {
# wipe freespace by filling a random file until it stops working
# this function runs faster than the hard disk itself on modern machines
# TODO: fill reserved space as well, this one works as normal user so will not do that
# TODO: add guideline how long this takes on my machines as a guideline
# TODO: add a helpfile
#
# References
# http://serverfault.com/questions/6440/is-there-an-alternative-to-dev-urandom
# time openssl enc -aes-256-ctr -pass pass:"$(dd if=/dev/urandom bs=128 count=1 2>/dev/null | base64)" -nosalt < /dev/zero | pv -pterb > ~/randomfile.bin
local ROOTPATH=$(readlink -f -- "$1")
if [[ -z "$ROOTPATH" || ! -d "$ROOTPATH" ]]; then
echo "Must specify valid directory corresponding to device to kill space on!!!" 1>&2
return 1
fi
# check if I can run a command with elevated priveleges
# TODO:: not sure mbuffer is necessary
# time openssl enc -aes-256-ctr -pass pass:"$(dd if=/dev/urandom bs=128 count=1 2>/dev/null | base64)" -nosalt < /dev/zero | mbuffer -D 16384M -m 2048M -s 64k -A /bin/false -f -o "$ROOTPATH"/randomfile.bin
# TODO: it fluxuates, not sure why this is?
# TODO: run filling as root, but optionally don't use root and/or su to current user
# TODO: consider possibility of a small file too
# use $USERNAME to ensure that things are good
if (( $(cat /proc/meminfo | grep --color=never MemTotal | awk '{print $2}') < "4194304" )); then
local BUFSIZE=512M
else
local BUFSIZE=2048M
fi
echo "Space left on path where randomfile.bin will be" 1>&2
df -Ph . | tail -1 | awk '{print $4}'
echo "Using buffer size $BUFSIZE!" 1>&2
sudo true || { echo 'Failed to sudo!' 1>&2; return 1; }
# TOD:O move down? get rid of sudo?
[[ -e "$ROOTPATH"/randomfile.bin ]] && sudo rm "$ROOTPATH"/randomfile.bin
sudo -i su root <<EOF
time openssl enc -aes-256-ctr -pass pass:"$(dd if=/dev/urandom bs=128 count=1 2>/dev/null | base64)" -nosalt < /dev/zero | mbuffer -m "$BUFSIZE" -s 64k -A /bin/false -f -o "$ROOTPATH"/randomfile.bin
sync
sleep 60
sync
sleep 60
echo "Size of written file:"
du -sh "$ROOTPATH"/randomfile.bin
du -shb "$ROOTPATH"/randomfile.bin
rm "$ROOTPATH"/randomfile.bin
EOF
}
kill-drive-random-data () {
# TODO: ask if we are sure and add some better checks
# use openssl to write random data to disks, much faster than /dev/urandom and appropriate for this
local USAGE="Usage: kill-drive-random-data <<device name>>
<<device name>> device to fill with random data"
if [[ ! -z "$1" && -b "$1" ]]; then
time {
openssl enc -aes-256-ctr -pass pass:"$(dd if=/dev/urandom bs=128 count=1 2>/dev/null | base64)" -nosalt < /dev/zero | pv -pterb | sudo dd of="$1" bs=64k
}
else
yell "File $1 is non-existent or not a block device!!!"
echo "$USAGE" 1>&2
fi
}
write-random-data () {
# use openssl to write random data, useful for generating files for benchmarking and tests
local USAGE="Usage: write-random-data <<size in gb>> <<output file>>
<<size in gb>> number of gigs of random data to generate
<<output filename>> filename to write"
if [[ ! -z $1 && ! -z $2 ]]; then
time openssl enc -aes-256-ctr -pass pass:"$(dd if=/dev/urandom bs=128 count=1 2>/dev/null | base64)" -nosalt < /dev/zero | pv -pterb --size "$1"g --stop-at-size > "$2"
else
echo "$USAGE" 1>&2
fi
}
write-random-data-stdout () {
# use openssl to write random data to stdout, useful for filling
# pipes to generate files for benchmarking and tests
# TODO: write usage to stderr, for everything
# XXXX: untested
local USAGE="Usage: write-random-data <<size in gb>>
<<size in gb>> number of gigs of random data to generate"
# TODO: probably a better way to do this
if [[ ! -z $1 ]]; then
time openssl enc -aes-256-ctr -pass pass:"$(dd if=/dev/urandom bs=128 count=1 2>/dev/null | base64)" -nosalt < /dev/zero | pv -pterb --size "$1"g --stop-at-size
else
echo "$USAGE" 1>&2
fi
}
# TODO: decide whether elevated privilages are necessary for these commands
kill-file () {
# XXXX: dangerous functions for convenience in batch mode
if [[ -f "$1" ]]; then
if [[ -h "$1" ]]; then
yell "Cannot kill-file symlink $1!!!"
else
msg "Killing $1 with kill-file"
shred --iterations=3 --remove "$1"
fi
else
# TODO: warn if not file
warn "File $1 is not a file or does not exist, and is not cleared!"
fi
}
kill-recursive () {
# Xxxx: dangerous functions for convenience in batch mode
if [[ -d "$1" ]]; then
if [[ -h "$1" ]]; then
yell "Cannot kill-recursive symlink $1!!!"
else
msg "Unlinking all symlinks in directory $1 with kill-recursive"
find "$1" -type l -printf "Unlinking: %p\n" -delete
msg "Clearing files in directory $1 with kill-recursive"
find "$1" -type f -printf "Shredding: %p\n" -exec shred --iterations=3 --remove {} \;
msg "Clearing $1 and empty directories with kill-recursive"
find "$1" -depth -type d -printf "Removing: %p\n" -exec rmdir {} \;
fi
else
warn "Directory $1 does not exist and not deleted!"
fi
}
kill-file-contents () {
# XXXX: dangerous functions for convenience in batch mode
kill-file "$1"
touch "$1"
}
kill-directory-contents () {
# XXXX: dangerous functions for convenience in batch mode
# TODO: find way to kill directories too for some uses, but without going haywire
# XXXX: recursive, very dangerous!!!
# TODO: test if empty and give warning
# TODO: make sure no symlinks in directory
if [[ -d "$1" ]]; then
if [[ -h "$1" ]];then
yell "Cannot kill-directory-conents symlink $1!!!"
else
# safer command
# https://stackoverflow.com/questions/20456666/bash-checking-if-folder-has-contents
if find "$1/" -mindepth 1 -print -quit 2>/dev/null | grep --color=never -q .; then
msg "Unlinking all symlinks of depth 1 in directory $1 with kill-directory-contents"
find "$1/" -mindepth 1 -type l -printf "Unlinking: %p\n" -delete
msg "Clearing directory contents $1 with kill-directory-contents"
find "$1/" -mindepth 1 -type f -printf "Shredding: %p\n" -exec shred --iterations=3 --remove {} \;
else
warn "Directory $1 empty and not cleared!"
fi
fi
else
warn "Directory $1 does not exist and not cleared!"
fi
}
# TODO: give warning message, not quite same as above
kill-directory-files () {
# XXXX: dangerous functions for convenience in batch mode
# TODO: find way to kill directories too for some uses, but without going haywire
# XXXX: recursive, very dangerous!!!
if [[ -d "$1" ]]; then
if [[ -h "$1" ]];then
yell "Cannot kill-directory-files symlink $1!!!"
else
msg "Unlinking all symlinks of depth 1 in directory $1 with kill-directory-files"
find "$1/" -mindepth 1 -type l -printf "Unlinking: %p\n" -delete
msg "Clearing directory files $1 with kill-directory-files"
find "$1/" -mindepth 1 -maxdepth 1 -type f -printf "Shredding: %p\n" -exec shred --iterations=3 --remove {} \;
fi
fi
}
kill-directory-contents-preserve-structure () {
# XXXX: dangerous functions for convenience in batch mode
if [[ -d "$1" ]]; then
if [[ -h "$1" ]];then
yell "Cannot kill-directory-contents-preserve-structure symlink $1!!!"
else
# TODO: does this go into dot directories
msg "Unlinking all symlinks of in directory $1 with kill-directory-contents-preserve-structure"
find "$1/" -mindepth 1 -type l -printf "Unlinking: %p\n" -delete
msg "Clearing directory contents in $1 with kill-directory-contents-preserve-structure"
find "$1/" -type f -printf "Shredding: %p\n" -exec shred --iterations=3 --remove {} \;
fi
fi
}
kill-find-matching () {
# XXXX: dangerous functions for convenience in batch mode
if [[ -d "$1" ]]; then
# XXXX: double find a few milliseconds slower but gives good error messages
if find "$1" -name "$2" | grep --color=never '.' >/dev/null; then
if [[ -h "$1" ]];then
yell "Cannot kill-find-matching symlink $1!!!"
else
# TODO: add more safeties against shredding linked or symlinked files?
find "$1" -type f -name "$2" -printf "Shredding: %p\n" -exec shred --iterations=3 --remove {} \;
fi
else
warn "Directory $1 exists but nothing matching $2 to be cleared!"
fi
else
warn "Directory $1 does not exist so nothing matching $2 not cleared!"
fi
}
kill-matching-line () {
# XXXX: dangerous functions for convenience in batch mode
[[ -f "$1" ]] && sed -i "/$1/d" "$2"
}
mv-directory-contents-to-tmp-shred () {
if [[ -d "$1" ]]; then
local THESTEMNAME="$2"
local THEDIR_CONTENTS=$(find "$1" -mindepth 1 -maxdepth 1)
if [[ $(wc -l <<< "$THEDIR_CONTENTS") -gt "0" ]]; then
local THEBASENAME=$(basename "$1")
if [[ -n "$THESTEMNAME" ]]; then
local SHREDDIR="$HOME/tmp/tmp-shred/$(date-time-stamp)-$THESTEMNAME-$THEBASENAME"
else
local SHREDDIR="$HOME/tmp/tmp-shred/$(date-time-stamp)-$THEBASENAME"
fi
mv "$1" "$SHREDDIR"
# recreate directory
mkdir -p "$1"
msg "Move of contents of $1 to $SHREDDIR successful!"
else
warn "Directory $1 appears to be empty and contents not moved to $HOME/tmp/tmp-shred!"
fi
else
warn "Directory $1 does not exist and contents cannot be moved to $HOME/tmp/tmp-shred!"
fi
}
super-password () {
# generate a 30 character random password for many uses
# XXXX: may not actually be a super password generator, needs to be security audited
# TODO: decide whether to use GPG or openssl for this
#
# References:
# http://www.commandlinefu.com/commands/view/8869/generate-a-random-password-30-characters-long
local RANDOMTIMES=$((($RANDOM % 113) + 151))
# XXXX: is this loop a good or bad thing?
for i in {1.."$RANDOMTIMES"}; do
gpg-batch --gen-random --armor 1 30 >/dev/null
done
gpg-batch --gen-random --armor 1 30
}
super-password-easy () {
# XXXX: 30 character only lowercase password that is slighly nicer
# for humans to enter on mobile devices, etc.
#
# large enough that it is as hard as a 128 bit key to brute force
#
# if mobile devices become nicer
local RANDOMTIMES=$((($RANDOM % 113) + 151))
# XXXX: is this loop a good or bad thing?
for i in {1.."$RANDOMTIMES"}; do
openssl rand -base64 128 >/dev/null
done
# http://www.commandlinefu.com/commands/view/722/generate-a-random-password-30-characters-long
openssl rand -base64 128 | grep -o -- '[a-z]' | head -n 30 | tr -d '\n'; echo
}
ssh-check-host-fingerprints () {
# get the sshd fingerprints of the current host
# TODO: no color for now
echo "$HOSTNAME" 1>&2
if [[ -f /etc/ssh/ssh_host_ecdsa_key.pub ]]; then
h2 'ECDSA fingerprint'
awk '{print $2}' /etc/ssh/ssh_host_ecdsa_key.pub | base64 -d | md5sum | sed 's/../&:/g; s/: .*$//'
ssh-keygen -lf /etc/ssh/ssh_host_ecdsa_key.pub
h3 'ECDSA public key'
cat /etc/ssh/ssh_host_ecdsa_key.pub
fi
if [[ -f /etc/ssh/ssh_host_ed25519_key.pub ]]; then
h2 'ED25519 fingerprint'
# https://superuser.com/questions/1088165/get-ssh-key-fingerprint-in-old-hex-format-on-new-version-of-openssh
awk '{print $2}' /etc/ssh/ssh_host_ed25519_key.pub | base64 -d | md5sum | sed 's/../&:/g; s/: .*$//'
# new style for those that do not have it
awk '{print $2}' /etc/ssh/ssh_host_ed25519_key.pub | base64 -d | sha256sum -b | awk '{print $1}' | xxd -r -p | base64
h3 'ED25519 public key'
cat /etc/ssh/ssh_host_ed25519_key.pub
fi
if [[ -f /etc/ssh/ssh_host_rsa_key.pub ]]; then
h2 'RSA fingerprint'
awk '{print $2}' /etc/ssh/ssh_host_rsa_key.pub | base64 -d | md5sum | sed 's/../&:/g; s/: .*$//'
ssh-keygen -lf /etc/ssh/ssh_host_rsa_key.pub
h3 'RSA public key'
cat /etc/ssh/ssh_host_rsa_key.pub
fi
}
ssh-user-fingerprint () {
echo "$HOSTNAME" 1>&2
if [[ -f "$HOME/.ssh/id_ecdsa.pub" ]]; then
h2 'ECDSA fingerprint'
awk '{print $2}' "$HOME/.ssh/id_ecdsa.pub" | base64 -d | md5sum | sed 's/../&:/g; s/: .*$//'
ssh-keygen -lf "$HOME/.ssh/id_ecdsa.pub"
# h3 'ECDSA public key'
# cat /etc/ssh/ssh_host_ecdsa_key.pub
fi
if [[ -f "$HOME/.ssh/id_ed25519.pub" ]]; then
h2 'ED25519 fingerprint'
# https://superuser.com/questions/1088165/get-ssh-key-fingerprint-in-old-hex-format-on-new-version-of-openssh
awk '{print $2}' "$HOME/.ssh/id_ed25519.pub" | base64 -d | md5sum | sed 's/../&:/g; s/: .*$//'
# new style for those that do not have it
awk '{print $2}' "$HOME/.ssh/id_ed25519.pub" | base64 -d | sha256sum -b | awk '{print $1}' | xxd -r -p | base64
# h3 'ED25519 public key'
# cat /etc/ssh/ssh_host_ed25519_key.pub
fi
if [[ -f "$HOME/.ssh/id_rsa.pub" ]]; then
h2 'RSA fingerprint'
awk '{print $2}' "$HOME/.ssh/id_rsa.pub" | base64 -d | md5sum | sed 's/../&:/g; s/: .*$//'
ssh-keygen -lf "$HOME/.ssh/id_rsa.pub"
# h3 'RSA public key'
# cat /etc/ssh/ssh_host_rsa_key.pub
fi
}
crypt-backup-extract () {
# just decompress .tzo for now, allow selection
# TODO: decompress appropriate compression file type
# XXXX: do not use -v on tar, too slow
gpg-batch --decrypt "$1" | mbuffer -q -m 2048M | lzop -d | mbuffer -m 2048M | tar --extract --file -
}
crypt-device-image-to-here () {
# image a disk parition here
# good for quick and dirty backups of disks on secondary machines before major modifications
# TODO: will need to create and test a restore thing
# mbuffer limtted to easily fit on machines with 1G Ram
# TODO: change to lzop, I find it better for realtime compression on my machines
sudo dd if=/dev/"$1" bs=64k conv=noerror,sync | lz4 | mbuffer -m 256M | gpg-batch --compress-algo none --cipher-algo AES256 --recipient "$CRYPTGPGUSER" --output - --encrypt - | mbuffer -q -m 256M -s 64k -o ./"$HOSTNAME"-"$1"--$(date +%Y%m%d%H%M%S).lz4.gpg
}
ssh-controlmaster-kill-all () {
\rm "$HOME/.ssh/controlmasters/"*
}
ssh-controlmaster-kill-select () {
local CONTROLMASTERS=$(\ls --color=never -1 "$HOME/.ssh/controlmasters/")
if [[ -n "$CONTROLMASTERS" ]]; then
IFS=$'\n'
local count=1
for item in $CONTROLMASTERS; do
echo "$count) $item"
local count=$(( $count + 1 ))
done
while read -r -t 0;do read -r; done
read -n 1 -s -r -p "Enter the controlmaster to kill: " CONTROLMASTERSELECT
echo ""
ssh -O exit $((sed -n "$CONTROLMASTERSELECT"p | sed 's/:.*//g') <<< "$CONTROLMASTERS")
else
echo "$HOME/.ssh/controlmasters is empty!" 1>&2
fi
}
ssh-kill-offending-key () {
# TODO: backup?
# TODO: do this automatically if there is already a good key?
if [[ -n "$1" && "$1" =~ known_hosts ]]; then
# TODO: how to split?
local HOSTSFILE=$(cut -d':' -f1 <<< "$1")
local LINENO=$(cut -d':' -f2 <<< "$1")
if [[ -n "$HOSTSFILE" && -n "$LINENO" ]];then
# delete appropriate line
echo "Deleting $LINENO in $HOSTSFILE!!!" 1>&2
sed -i -e "${LINENO}d" "$HOSTSFILE"
fi
else
warn "Must specific a known_hosts file and line number!"
fi
}
# may want a standalone shell script
# TODO: non-functional
crypt-password-database-retrieve-primary () {
# TODO: change password database name
local CRYPT_PASSWORD_DATABASE=
# TODO: base64 decode
local URL="$1"
# load appropriate org-file heading into memory
sed -n '/^\* Website Logins$/,/^\*/p' <<< $(gpg2 --no-verbose --quiet --batch --yes --decrypt "$CRYPT_PASSWORD_DATABASE" 2>/dev/null)
}
# TODO: non-functional
crypt-password-database-retrieve-second () {
local CRYPT_PASSWORD_DATABASE=
# load org-file into memory
}
# TODO: non-functional
crypt-password-database-retrieve-third () {
local CRYPT_PASSWORD_DATABASE=
# load org-file into memory
}
container-create-encrypted () {
# create an encrypted container formatted to ext4
# TODO: error checking!!!
sudo true || { echo 'Failed to sudo!' 1>&2; return 1; }
local USAGE="Usage: container-create-encrypted <<size>> <<container filename>>"
if [[ -z "$3" ]]; then
echo "$USAGE" 1>&2
return 1
fi
local THESIZE="$1"
local THESIZEBITS=$(convert-size-to-bytes-1024 "$1")
local THENUMBERBLOCKS=$(( "$THESIZEBITS" / 65535 ))
local THEFNAME="$2"
# TODO: get rid of any extension
local THECONTAINERNAME=$(basename -- "$2")
if [[ -e "$THEFNAME" ]]; then
echo "Filename $THEFNAME cannot exist!" 1>&2
echo "$USAGE" 1>&2
return 1
fi
if [[ ! $(readlink -f -- "$THEFNAME") =~ $HOME ]];then
echo "Filename must be created in home directory!" 1>&2
return 1
fi
# TODO: check that size is valid
# TODO: make sure file in second level of home directory, not device
# create file
fallocate -l "$THESIZE" "$THEFNAME"
# TODO: do I need count in files below
# fill with zeros first
dd if=/dev/zero of="$THEFNAME" bs=64k count="$THENUMBERBLOCKS"
# fill with random data
dd if=/dev/urandom of="$THEFNAME" bs=64k count="$THENUMBERBLOCKS"
sudo cryptsetup -y luksFormat "$THEFNAME"
sudo cryptsetup luksDump "$THEFNAME"
# first run of container and format with ext4
# TODO: used to use ext2, but unless there is a specific reason, just going with ext4
sudo cryptsetup luksOpen "$THEFNAME" "$THECONTAINERNAME"
sudo mkfs.ext4 /dev/mapper/"$THECONTAINERNAME"
sudo cryptsetup luksClose "$THECONTAINERNAME"
}