Commit 036c3b37 authored by ElenaSubbotina's avatar ElenaSubbotina

XlsFormatReader - decrypter ...

parent dddaeb3e
......@@ -55,7 +55,7 @@ int _tmain(int argc, _TCHAR* argv[])
std::wstring outputDir = FileSystem::Directory::GetFolderPath(dstPath);
std::wstring dstTempPath = FileSystem::Directory::CreateDirectoryWithUniqueName(outputDir);
hr = ConvertXls2Xlsx(srcFileName, dstTempPath, L"", L"C:\\Windows\\Fonts", NULL);
hr = ConvertXls2Xlsx(srcFileName, dstTempPath, L"password", L"C:\\Windows\\Fonts", NULL);
if (hr == S_OK)
{
......
......@@ -37,11 +37,11 @@
namespace CRYPT
{
RC4Decryptor::RC4Decryptor(CryptRC4Data & header, std::wstring password, int type) :
crypt (new RC4Crypt(header, password, type)),
type (Crypt::RC4)
RC4Decryptor::RC4Decryptor(CryptRC4Data & header, std::wstring _password, int _type) :
crypt(new RC4Crypt(header, _password, _type))
{
crypt_data = header;
crypt_data = header;
type = _type;
}
void RC4Decryptor::Decrypt(char* data, const size_t size, const unsigned long stream_pos)
......@@ -54,7 +54,7 @@ namespace CRYPT
return crypt->IsVerify();
}
bool RC4Decryptor::SetPassword(std::wstring password, int type)
bool RC4Decryptor::SetPassword(std::wstring password)
{
crypt.reset();
crypt = CryptPtr(new RC4Crypt(crypt_data, password, type));
......
......@@ -43,18 +43,16 @@ namespace CRYPT
RC4Decryptor(CryptRC4Data & header, std::wstring password, int type);
virtual void Decrypt(char* data, const size_t size, const unsigned long stream_pos);
virtual bool SetPassword(std::wstring password);
bool IsVerify();
bool SetPassword(std::wstring password, int type = 1);
virtual bool IsVerify();
private:
int type;
CryptPtr crypt;
Crypt::crypt_type type;
CryptRC4Data crypt_data;
};
typedef boost::shared_ptr<RC4Decryptor> RC4DecryptorPtr;
} // namespace CRYPT
......@@ -69,28 +69,27 @@ void FilePass::readFields(CFRecord& record)
bEnabled = true;
majorVer = *record.getCurData<unsigned short>();
rc4HeaderPtr = CRYPTO::RC4EncryptionHeaderPtr(new CRYPTO::RC4EncryptionHeader());
rc4HeaderPtr->bStandard = 0x0001 == majorVer ? true : false; // _S2dvT1xU_R3bOPwre4_.xls
rc4HeaderPtr->load (record);
cryptHeaderPtr = CRYPTO::RC4EncryptionHeaderPtr(new CRYPTO::RC4EncryptionHeader());
cryptHeaderPtr->bStandard = 0x0001 == majorVer ? true : false; // _S2dvT1xU_R3bOPwre4_.xls
if (rc4HeaderPtr->bStandard)
cryptHeaderPtr->load (record);
if (cryptHeaderPtr->bStandard)
{
record.getGlobalWorkbookInfo()->decryptor =
CRYPT::RC4DecryptorPtr(new CRYPT::RC4Decryptor(rc4HeaderPtr->crypt_data_rc4, record.getGlobalWorkbookInfo()->password, 2));
CRYPT::DecryptorPtr(new CRYPT::RC4Decryptor(cryptHeaderPtr->crypt_data_rc4, record.getGlobalWorkbookInfo()->password, 2));
}
else
{
//CRYPT::ECMADecryptor Decryptor;
//Decryptor.SetCryptData(rc4HeaderPtr->crypt_data_aes);
record.getGlobalWorkbookInfo()->decryptor =
CRYPT::DecryptorPtr(new CRYPT::ECMADecryptor());
CRYPT::ECMADecryptor *crypter = dynamic_cast<CRYPT::ECMADecryptor *>(record.getGlobalWorkbookInfo()->decryptor.get());
//if (Decryptor.SetPassword(L"VelvetSweatshop") == false)
//{
//}
// //record.getGlobalWorkbookInfo()->decryptor =
// //CRYPT::ECMADecryptor(new CRYPT::RC4Decryptor(rc4HeaderPtr->crypt_data_rc4, record.getGlobalWorkbookInfo()->password, 2));
crypter->SetCryptData(cryptHeaderPtr->crypt_data_aes);
crypter->SetPassword(record.getGlobalWorkbookInfo()->password);
}
}
......
......@@ -60,8 +60,7 @@ public:
XORObfuscation key;
_UINT16 majorVer;
CRYPTO::RC4EncryptionHeaderPtr rc4HeaderPtr;
CRYPTO::RC4CryptoAPIEncryptionHeader rc4CryptoAPIHeader;
CRYPTO::RC4EncryptionHeaderPtr cryptHeaderPtr;
};
} // namespace XLS
......
......@@ -49,13 +49,12 @@ public:
virtual void load(XLS::CFRecord& record);
EncryptionHeaderFlags Flags;
_INT32 AlgID;
_INT32 AlgIDHash;
_UINT32 KeySize;
_UINT32 ProviderType;
std::wstring CSPName;
EncryptionHeaderFlags Flags;
_INT32 AlgID;
_INT32 AlgIDHash;
_UINT32 KeySize;
_UINT32 ProviderType;
std::wstring CSPName;
};
} // namespace CRYPTO
......
......@@ -50,7 +50,6 @@ public:
virtual void load(XLS::CFRecord& record);
bool fCryptoAPI;
bool fDocProps;
bool fExternal;
......
......@@ -48,7 +48,6 @@ public:
virtual void load(XLS::CFRecord& record);
_UINT32 SaltSize;
struct SALT_TAG
{
......
......@@ -65,8 +65,9 @@ void RC4EncryptionHeader::load(XLS::CFRecord& record)
bool fExternal = GETBIT(flags, 3);
bool fAES = GETBIT(flags, 4);
record >> flags;
//EncryptionHeader
unsigned short Reserved3;
record >> Reserved3;
_UINT32 HeaderSize; record >> HeaderSize;
_UINT32 Flags; record >> Flags;
_UINT32 SizeExtra; record >> SizeExtra;
......@@ -80,19 +81,9 @@ void RC4EncryptionHeader::load(XLS::CFRecord& record)
int pos = record.getRdPtr();
int size = record.getDataSize();
std::vector<char> dataCSPName;
while(pos < size - 1)
{
char s;
record >> s; dataCSPName.push_back(s);
record >> s; dataCSPName.push_back(s);
if (dataCSPName[dataCSPName.size() - 1] == 0 && dataCSPName[dataCSPName.size() - 2] == 0)
{
break;
}
pos+=2;//unicode null-terminate string
}
std::wstring providerName;
record >> providerName;
//EncryptionVerifier
record >> crypt_data_aes.saltSize;
......@@ -134,7 +125,7 @@ void RC4EncryptionHeader::load(XLS::CFRecord& record)
pos = record.getRdPtr();
//------------------------------------------------------------------------------------------
crypt_data_aes.hashAlgorithm = CRYPT_METHOD::SHA1; //by AlgIDHash -> 0x0000 || 0x8004
crypt_data_aes.hashAlgorithm = CRYPT_METHOD::SHA1; //by AlgIDHash -> 0x0000(reserved ??) || 0x8004(sha1)
crypt_data_aes.spinCount = 50000;
switch(AlgID)
......@@ -162,15 +153,6 @@ void RC4EncryptionHeader::load(XLS::CFRecord& record)
case 0x0001: crypt_data_aes.cipherAlgorithm = CRYPT_METHOD::RC4; break;
case 0x0018: crypt_data_aes.cipherAlgorithm = CRYPT_METHOD::AES_ECB; break;
}
if (crypt_data_aes.cipherAlgorithm == CRYPT_METHOD::RC4)
{
bStandard = true;
memcpy(&crypt_data_rc4.Salt, crypt_data_aes.saltValue.c_str(), 16);
memcpy(&crypt_data_rc4.EncryptedVerifier, crypt_data_aes.encryptedVerifierValue.c_str(), 16);
memcpy(&crypt_data_rc4.EncryptedVerifierHash, crypt_data_aes.encryptedVerifierInput.c_str(), 16);
}
}
}
......
......@@ -77,7 +77,7 @@ public:
unsigned short CodePage;
CRYPT::RC4DecryptorPtr decryptor;
CRYPT::DecryptorPtr decryptor;
std::wstring password;
std::vector<std::wstring> sheets_state;
......
......@@ -151,37 +151,6 @@ BaseObjectPtr GlobalsSubstream::clone()
return BaseObjectPtr(new GlobalsSubstream(*this));
}
// class Parenthesis_Window1_Backup_HideObj_Date1904 : public ABNFParenthesis
// {
// public:
// BaseObjectPtr clone()
// {
// return BaseObjectPtr(new Parenthesis_OO_order_workaround_1(*this));
// }
//
// const bool loadContent(BinProcessor& proc)
// {
// if(!proc.mandatory<SXAddl_SXCCacheField_SXDCaption>())
// {
// return false;
// }
// proc.repeated<Continue_SxaddlSxString>(0, 0);
// return true;
// };
// };
//
/*
WORKBOOKCONTENT = [WriteProtect] [FilePass] [Template] INTERFACE WriteAccess [FileSharing] CodePage
*2047Lel DSF [Excel9File] RRTabId [ObProj] [ObNoMacros] [CodeName [FNGROUPS] *Lbl
[OleObjectSize] PROTECTION 1*Window1 Backup HideObj Date1904 CalcPrecision RefreshAll
BookBool FORMATTING *(PIVOTCACHEDEFINITION) [DOCROUTE] *UserBView UsesELFs 1*BUNDLESHEET
METADATA [MTRSettings] [ForceFullCalculation] Country *SUPBOOK *LBL *RTD [RecalcId]
*HFPicture *MSODRAWINGGROUP [SHAREDSTRINGS] ExtSST *WebPub [WOpt] [CrErr] [BookExt]
*FeatHdr *DConn [THEME] [CompressPictures] [Compat12] [GUIDTypeLib] EOF
WORKBOOK = BOF WORKBOOKCONTENT
*/
const bool GlobalsSubstream::loadContent(BinProcessor& proc)
{
int count = 0;
......@@ -229,7 +198,7 @@ const bool GlobalsSubstream::loadContent(BinProcessor& proc)
if (( proc.getGlobalWorkbookInfo()->decryptor) &&
( proc.getGlobalWorkbookInfo()->decryptor->IsVerify() == false))
{
if (!proc.getGlobalWorkbookInfo()->decryptor->SetPassword(L"VelvetSweatshop", 2))
if (!proc.getGlobalWorkbookInfo()->decryptor->SetPassword(L"VelvetSweatshop"))
return false;
}
}
......
......@@ -105,6 +105,7 @@ const bool WorkbookStreamObject::loadContent(BinProcessor& proc)
m_GlobalsSubstream = elements_.back();
elements_.pop_back();
}
if (!GlobalsSubstream_found) return false;
}
break;
case BOF::st_Worksheet:
......
......@@ -120,9 +120,9 @@ typedef struct tagBITMAPCOREHEADER {
XlsConverter::XlsConverter(const std::wstring & xls_file, const std::wstring & _xlsx_path, const std::wstring & password, const std::wstring & fontsPath, const ProgressCallback* CallBack)
{
xlsx_path = _xlsx_path;
output_document = NULL;
xlsx_context = NULL;
xlsx_path = _xlsx_path;
output_document = NULL;
xlsx_context = NULL;
pCallBack = CallBack;
bUserStopConvert = false;
......@@ -202,8 +202,7 @@ XlsConverter::XlsConverter(const std::wstring & xls_file, const std::wstring & _
XLS::CFStreamCacheReader stream_reader(cfile.getWorkbookStream(), xls_global_info);
xls_document = boost::shared_ptr<XLS::WorkbookStreamObject>(new XLS::WorkbookStreamObject(workbook_code_page));
XLS::BinReaderProcessor proc(stream_reader , xls_document.get() , true);
proc.mandatory(*xls_document.get());
......@@ -211,7 +210,6 @@ XlsConverter::XlsConverter(const std::wstring & xls_file, const std::wstring & _
{
is_encrypted = true;
if (xls_global_info->decryptor->IsVerify() == false) return;
}
}
catch(...)
......@@ -225,10 +223,7 @@ XlsConverter::XlsConverter(const std::wstring & xls_file, const std::wstring & _
std::wstring sVer = STR::int2hex_wstr(xls_global_info->Version);
Log::error("Version xls is old !!! - " + std::string(sVer.begin(), sVer.end()));
is_older_version = true;
//return;
}
}
output_document = new oox::package::xlsx_document();
xlsx_context = new oox::xlsx_conversion_context(output_document);
}
......
......@@ -288,10 +288,6 @@
RelativePath="..\common\simple_xml_writer.h"
>
</File>
<File
RelativePath="..\..\..\Common\DocxFormat\Source\XML\stringcommon.cpp"
>
</File>
<File
RelativePath="..\common\utils.cpp"
>
......
......@@ -38,8 +38,14 @@
#include "../../Common/3dParty/cryptopp/aes.h"
#include "../../Common/3dParty/cryptopp/sha.h"
#include "../../Common/3dParty/cryptopp/md5.h"
#include "../../Common/3dParty/cryptopp/rsa.h"
#include "../../Common/3dParty/cryptopp/rc2.h"
#include "../../Common/3dParty/cryptopp/arc4.h"
#include "../../Common/3dParty/cryptopp/rc5.h"
#include "../../Common/3dParty/cryptopp/pwdbased.h"
#include "../../Common/3dParty/cryptopp/filters.h"
#include "../../Common/3dParty/cryptopp/osrng.h"
#include "../../Common/3dParty/cryptopp/hex.h"
#include "../../Common/DocxFormat/Source/Base/unicode_util.h"
......@@ -263,14 +269,50 @@ _buf GenerateHashKey(_buf & salt, _buf & password, int hashSize, int spin, CRYPT
}
bool DecryptCipher(_buf & key, _buf & iv, _buf & data_inp, _buf & data_out, CRYPT_METHOD::_cipherAlgorithm algorithm)
{
{
if (algorithm == CRYPT_METHOD::RC4)
{
//CryptoPP::ARC4 rc4(key.ptr, key.size);
//data_out.ptr = new unsigned char[data_inp.size];
//data_out.size = data_inp.size;
//rc4.ProcessData(data_out.ptr, data_inp.ptr, data_inp.size);
//CryptoPP::RC2::Decryption rc2Decryption(key.ptr, key.size);
//modeDecryption = new CryptoPP::ECB_Mode_ExternalCipher::Decryption(rc2Decryption, iv.ptr );
//CryptoPP::RC5::Decryption rc5Decryption(key.ptr, key.size);
//modeDecryption = new CryptoPPp::ECB_Mode_ExternalCipher::Decryption(rc5Decryption, iv.ptr );
//rsa aes
//CryptoPP::HexDecoder buffer;
//buffer.Put(key.ptr, key.size);
//
////CryptoPP::StringSource buffer(key.ptr, key.size, true);
//CryptoPP::RSA::PrivateKey priv;
//priv.Load(buffer);
//CryptoPP::AutoSeededRandomPool rng;
//if(!priv.Validate(rng, 3))
// throw std::runtime_error("Rsa private key validation failed");
//if(!priv.Validate(rng, 3))
// throw std::runtime_error("Dsa private key validation failed");
//CryptoPP::RSAES_PKCS1v15_Decryptor rsaDecryption(priv);
//std::string data((char*)data_inp.ptr, data_inp.size);
//std::string decrypted;
// CryptoPP::StringSource ss(data, true,
// new CryptoPP::PK_DecryptorFilter(rng, rsaDecryption,
// new CryptoPP::StringSink(decrypted)) );
// data_out = decrypted;
//return true;
return false;
}
else
{
CryptoPP::AES::Decryption aesDecryption(key.ptr, key.size);
CryptoPP::StreamTransformation *modeDecryption = NULL;
CryptoPP::AES::Decryption aesDecryption(key.ptr, key.size);
switch(algorithm)
{
......@@ -281,8 +323,9 @@ bool DecryptCipher(_buf & key, _buf & iv, _buf & data_inp, _buf & data_out, CRYP
modeDecryption = new CryptoPP::CBC_Mode_ExternalCipher::Decryption(aesDecryption, iv.ptr );
break;
}
if (!modeDecryption) return false;
if (!data_out.ptr)
{
data_out = _buf(data_inp.size);
......@@ -303,12 +346,16 @@ namespace CRYPT
ECMADecryptor::ECMADecryptor()
{
bVerify = false;
}
bool ECMADecryptor::SetPassword(std::wstring password_)
bool ECMADecryptor::SetPassword(std::wstring _password)
{
password = password_;
bVerify = false;
password = _password;
if (password.empty()) return false;
if (cryptData.bAgile)
{
_buf pPassword (password);
......@@ -334,7 +381,7 @@ bool ECMADecryptor::SetPassword(std::wstring password_)
_buf verifierHashKey = GenerateAgileKey(pSalt, pPassword, pValueBlockKey, cryptData.keySize, cryptData.spinCount, cryptData.hashAlgorithm);
DecryptCipher(verifierHashKey, pSalt, pEncVerValue, decryptedVerifierHashBytes, cryptData.cipherAlgorithm);
return (decryptedVerifierHashBytes==hashBuf);
bVerify = (decryptedVerifierHashBytes==hashBuf);
}
else
{
......@@ -359,8 +406,14 @@ bool ECMADecryptor::SetPassword(std::wstring password_)
DecryptCipher(hashKey, empty, pEncVerValue, decryptedVerifierHashBytes, cryptData.cipherAlgorithm);
return (decryptedVerifierHashBytes==hashBuf);
bVerify = (decryptedVerifierHashBytes==hashBuf);
}
return bVerify;
}
bool ECMADecryptor::IsVerify()
{
return bVerify;
}
void ECMADecryptor::SetCryptData(_cryptData &data)
......@@ -369,13 +422,16 @@ void ECMADecryptor::SetCryptData(_cryptData &data)
}
void ECMADecryptor::Decrypt(char* data , const size_t size, const unsigned long stream_pos)
{
unsigned char* data_out = NULL;
Decrypt((unsigned char*)data, size, data_out);
if (data_out)
if (bVerify)
{
delete []data;
data = (char*)data_out;
unsigned char* data_out = NULL;
Decrypt((unsigned char*)data, size, data_out);
if (data_out)
{
delete []data;
data = (char*)data_out;
}
}
}
void ECMADecryptor::Decrypt(unsigned char* data_inp, int size, unsigned char*& data_out)
......
......@@ -34,6 +34,8 @@
#include <string>
#include <vector>
#include <boost/shared_ptr.hpp>
namespace CRYPT_METHOD
{
enum _hashAlgorithm
......@@ -60,7 +62,11 @@ class Decryptor
{
public:
virtual void Decrypt(char* data, const size_t size, const unsigned long stream_pos) = 0;
virtual bool SetPassword(std::wstring password) = 0;
virtual bool IsVerify() = 0;
};
typedef boost::shared_ptr<Decryptor> DecryptorPtr;
class ECMADecryptor : public Decryptor
{
......@@ -102,16 +108,18 @@ public:
ECMADecryptor();
virtual ~ECMADecryptor(){}
void Decrypt(unsigned char* data, int size, unsigned char*& data_out);
virtual void Decrypt(char* data , const size_t size, const unsigned long stream_pos);
bool SetPassword(std::wstring password);
void Decrypt (unsigned char* data, int size, unsigned char*& data_out);
virtual void Decrypt (char* data , const size_t size, const unsigned long stream_pos);
virtual bool SetPassword (std::wstring password);
virtual bool IsVerify();
void SetCryptData(_cryptData &data);
private:
_cryptData cryptData;
std::wstring password;
_cryptData cryptData;
bool bVerify;
};
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment