Skip to content

Commit 9787399

Browse files
committed
feat: cryptography-easy cbc
1 parent fbc209b commit 9787399

20 files changed

+228
-0
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
## Cryptography - Easy CBC
2+
3+
In this challenge, we were given an image that has been encrypted.
4+
5+
![res](assets/out.bmp)
6+
7+
This image has been encrypted with the following [Python script](assets/challenge.py) that implemented AES-CBC encryption.
8+
9+
> AES-CBC (Advanced Encryption Standard - Cipher Block Chaining) is a method of encrypting data to keep it secure. It works by dividing the data into blocks, and then encrypting each block with a secret key using the AES algorithm. The blocks are linked together in a chain, with each block XORed with the previous block before being encrypted. This helps to make it harder for patterns in the plaintext to be seen in the ciphertext.
10+
11+
The code snippet below.
12+
13+
```python
14+
# !pip install certifi==2021.10.8
15+
# !pip install cffi==1.15.0
16+
# !pip install cryptography==36.0.2
17+
# !pip install Pillow==9.0.1
18+
# !pip install wincertstore==0.2
19+
import os
20+
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
21+
from cryptography.hazmat.backends import default_backend
22+
import PIL.Image as Image
23+
24+
class CBCEncryption:
25+
def __init__(self, key, iv):
26+
self.cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
27+
self.encryptor = self.cipher.encryptor()
28+
29+
def encrypt(self, image):
30+
return self.encryptor.update(image)
31+
32+
def finalize_encrypt(self):
33+
return self.encryptor.finalize()
34+
35+
def EncryptImage(encryption, image, output):
36+
output = output + '.bmp'
37+
image = Image.open(image)
38+
image.save('temp.bmp')
39+
with open('temp.bmp', 'rb') as reader:
40+
with open(output, 'wb') as writer:
41+
image_data = reader.read()
42+
header, body = image_data[:54], image_data[54:]
43+
body += b'\x35' * (16 -(len(body) % 16))
44+
body = encryption.encrypt(body) + encryption.finalize_encrypt()
45+
writer.write(header + body)
46+
writer.close()
47+
reader.close()
48+
os.remove('temp.bmp')
49+
50+
def main():
51+
key = b'JOINTSCTF2023'
52+
key = key.ljust(32, b'\x35')
53+
54+
iv = key[:16]
55+
iv = bytearray(iv)
56+
for i in range(16):
57+
iv[i] = iv[i] ^ 0x35
58+
iv = bytes(iv)
59+
60+
AesCbc = CBCEncryption(key, iv)
61+
EncryptImage(encryption=AesCbc, image='flag.jpg', output='out')
62+
63+
if __name__ == '__main__':
64+
main()
65+
```
66+
67+
It defines a class called `CBCEncryption` to handle encryption using the provided `key` and initialization `vector (IV)`, and a function called `EncryptImage` to encrypt the image file. The `key` and `iv` values are manipulated to ensure they are of the correct length. The main function initializes the encryption class with a `key` and `IV`, and then calls the `EncryptImage` function to encrypt an image file called "flag.jpg" and save the output as "out.bmp".
68+
69+
The `EncryptImage` function opens the input image file and saves a temporary copy of it as a BMP file. It then reads the image data from the temporary file, splits the data into a header section and a body section, and pads the body section with additional data to make it a multiple of 16 bytes long. It then uses the provided `CBCEncryption` object to encrypt the padded body section, and saves the encrypted data along with the original header section as the output BMP file.
70+
71+
To decrypt the image, we should do the reverse engineering using the same `key` and `IV` that were used to encrypt the data, and perform the decryption process in the reverse order of the encryption process. Beside that, it also important to unpad the decrypted data to remove any additional bytes that were added during the padding process. The decryptor script can be seen in [here](decrypt.py).
72+
73+
The code snippet below.
74+
75+
```python
76+
import os
77+
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
78+
from cryptography.hazmat.backends import default_backend
79+
import PIL.Image as Image
80+
81+
# define a class for CBC decryption
82+
class CBCDecryption:
83+
# initialization function to set up the cipher and decryptor
84+
def __init__(self, key, iv):
85+
self.cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
86+
self.decryptor = self.cipher.decryptor()
87+
88+
# function to decrypt ciphertext
89+
def decrypt(self, ciphertext):
90+
return self.decryptor.update(ciphertext)
91+
92+
# function to decrypt an image using a CBC decryption object
93+
def DecryptImage(decryption, image, output):
94+
# open the input image file and create the output file
95+
with open(image, 'rb') as reader:
96+
with open(output, 'wb') as writer:
97+
# read the image data and separate the header and body
98+
image_data = reader.read()
99+
header, body = image_data[:54], image_data[54:]
100+
# decrypt the body of the image
101+
body = decryption.decrypt(body)
102+
# write the decrypted image data to the output file
103+
writer.write(header + body)
104+
writer.close()
105+
reader.close()
106+
107+
# main function to set up decryption object and decrypt image
108+
def main():
109+
# set up the key and IV for decryption
110+
key = b'JOINTSCTF2023'
111+
key = key.ljust(32, b'\x35')
112+
iv = key[:16]
113+
iv = bytearray(iv)
114+
for i in range(16):
115+
iv[i] = iv[i] ^ 0x35
116+
iv = bytes(iv)
117+
118+
# create a CBC decryption object with the key and IV
119+
AesCbc = CBCDecryption(key, iv)
120+
# decrypt the image using the decryption object
121+
DecryptImage(decryption=AesCbc, image='out.bmp', output='flag_decrypted.jpg')
122+
123+
if __name__ == '__main__':
124+
main()
125+
```
126+
127+
Run it, and we got the flag.
128+
129+
![res](flag_decrypted.jpg)
130+
131+
</br>
132+
133+
So, this is the flag.
134+
135+
```
136+
JCTF2023{n4rim0_in9_pAndum}
137+
```
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Description
2+
Whoa, do you know that you can encrypt an image and make it like nonsense? anyway, recently I heard about this AES-CBC encryption and I try to use it to encrypt an image.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# !pip install certifi==2021.10.8
2+
# !pip install cffi==1.15.0
3+
# !pip install cryptography==36.0.2
4+
# !pip install Pillow==9.0.1
5+
# !pip install wincertstore==0.2
6+
import os
7+
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
8+
from cryptography.hazmat.backends import default_backend
9+
import PIL.Image as Image
10+
11+
class CBCEncryption:
12+
def __init__(self, key, iv):
13+
self.cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
14+
self.encryptor = self.cipher.encryptor()
15+
16+
def encrypt(self, image):
17+
return self.encryptor.update(image)
18+
19+
def finalize_encrypt(self):
20+
return self.encryptor.finalize()
21+
22+
def EncryptImage(encryption, image, output):
23+
output = output + '.bmp'
24+
image = Image.open(image)
25+
image.save('temp.bmp')
26+
with open('temp.bmp', 'rb') as reader:
27+
with open(output, 'wb') as writer:
28+
image_data = reader.read()
29+
header, body = image_data[:54], image_data[54:]
30+
body += b'\x35' * (16 -(len(body) % 16))
31+
body = encryption.encrypt(body) + encryption.finalize_encrypt()
32+
writer.write(header + body)
33+
writer.close()
34+
reader.close()
35+
os.remove('temp.bmp')
36+
37+
def main():
38+
key = b'JOINTSCTF2023'
39+
key = key.ljust(32, b'\x35')
40+
41+
iv = key[:16]
42+
iv = bytearray(iv)
43+
for i in range(16):
44+
iv[i] = iv[i] ^ 0x35
45+
iv = bytes(iv)
46+
47+
AesCbc = CBCEncryption(key, iv)
48+
EncryptImage(encryption=AesCbc, image='flag.jpg', output='out')
49+
50+
if __name__ == '__main__':
51+
main()
Binary file not shown.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import os
2+
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
3+
from cryptography.hazmat.backends import default_backend
4+
import PIL.Image as Image
5+
6+
class CBCDecryption:
7+
def __init__(self, key, iv):
8+
self.cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
9+
self.decryptor = self.cipher.decryptor()
10+
11+
def decrypt(self, ciphertext):
12+
return self.decryptor.update(ciphertext)
13+
14+
def DecryptImage(decryption, image, output):
15+
with open(image, 'rb') as reader:
16+
with open(output, 'wb') as writer:
17+
image_data = reader.read()
18+
header, body = image_data[:54], image_data[54:]
19+
body = decryption.decrypt(body)
20+
writer.write(header + body)
21+
writer.close()
22+
reader.close()
23+
24+
def main():
25+
key = b'JOINTSCTF2023'
26+
key = key.ljust(32, b'\x35')
27+
28+
iv = key[:16]
29+
iv = bytearray(iv)
30+
for i in range(16):
31+
iv[i] = iv[i] ^ 0x35
32+
iv = bytes(iv)
33+
34+
AesCbc = CBCDecryption(key, iv)
35+
DecryptImage(decryption=AesCbc, image='/Kompetisi/JCTF2023/crypto1/out.bmp', output='flag_decrypted.jpg')
36+
37+
if __name__ == '__main__':
38+
main()
Loading

WU_ARACTF_ctf masbro.pdf

9.81 MB
Binary file not shown.

Write Up.docx.pdf

24.9 KB
Binary file not shown.

Write Up.pdf

174 KB
Binary file not shown.

Write-UpMatryochska.pdf

1.66 MB
Binary file not shown.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy