7thSamurai / steganography

Simple C++ Image Steganography tool to encrypt and hide files insde images using Least-Significant-Bit encoding.
MIT License
995 stars 66 forks source link

Segfault error #7

Closed vaygr closed 5 months ago

vaygr commented 5 months ago

Trying to embed steganography binary itself into a 1.3M png image:

./steganography encode -i expansion.png -e steganography -o exp.png
Password: ...
* Image size: 1280x1024 pixels
* Encoding level: Low (Default)
* Max embed size: 639.25 KiB
* Embed size: 635.36 KiB
* Encrypted embed size: 635.36 KiB
* Generated CRC32 checksum
* Generated encryption key with PBKDF2-HMAC-SHA-256 (20000 rounds)
steganography: steganography/src/aes.cpp:91: void AES::cbc_encrypt(void*, std::size_t, void*): Assertion `size % block_len == 0' failed.
Aborted (core dumped)
7thSamurai commented 5 months ago

Hello,

First of all, sorry about the delay it took me to look at this. I haven't been very active on here as of late...

So, I was initially unable to duplicate your problem when attempting to embed the executable itself. This was apparently due to different executable sizes due to our different systems. I then instead tried embedding a null file of the same length as yours (635.36 KiB), generated using:

dd if=/dev/zero of=zeros.bin bs=1 count=650608 

This method produced the exact same error as the one that you had received. After some further investigation, the problem seemed to occur when the embed file size was a multiple of 16.

The source of the issue ended up being the conditional check in this block of code right here:

https://github.com/7thSamurai/steganography/blob/6ba5d96e4a3fd1768697239197df2dc058054b80/src/main.cpp#L54-L58

The bug being that the mod 16 check was being preformed on the unaltered size variable and not the padded_size variable with the +1. Therefore if the input file size was a multiple of 16, then the one byte would be added, making it no longer a multiple of 16, and then the mod check would wrongfully fail and no padding would be applied. Which would latter cause the assert in AES::cbc_encrypt function to be triggered.

This has been fixed in commit 67e11d3.

Thank you for noticing this, much appreciated! I'm surprised it hadn't cropped up earlier!