I have been battling this infection I got that encrypts my files in 512 byte chunks with a friend. We have managed to find the Decryption function we think in IDA (the code is heavily obfuscated) from a user who paid for the decrypter. Below is the C dump of the encryption function:
int __stdcall sub_40C78E(int a1, int a2, int a3, int a4)
{
int result;
char v5;
int v6;
int v7;
int v8;
v7 = a1;
v6 = a2;
v5 = 0;
result = 0;
if ( a2 )
{
v8 = a3;
do
{
LOBYTE(v8) = v5 + v8;
*(_BYTE *)v7 ^= v8;
v5 = *(_BYTE *)v7++;
v8 = __ROL__(a4 + v8, 8);
--v6;
}
while ( v6 );
result = v8;
}
return result;
}
My friend tried to simplify it or make sense of it and this is what he came up with:
int __stdcall sub_40C78E((_BYTE *)buffer, int nonce1, int nonce2)
{
char v5;
int n;
int v8;
n = 0x400; // It is a little bit confusing, because the length of block is 0x200 (rest of buffer is filled by 0).
// Only first 0x200 bytes are saved to a file for block CT0A.
v5 = 0;
v8 = nonce1;
do
{
LOBYTE(v8) = v5 + LOBYTE(v8);
*buffer ^= LOBYTE(v8);
v5 = *buffer;
buffer++;
v8 = __ROL__(nonce2 + v8, 8);
--n;
}
while ( n );
return v8;
}
Where nonce1 and nonce2 is suppose to represent some kind of key.
What we have found with this infection is that if you XOR the first byte of the cipher text with the plain text, you get a key byte you can use to get the first byte of every file back. Which makes sense with this function because the first time in the loop the key is added to 0, which means it is simply the key. But then this guy used some type of weird CFB type xor encryption where it uses the previous xor'ed byte with the key next.
I just don't quite understand the function and I was hoping somewhere here could perhaps simplify it more for me or explain it. I also know C#, and VB if anyone would know how to explain the function in these languages.