7 Jun 2024
An opportunity to play with practical cryptography and see how easy it is to encrypt all of a user’s files.
Preparation
-
Bring a USB stick to the lab to save your work
-
Use
rsync
to copy files from one directory hierarchy to another (hint:man rsync
may be helpful)
Procedure
Caution
|
Caution: real-world harms and ethical expectations
In this lab, you are going to write software that, although unsophisticated, could cause unrecoverable file loss. Like real ransomware, effects are reversible if you have good backups and/or if you have a working decryptor. However, untested backups should be assumed not to work, and decryptors don’t always work as they should! As a matter of ethical practice:
As in all aspects of your academic program, failure to behave in an ethical manner could have implications for your professional suitability and your ability to continue in the program. |
-
Log into Kali using the username
l33t
and the passwordopposable thumbs
. -
Copy files into your home directory:
-
the
frank
user’sMusic
directory -
the
sam
user’sPhotos
directory
-
-
Write a Python function that uses the
secrets
module to generate a secret $n$-bit key. Show the output of this function for several key sizes — you may findbinascii.hexlify
helpful for this purpose. -
Write a Python function that will encrypt the contents of a given file with a given key, using a cipher of your own choosing. You may find the
Cryptodome
module helpful; on our Python installation you may need to refer to this module asCryptodome.Cipher
rather thanCrypto.Cipher
. This function should return the ciphertext as bytes. Show this function’s output when applied to several (small) files. -
Use the Python
os.walk
function to inspect every file within a directory (searching recursively), outputting for each:-
the path used to reach the file
-
the first 16 B of the file (in hex format)
-
the first 16 B of the ciphertext of the file (in hex format), using a key passed into your function
-
-
Write a Python script that will generate a random key, saving it in a specified file path, then walk through a specified directory recursively, encrypting all files it finds in place (i.e., overwriting the originals). Demonstrate that this script works when executed against the
l33t
user’s home directory. -
Required for ENGI 9823, optional for ENGI 7420: Take the symmetric key that you used to encrypt the user’s data and encrypt it under another key known to you [1]. Write a decryptor that can decrypt the user’s files given their encrypted key. Demonstrate that it works and that the user gets their files back.