Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leftover padding in decrypted files #3

Closed
L-Leite opened this issue Feb 7, 2019 · 5 comments
Closed

Leftover padding in decrypted files #3

L-Leite opened this issue Feb 7, 2019 · 5 comments
Labels
bug Something isn't working

Comments

@L-Leite
Copy link
Owner

L-Leite commented Feb 7, 2019

UnCSO2 is leaving extra bytes in the end of the decrypted files due to padding.

Decrypted file example maplist.txt:
decrypted_crap
The red rectangles highlight the padding left into the files.

Counter-Strike Source's maplist.txt:
orig_good

The game decrypts the files properly (it uses OpenSSL), so I'm guessing it's Crypto++'s fault or some missing option when decrypting the files.

Porting UnCSO2 to use LibreSSL will probably fix this since it's a closer method of decrypting to the game.

@GEEKiDoS
Copy link

GEEKiDoS commented Feb 7, 2019

I did't get padding bytes using System.Security.Cryptography of .NET Framework with those code

var rijndaelManaged = new RijndaelManaged();

rijndaelManaged.Mode = CipherMode.CBC;

rijndaelManaged.IV = s_EmptyIv16;
rijndaelManaged.Key = GeneratePkgListKey(flag, fileName);

var decryptor = rijndaelManaged.CreateDecryptor();

var outBuffer = decryptor.TransformFinalBlock(inBuffer, 0, inBuffer.Length);

I guess you just misconfigured crypto options.

@GEEKiDoS
Copy link

GEEKiDoS commented Feb 7, 2019

Or your could remove the extra padding by hand:

uint32_t GetRealBufferSize(uint8_t* pOutBuffer, size_t iBufferSize)
{
	uint8_t currentByte = 0;
	int paddingLength = 0;
	for (int i = iBufferSize - 16; i < iBufferSize; i++)
	{
		if (pOutBuffer[i] != currentByte)
		{
			currentByte = pOutBuffer[i];
			paddingLength = 1;
		}
		else
		{
			paddingLength++;
		}
	}

	return iBufferSize - paddingLength;
}

if ( !DecryptEncFile( targetFile, pBuffer, iBufferSize ) ) { /* ... */ }
iBufferSize = GetRealBufferSize(pBuffer, iBufferSize);

That's working.

@L-Leite
Copy link
Owner Author

L-Leite commented Feb 7, 2019

Wouldn't that not work if the padding bytes are different? What if a file for ends with ////////////////?

@GEEKiDoS
Copy link

GEEKiDoS commented Feb 8, 2019

Wouldn't that not work if the padding bytes are different? What if a file for ends with ////////////////?

The game is using PKCS7 padding mode, after some searching I find that all the padding byte will be the length of padding data, That function is not needed anymore.

iBufferSize -= pOutBuffer[iBufferSize - 1] ;

The padding modes I searched on internet: (DD is random data, after that is padding bytes, 05 is size of padding bytes)

ANSI X.923
 | DD DD DD DD DD DD DD DD | DD DD DD 00 00 00 00 05 | 

ISO 10126
 | DD DD DD DD DD DD DD DD | DD DD DD 95 81 28 A7 05 |

PKCS7
 | DD DD DD DD DD DD DD DD | DD DD DD 05 05 05 05 05 |

But the Zero mode is padding with 0 and ISO/IEC 7816-4 is using the 0x80 as mark of start, then padding with 0:

ISO/IEC 7816-4
 | DD DD DD DD DD DD DD DD | DD DD DD 80 00 00 00 00 |

Zero padding
 | DD DD DD DD DD DD DD DD | DD DD DD 00 00 00 00 00 |

@L-Leite L-Leite changed the title Extra padding in decrypted files Leftover padding in decrypted files Feb 17, 2019
@L-Leite
Copy link
Owner Author

L-Leite commented Oct 3, 2019

Should be fixed in Release v2.0.4

@L-Leite L-Leite closed this as completed Oct 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants