From: James Nobis Date: Sun, 7 Nov 2010 01:19:23 +0000 (-0500) Subject: Merge branch 'master' of git@gitorious.org:freerainbowtables-applications/freerainbow... X-Git-Url: https://git.sesse.net/?p=freerainbowtables;a=commitdiff_plain;h=86bbf0fd5ba4e07d3279b4179fd8fc808198eaae Merge branch 'master' of git@gitorious.org:freerainbowtables-applications/freerainbowtables Conflicts: Client Applications/rcracki_mt/ChainWalkContext.cpp Client Applications/rcracki_mt/ChainWalkContext.h Client Applications/rcracki_mt/CrackEngine.cpp Client Applications/rcracki_mt/HashAlgorithm.cpp Client Applications/rcracki_mt/HashRoutine.cpp Client Applications/rcracki_mt/HashSet.cpp Client Applications/rcracki_mt/MemoryPool.cpp Client Applications/rcracki_mt/MemoryPool.h Client Applications/rcracki_mt/Public.cpp Client Applications/rcracki_mt/Public.h Client Applications/rcracki_mt/RTI2Reader.cpp Client Applications/rcracki_mt/RTI2Reader.h Client Applications/rcracki_mt/RainbowCrack.cpp Client Applications/rcracki_mt/lm2ntlm.cpp Client Applications/rcracki_mt/md4.cpp Client Applications/rcracki_mt/rcrackiThread.cpp Client Applications/rcracki_mt/sha1.cpp Client Applications/rti2rto/rti2rto.cpp Common/rt api/BaseRTReader.h Common/rt api/MemoryPool.cpp Common/rt api/RTI2Reader.cpp Common/rt api/RTIReader.cpp merging with PB's changes UINT4 -> uint32 changes misc fixes for rti2 misc fixes for long 64/32 compat --- 86bbf0fd5ba4e07d3279b4179fd8fc808198eaae diff --cc Client Applications/converti2/Makefile index 6323267,d3715df..4886c1a --- a/Client Applications/converti2/Makefile +++ b/Client Applications/converti2/Makefile @@@ -3,10 -3,10 +3,10 @@@ CC = g+ OPTIMIZATION = -O3 INCLUDES = -I../../Common/rt\ api # XXX todo currently only 32-bit targets work --CFLAGS = -Wall -m32 -ansi $(INCLUDES) $(OPTIMIZATION) -c --LFLAGS = -Wall -m32 -ansi $(INCLUDES) $(OPTIMIZATION) ++CFLAGS = -Wall -m32 -ansi $(INCLUDES) $(OPTIMIZATION) -c $(DEBUG) ++LFLAGS = -Wall -m32 -ansi $(INCLUDES) $(OPTIMIZATION) $(DEBUG) LIBS = --OBJS = MemoryPool.o Public.o ++OBJS = MemoryPool.o Public.o RTI2Reader.o RTIReader.o RTReader.o COMMON_API_PATH = ../../Common/rt\ api all: converti2 @@@ -17,10 -17,10 +17,25 @@@ converti2: $(OBJS clean: rm -f *.o converti2 ++debug: DEBUG += -DDEBUG -g ++debug: all ++ ++#m32: DEBUG += -m32 ++#m32: converti2 ++ rebuild: clean all -MemoryPool.o: $(COMMON_API_PATH)/MemoryPool.h $(COMMON_API_PATH)/MemoryPool.cpp Public.h +MemoryPool.o: $(COMMON_API_PATH)/MemoryPool.h $(COMMON_API_PATH)/MemoryPool.cpp $(COMMON_API_PATH)/Public.h $(CC) $(CFLAGS) $(COMMON_API_PATH)/MemoryPool.cpp -Public.o: Public.h Public.cpp - $(CC) $(CFLAGS) Public.cpp +Public.o: $(COMMON_API_PATH)/Public.h $(COMMON_API_PATH)/Public.cpp + $(CC) $(CFLAGS) $(COMMON_API_PATH)/Public.cpp ++ ++RTI2Reader.o: $(COMMON_API_PATH)/RTI2Reader.h $(COMMON_API_PATH)/RTI2Reader.cpp ++ $(CC) $(CFLAGS) $(COMMON_API_PATH)/RTI2Reader.cpp ++ ++RTIReader.o: $(COMMON_API_PATH)/RTIReader.h $(COMMON_API_PATH)/RTIReader.cpp ++ $(CC) $(CFLAGS) $(COMMON_API_PATH)/RTIReader.cpp ++ ++RTReader.o: $(COMMON_API_PATH)/RTReader.h $(COMMON_API_PATH)/RTReader.cpp ++ $(CC) $(CFLAGS) $(COMMON_API_PATH)/RTReader.cpp diff --cc Client Applications/converti2/Public.cpp index 7e54203,7e54203..0000000 deleted file mode 100644,100644 --- a/Client Applications/converti2/Public.cpp +++ /dev/null @@@ -1,319 -1,319 +1,0 @@@ --/* -- * freerainbowtables is a project for generating, distributing, and using -- * perfect rainbow tables -- * -- * Copyright (C) Zhu Shuanglei -- * Copyright Martin Westergaard Jørgensen -- * Copyright 2009, 2010 Daniël Niggebrugge -- * Copyright 2009, 2010 James Nobis -- * -- * This file is part of freerainbowtables. -- * -- * freerainbowtables is free software: you can redistribute it and/or modify -- * it under the terms of the GNU General Public License as published by -- * the Free Software Foundation, either version 2 of the License, or -- * (at your option) any later version. -- * -- * freerainbowtables is distributed in the hope that it will be useful, -- * but WITHOUT ANY WARRANTY; without even the implied warranty of -- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -- * GNU General Public License for more details. -- * -- * You should have received a copy of the GNU General Public License -- * along with freerainbowtables. If not, see . --*/ -- --#ifdef _WIN32 -- #pragma warning(disable : 4786) --#endif -- --#ifdef _WIN32 -- --#else --#include --#include --#include --#include --#include --#include --#include -- --#endif -- --#include "Public.h" -- --#ifdef _WIN32 -- #include --#elif defined(__APPLE__) || \ -- ((defined(__unix__) || defined(unix)) && !defined(USG)) -- -- #include -- -- #if defined(BSD) -- #include -- #elif defined(__linux__) -- #include -- #else -- #error Unsupported Operating system -- #endif --#endif -- --////////////////////////////////////////////////////////////////////// -- --unsigned int GetFileLen(FILE* file) --{ -- unsigned int pos = ftell(file); -- fseek(file, 0, SEEK_END); -- unsigned int len = ftell(file); -- fseek(file, pos, SEEK_SET); -- -- return len; --} -- --string TrimString(string s) --{ -- while (s.size() > 0) -- { -- if (s[0] == ' ' || s[0] == '\t') -- s = s.substr(1); -- else -- break; -- } -- -- while (s.size() > 0) -- { -- if (s[s.size() - 1] == ' ' || s[s.size() - 1] == '\t') -- s = s.substr(0, s.size() - 1); -- else -- break; -- } -- -- return s; --} --bool GetHybridCharsets(string sCharset, vector& vCharset) --{ -- // Example: hybrid(mixalpha-numeric-all-space#1-6,numeric#1-4) -- if(sCharset.substr(0, 6) != "hybrid") // Not hybrid charset -- return false; -- -- UINT4 nEnd = (int) sCharset.rfind(')'); -- UINT4 nStart = (int) sCharset.rfind('('); -- string sChar = sCharset.substr(nStart + 1, nEnd - nStart - 1); -- vector vParts; -- SeperateString(sChar, ",", vParts); -- for(UINT4 i = 0; i < vParts.size(); i++) -- { -- tCharset stCharset; -- vector vParts2; -- SeperateString(vParts[i], "#", vParts2); -- stCharset.sName = vParts2[0]; -- vector vParts3; -- SeperateString(vParts2[1], "-", vParts3); -- stCharset.nPlainLenMin = atoi(vParts3[0].c_str()); -- stCharset.nPlainLenMax = atoi(vParts3[1].c_str()); -- vCharset.push_back(stCharset); -- } -- return true; --} --bool ReadLinesFromFile(string sPathName, vector& vLine) --{ -- vLine.clear(); -- FILE *file = fopen(sPathName.c_str(), "rb"); -- if (file != NULL) -- { -- unsigned int len = GetFileLen(file); -- char* data = new char[len + 1]; -- fread(data, 1, len, file); -- data[len] = '\0'; -- string content = data; -- content += "\n"; -- delete [] data; -- -- unsigned int i; -- for (i = 0; i < content.size(); i++) -- { -- if (content[i] == '\r') -- content[i] = '\n'; -- } -- -- int n; -- while ((n = content.find("\n", 0)) != -1) -- { -- string line = content.substr(0, n); -- line = TrimString(line); -- if (line != "") -- vLine.push_back(line); -- content = content.substr(n + 1); -- } -- -- fclose(file); -- } -- else -- return false; -- -- return true; --} -- --bool SeperateString(string s, string sSeperator, vector& vPart) --{ -- vPart.clear(); -- -- unsigned int i; -- for (i = 0; i < sSeperator.size(); i++) -- { -- int n = s.find(sSeperator[i]); -- if (n != -1) -- { -- vPart.push_back(s.substr(0, n)); -- s = s.substr(n + 1); -- } -- else -- return false; -- } -- vPart.push_back(s); -- -- return true; --} -- --string uint64tostr(uint64 n) --{ -- char str[32]; -- --#ifdef _WIN32 -- sprintf(str, "%I64u", n); --#else -- sprintf(str, "%llu", n); --#endif -- -- return str; --} -- --string uint64tohexstr(uint64 n) --{ -- char str[32]; -- --#ifdef _WIN32 -- sprintf(str, "%016I64x", n); --#else -- sprintf(str, "%016llx", n); --#endif -- -- return str; --} -- --string HexToStr(const unsigned char* pData, int nLen) --{ -- string sRet; -- int i; -- for (i = 0; i < nLen; i++) -- { -- char szByte[3]; -- sprintf(szByte, "%02x", pData[i]); -- sRet += szByte; -- } -- -- return sRet; --} -- --uint64 GetAvailPhysMemorySize() --{ --#ifdef _WIN32 -- MEMORYSTATUS ms; -- GlobalMemoryStatus(&ms); -- return ms.dwAvailPhys; --#elif defined(BSD) -- int mib[2] = { CTL_HW, HW_PHYSMEM }; -- uint64 physMem; -- //XXX warning size_t isn't portable -- size_t len; -- len = sizeof(physMem); -- sysctl(mib, 2, &physMem, &len, NULL, 0); -- return physMem; --#elif defined(__linux__) -- struct sysinfo info; -- sysinfo(&info); -- return ( info.freeram + info.bufferram ) * (unsigned long) info.mem_unit; --#else -- return 0; -- #error Unsupported Operating System --#endif --} -- --void ParseHash(string sHash, unsigned char* pHash, int& nHashLen) --{ -- UINT4 i; -- for (i = 0; i < sHash.size() / 2; i++) -- { -- string sSub = sHash.substr(i * 2, 2); -- int nValue; -- sscanf(sSub.c_str(), "%02x", &nValue); -- pHash[i] = (unsigned char)nValue; -- } -- -- nHashLen = sHash.size() / 2; --} -- --void Logo() --{ -- printf("RainbowCrack (improved) 2.0 - Making a Faster Cryptanalytic Time-Memory Trade-Off\n"); -- printf("by Martin Westergaard \n"); -- printf("http://www.freerainbowtables.com/\n"); -- printf("original code by Zhu Shuanglei \n"); -- printf("http://www.antsight.com/zsl/rainbowcrack/\n\n"); --} -- --// XXX nmap is GPL2, will check newer releases regarding license --// Code comes from nmap, used for the linux implementation of kbhit() --#ifndef _WIN32 -- --static int tty_fd = 0; --struct termios saved_ti; -- --int tty_getchar() --{ -- int c, numChars; -- -- if (tty_fd && tcgetpgrp(tty_fd) == getpid()) { -- c = 0; -- numChars = read(tty_fd, &c, 1); -- if (numChars > 0) return c; -- } -- -- return -1; --} -- --void tty_done() --{ -- if (!tty_fd) return; -- -- tcsetattr(tty_fd, TCSANOW, &saved_ti); -- -- close(tty_fd); -- tty_fd = 0; --} -- --void tty_init() --{ -- struct termios ti; -- -- if (tty_fd) -- return; -- -- if ((tty_fd = open("/dev/tty", O_RDONLY | O_NONBLOCK)) < 0) return; -- -- tcgetattr(tty_fd, &ti); -- saved_ti = ti; -- ti.c_lflag &= ~(ICANON | ECHO); -- ti.c_cc[VMIN] = 1; -- ti.c_cc[VTIME] = 0; -- tcsetattr(tty_fd, TCSANOW, &ti); -- -- atexit(tty_done); --} -- --void tty_flush(void) --{ -- tcflush(tty_fd, TCIFLUSH); --} --// end nmap code --#endif diff --cc Client Applications/converti2/Public.h index 6c15430,6c15430..0000000 deleted file mode 100644,100644 --- a/Client Applications/converti2/Public.h +++ /dev/null @@@ -1,113 -1,113 +1,0 @@@ --/* -- * freerainbowtables is a project for generating, distributing, and using -- * perfect rainbow tables -- * -- * Copyright (C) Zhu Shuanglei -- * Copyright 2009, 2010 Daniël Niggebrugge -- * Copyright 2009, 2010 James Nobis -- * -- * This file is part of freerainbowtables. -- * -- * freerainbowtables is free software: you can redistribute it and/or modify -- * it under the terms of the GNU General Public License as published by -- * the Free Software Foundation, either version 2 of the License, or -- * (at your option) any later version. -- * -- * freerainbowtables is distributed in the hope that it will be useful, -- * but WITHOUT ANY WARRANTY; without even the implied warranty of -- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -- * GNU General Public License for more details. -- * -- * You should have received a copy of the GNU General Public License -- * along with freerainbowtables. If not, see . -- */ -- --#ifndef _PUBLIC_H --#define _PUBLIC_H -- --#include -- --#include --#include --#include -- --#include "global.h" -- --using namespace std; -- --struct RainbowChain --{ -- uint64 nIndexS; -- uint64 nIndexE; --}; -- --struct RainbowChainCP --{ -- uint64 nIndexS; -- uint64 nIndexE; -- unsigned short nCheckPoint; --}; --struct IndexChain --{ -- uint64 nPrefix; -- int nFirstChain; -- unsigned int nChainCount; --}; --struct FoundRainbowChain --{ -- uint64 nIndexS; -- int nIndexE; -- int nCheckPoint; -- int nGuessedPos; --}; --struct ChainCheckChain --{ -- uint64 nIndexS; -- int nGuessedPos; --}; --struct IndexRow --{ -- uint64 prefix; -- unsigned int prefixstart, numchains; --}; -- --typedef struct --{ -- string sName; -- int nPlainLenMin; -- int nPlainLenMax; --} tCharset; -- --#define MAX_PLAIN_LEN 256 --#define MIN_HASH_LEN 8 --#define MAX_HASH_LEN 256 --#define MAX_SALT_LEN 256 -- --// XXX nmap is GPL2, will check newer releases regarding license --// Code comes from nmap, used for the linux implementation of kbhit() --#ifndef _WIN32 --#include --#include --#include -- --int tty_getchar(); --void tty_done(); --void tty_init(); --void tty_flush(void); --// end nmap code -- --#endif -- --unsigned int GetFileLen(FILE* file); --string TrimString(string s); --bool ReadLinesFromFile(string sPathName, vector& vLine); --bool SeperateString(string s, string sSeperator, vector& vPart); --string uint64tostr(uint64 n); --string uint64tohexstr(uint64 n); --string HexToStr(const unsigned char* pData, int nLen); --uint64 GetAvailPhysMemorySize(); --void ParseHash(string sHash, unsigned char* pHash, int& nHashLen); --bool GetHybridCharsets(string sCharset, vector& vCharset); --void Logo(); -- --#endif diff --cc Client Applications/converti2/converti2.cpp index db703e9,1d8d5e7..da728c3 mode 100644,100644..100755 --- a/Client Applications/converti2/converti2.cpp +++ b/Client Applications/converti2/converti2.cpp @@@ -14,9 -13,12 +14,11 @@@ #include #include #include - -#include + #include #include "Public.h" #include "MemoryPool.h" + #include "RTIReader.h" + #include "RTReader.h" using namespace std; @@@ -290,79 -289,67 +289,67 @@@ void ConvertRainbowTable(string sPathNa int nIndex = sPathName.find_last_of('/'); #endif string sFileName; - if (nIndex != -1) + if (nIndex != -1) { sFileName = sPathName.substr(nIndex + 1); - else + } + else { sFileName = sPathName; - // Info - printf("%s:\n", sFileName.c_str()); - FILE* file = fopen(sPathName.c_str(), "rb"); - FILE* fileR = fopen(sResultFileName.c_str(), "wb"); + } unsigned int distribution[64] = {0}; unsigned int numProcessedChains = 0; - - if (file != NULL && fileR != NULL) - { + FILE* fileR; + BaseRTReader *reader = NULL; + if(sPathName.substr(sPathName.length() - 2, sPathName.length()) == "rt") + reader = (BaseRTReader*)new RTReader(sPathName); + else if(sPathName.substr(sPathName.length() - 3, sPathName.length()) == "rti") + reader = (BaseRTReader*)new RTIReader(sPathName); + if(reader == NULL) { + printf("%s is not a supported file (Only RT and RTI is supported)\n", sPathName.c_str()); + return; + } + // Info + printf("%s:\n", sFileName.c_str()); + if(showDistribution == 0) { + fileR = fopen(sResultFileName.c_str(), "wb"); + } + if (fileR != NULL || showDistribution == 1) { // File length check - UINT4 nFileLen = GetFileLen(file); - UINT4 nTotalChainCount = 0; - if(hascp == 0) nTotalChainCount = nFileLen / 16; - else nTotalChainCount = nFileLen / 18; - if ((hascp == 0 && nFileLen % 16 != 0) || (hascp == 1 && nFileLen % 18 != 0)) - { - printf("file length mismatch\n"); - } - else - { + + int size = reader->GetChainsLeft() * sizeof(RainbowChain); static CMemoryPool mp; unsigned int nAllocatedSize; - RainbowChainCP* pChain = (RainbowChainCP*)mp.Allocate(nFileLen, nAllocatedSize); - - unsigned int chainrowsize = ceil((float)(rti_startptlength + rti_endptlength + rti_cplength) / 8) * 8 ; // The size in bits (in whole bytes) + RainbowChain* pChain = (RainbowChain*)mp.Allocate(size, nAllocatedSize); - unsigned int chainrowsize = ceil((float)(rti_startptlength + rti_endptlength + rti_cplength) / 8) * 8 ; // The size in bits (in whole bytes) ++ uint32 chainrowsize = (uint32)ceil((float)(rti_startptlength + rti_endptlength + rti_cplength) / 8) * 8 ; // The size in bits (in whole bytes) unsigned int chainrowsizebytes = chainrowsize / 8; - - if (pChain != NULL) - { - nAllocatedSize = nAllocatedSize / sizeof(RainbowChainCP) * sizeof(RainbowChainCP); - fseek(file, 0, SEEK_SET); + if (pChain != NULL) { + nAllocatedSize = nAllocatedSize / sizeof(RainbowChain) * sizeof(RainbowChain); + unsigned int nChains = nAllocatedSize / sizeof(RainbowChain); uint64 curPrefix = 0, prefixStart = 0; vector indexes; - UINT4 nRainbowChainCountRead = 0; - while (true) // Chunk read loop - { + unsigned int chainsLeft; + while((chainsLeft = reader->GetChainsLeft()) > 0) { + /* if (ftell(file) == nFileLen) break;*/ - UINT4 nReadThisRound; - memset(pChain, 0x00, nAllocatedSize); - printf("reading...\n"); + printf("%u chains left to read\n", chainsLeft); - int nReadThisRound; ++ //int nReadThisRound; clock_t t1 = clock(); - for(nReadThisRound = 0; nReadThisRound < nAllocatedSize / sizeof(RainbowChainCP) && nRainbowChainCountRead < nTotalChainCount; nReadThisRound++) - { - if(fread(&pChain[nReadThisRound], 16, 1, file) != 1) - { - printf("Error reading file\n"); exit(1); - } - if(hascp == 1) - { - if(fread(&pChain[nReadThisRound].nCheckPoint, 2, 1, file) != 1) - { - printf("Error reading file\n"); exit(2); - } - } - nRainbowChainCountRead++; - } + printf("reading...\n"); + #ifdef _MEMORYDEBUG + printf("Grabbing %i chains from file\n", nChains); + #endif + reader->ReadChains(nChains, pChain); + #ifdef _MEMORYDEBUG + printf("Recieved %i chains from file\n", nChains); + #endif clock_t t2 = clock(); float fTime = 1.0f * (t2 - t1) / CLOCKS_PER_SEC; - int nDataRead = nRainbowChainCountRead * 16; - if(hascp == 1) nDataRead += nRainbowChainCountRead * 2; // Add the index readings too - printf("%u bytes read, disk access time: %.2f s\n", nDataRead , fTime); + printf("reading time: %.2f s\n", fTime); + printf("converting %i chains...\n", nChains); t1 = clock(); - - for(UINT4 i = 0; i < nReadThisRound; i++) - { - if(showDistribution == 1) - { - for(int i = 0; i < nChains; i++) { ++ for(unsigned int i = 0; i < nChains; i++) { + if(showDistribution == 1) { distribution[GetMaxBits(pChain[i].nIndexS)-1]++; } else @@@ -436,7 -417,7 +417,7 @@@ // unsigned int m_rti_index_prefixlength = GetMaxBits(high.prefix); unsigned int m_rti_index_numchainslength = GetMaxBits(high.numchains); // unsigned int m_rti_index_indexlength = GetMaxBits(high.prefixstart); -- unsigned int m_indexrowsize = ceil((float)(/*m_rti_index_indexlength + */m_rti_index_numchainslength) / 8) * 8; // The size in bits (in whole bytes) ++ uint32 m_indexrowsize = (uint32)ceil((float)(/*m_rti_index_indexlength + */m_rti_index_numchainslength) / 8) * 8; // The size in bits (in whole bytes) unsigned int m_indexrowsizebytes = m_indexrowsize / 8; FILE *pFileIndex = fopen(sResultFileName.append(".index").c_str(), "wb"); fwrite("RTI2", 1, 4, pFileIndex); @@@ -453,14 -434,14 +434,14 @@@ int zero = 0; fwrite(&indexes[0].prefix, 1, 8, pFileIndex); // Write the first prefix unsigned int lastPrefix = 0; - for(UINT4 i = 0; i < indexes.size(); i++) - { - if(i == 0) + for(UINT4 i = 0; i < indexes.size(); i++) { + if(i == 0) { lastPrefix = indexes[0].prefix; + } - unsigned int indexrow = 0; ++ //unsigned int indexrow = 0; // Checks how big a distance there is between the current and the next prefix. eg cur is 3 and next is 10 = 7. unsigned int diffSize = indexes[i].prefix - lastPrefix; - if(i > 0 && diffSize > 1) - { + if(i > 0 && diffSize > 1) { //indexrow |= indexes[i].prefixstart; //printf("Diffsize is %u\n", diffSize); diff --cc Client Applications/converti2/converti2.suo index d24f654,969775d..8a63fa9 mode 100644,100644..100755 Binary files differ diff --cc Client Applications/rcracki_mt/BaseRTReader.h index 17a93ac,0000000..a662ff4 mode 100644,000000..100644 --- a/Client Applications/rcracki_mt/BaseRTReader.h +++ b/Client Applications/rcracki_mt/BaseRTReader.h @@@ -1,45 -1,0 +1,46 @@@ +/* + * rcracki_mt is a multithreaded implementation and fork of the original + * RainbowCrack + * + * Copyright 2010 Martin Westergaard Jørgensen + * Copyright 2010 Daniël Niggebrugge + * Copyright 2010 James Nobis + * + * This file is part of rcracki_mt. + * + * rcracki_mt is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * rcracki_mt is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with rcracki_mt. If not, see . + */ + +#ifndef __BASERTREADER_H__ +#define __BASERTREADER_H__ + +#include "Public.h" +#include + +#if defined(_WIN32) && !defined(__GNUC__) + #include +#endif + +using namespace std; + +class BaseRTReader +{ +public: - virtual int ReadChains(unsigned int &numChains, RainbowChainO *pData) = 0; - virtual unsigned int GetChainsLeft() = 0; ++ virtual int ReadChains(uint32 &numChains, RainbowChainO *pData) = 0; ++ virtual uint32 GetChainsLeft() = 0; + ++ virtual ~BaseRTReader() { }; +}; + +#endif diff --cc Client Applications/rcracki_mt/ChainWalkSet.cpp index 84db65f,cab23a1..b80b4ab --- a/Client Applications/rcracki_mt/ChainWalkSet.cpp +++ b/Client Applications/rcracki_mt/ChainWalkSet.cpp @@@ -1,324 -1,324 +1,324 @@@ - /* - * rcracki_mt is a multithreaded implementation and fork of the original - * RainbowCrack - * - * Copyright (C) Zhu Shuanglei - * Copyright Martin Westergaard Jørgensen - * Copyright 2009, 2010 Daniël Niggebrugge - * Copyright 2009, 2010 James Nobis - * - * This file is part of rcracki_mt. - * - * rcracki_mt is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 2 of the License, or - * (at your option) any later version. - * - * rcracki_mt is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with rcracki_mt. If not, see . - */ - - #if defined(_WIN32) && !defined(__GNUC__) - #pragma warning(disable : 4786) - #endif - - #include "ChainWalkSet.h" - - CChainWalkSet::CChainWalkSet() - { - m_sHashRoutineName = ""; - m_sPlainCharsetName = ""; - m_nPlainLenMin = 0; - m_nPlainLenMax = 0; - m_nRainbowTableIndex = 0; - m_nRainbowChainLen = 0; - debug = false; - sPrecalcPathName = ""; - preCalcPart = 0; - } - - CChainWalkSet::~CChainWalkSet() - { - DiscardAll(); - } - - void CChainWalkSet::DiscardAll() - { - //printf("debug: discarding all walk...\n"); - - list::iterator it; - for (it = m_lChainWalk.begin(); it != m_lChainWalk.end(); it++) - delete [] it->pIndexE; - m_lChainWalk.clear(); - } - - string CChainWalkSet::CheckOrRotatePreCalcFile() - { - char sPreCalcFileName[255]; - - // 255 files limit to be sure - for (; preCalcPart < 255; preCalcPart++) - { - sprintf(sPreCalcFileName, "%s.%d", sPrecalcPathName.c_str(), preCalcPart); - string sReturnPreCalcPath(sPreCalcFileName); - - unsigned int fileLen = 0; - - FILE* file = fopen(sReturnPreCalcPath.c_str(), "ab"); - if(file!=NULL) - { - fileLen = GetFileLen(file); - long unsigned int nextFileLen = fileLen + (sizeof(uint64) * (m_nRainbowChainLen-1)); - // Rotate to next file if we are going to pass 2GB filesize - if (nextFileLen < ((unsigned)2 * 1024 * 1024 * 1024)) - { - // We might want to vPrecalcFiles.push_back(sReturnPreCalcPath) if we just created this file - // We don't as only newly generated chainwalksets will be stored to this new file, so we don't have to look there - if (debug) printf("Debug: Using for precalc: %s\n", sReturnPreCalcPath.c_str()); - fclose(file); - return sReturnPreCalcPath; - } - fclose(file); - } - } - - return string(""); - } - - void CChainWalkSet::updateUsedPrecalcFiles() - { - // we might also use this function to search a wildcard path of precalc files - vPrecalcFiles.clear(); - char sPreCalcFileName[255]; - - int i; - // 255 files max - for (i = 0; i < 255; i++) - { - sprintf(sPreCalcFileName, "%s.%d", sPrecalcPathName.c_str(), i); - string sTryPreCalcPath(sPreCalcFileName); - FILE* file = fopen(sTryPreCalcPath.c_str(), "rb"); - if(file!=NULL) { - vPrecalcFiles.push_back(sTryPreCalcPath); - fclose(file); - } - else { - break; - } - } - } - - void CChainWalkSet::removePrecalcFiles() - { - if (debug) printf("Debug: Removing precalc files.\n"); - updateUsedPrecalcFiles(); - string sCurrentPrecalcPathName = ""; - string sCurrentPrecalcIndexPathName = ""; - - int i; - for (i = 0; i < (int)vPrecalcFiles.size(); i++) - { - sCurrentPrecalcPathName = vPrecalcFiles[i]; - sCurrentPrecalcIndexPathName = sCurrentPrecalcPathName + ".index"; - - if (debug) printf("Debug: Removing precalc file: %s\n", sCurrentPrecalcPathName.c_str()); - - if (remove(sCurrentPrecalcPathName.c_str()) != 0) - if (debug) printf("Debug: Failed removing precalc file: %s\n", sCurrentPrecalcPathName.c_str()); - - if (debug) printf("Debug: Removing precalc index file: %s\n", sCurrentPrecalcIndexPathName.c_str()); - - if (remove(sCurrentPrecalcIndexPathName.c_str()) != 0) - if (debug) printf("Debug: Failed removing precalc index file: %s\n", sCurrentPrecalcIndexPathName.c_str()); - - } - } - - bool CChainWalkSet::FindInFile(uint64* pIndexE, unsigned char* pHash, int nHashLen) - { - int gotPrecalcOnLine = -1; - char precalculationLine[255]; - sprintf(precalculationLine, "%s_%s#%d-%d_%d_%d:%s\n", m_sHashRoutineName.c_str(), m_sPlainCharsetName.c_str(), m_nPlainLenMin, m_nPlainLenMax, m_nRainbowTableIndex, m_nRainbowChainLen, HexToStr(pHash, nHashLen).c_str() ); - string precalcString(precalculationLine); - - string sCurrentPrecalcPathName = ""; - string sCurrentPrecalcIndexPathName = ""; - long unsigned int offset; - - int i; - for (i = 0; i < (int)vPrecalcFiles.size() && gotPrecalcOnLine == -1; i++) - { - sCurrentPrecalcPathName = vPrecalcFiles[i]; - sCurrentPrecalcIndexPathName = sCurrentPrecalcPathName + ".index"; - - offset = 0; - - vector precalcLines; - if (ReadLinesFromFile(sCurrentPrecalcIndexPathName.c_str(), precalcLines)) - { - int j; - for (j = 0; j < (int)precalcLines.size(); j++) - { - if (precalcString.compare(0, precalcString.size()-1, precalcLines[j]) == 0) - { - gotPrecalcOnLine = j; - break; - } - - // Parse - vector vPart; - if (SeperateString(precalcLines[j], "___:", vPart)) - { - // add to offset - offset += ((atoi(vPart[3].c_str())-1) * sizeof(uint64)); - } - else { - // corrupt file - printf("Corrupted precalculation file!\n"); - gotPrecalcOnLine = -1; - break; - } - } - } - } - - if (gotPrecalcOnLine > -1) - { - if (debug) printf("Debug: Reading pre calculations from file, line %d offset %lu\n", gotPrecalcOnLine, offset); - - FILE* fp = fopen(sCurrentPrecalcPathName.c_str(), "rb"); - - if (fp!=NULL) { - fseek(fp, offset, SEEK_SET); - - // We should do some verification here, for example by recalculating the middle chain, to catch corrupted files - if(fread(pIndexE, sizeof(uint64), (unsigned long)m_nRainbowChainLen-1, fp) != (unsigned long)m_nRainbowChainLen-1) - printf("File read error."); - fclose(fp); - } - else - printf("Cannot open precalculation file %s.\n", sCurrentPrecalcPathName.c_str()); - - //printf("\npIndexE[0]: %s\n", uint64tostr(pIndexE[0]).c_str()); - //printf("\npIndexE[nRainbowChainLen-2]: %s\n", uint64tostr(pIndexE[m_nRainbowChainLen-2]).c_str()); - - return true; - } - - return false; - } - - void CChainWalkSet::StoreToFile(uint64* pIndexE, unsigned char* pHash, int nHashLen) - { - if (debug) printf("\nDebug: Storing precalc\n"); - - string sCurrentPrecalcPathName = CheckOrRotatePreCalcFile(); - string sCurrentPrecalcIndexPathName = sCurrentPrecalcPathName + ".index"; - - FILE* fp = fopen(sCurrentPrecalcPathName.c_str(), "ab"); - if(fp!=NULL) - { - if(fwrite(pIndexE, sizeof(uint64), (unsigned long)m_nRainbowChainLen-1, fp) != (unsigned long)m_nRainbowChainLen-1) - printf("File write error."); - else - { - FILE* file = fopen(sCurrentPrecalcIndexPathName.c_str(), "a"); - if (file!=NULL) - { - char precalculationLine[255]; - sprintf(precalculationLine, "%s_%s#%d-%d_%d_%d:%s\n", m_sHashRoutineName.c_str(), m_sPlainCharsetName.c_str(), m_nPlainLenMin, m_nPlainLenMax, m_nRainbowTableIndex, m_nRainbowChainLen, HexToStr(pHash, nHashLen).c_str() ); - fputs (precalculationLine, file); - fclose (file); - } - } - fclose(fp); - } - else - printf("Cannot open precalculation file %s\n", sCurrentPrecalcPathName.c_str()); - } - - uint64* CChainWalkSet::RequestWalk(unsigned char* pHash, int nHashLen, - string sHashRoutineName, - string sPlainCharsetName, int nPlainLenMin, int nPlainLenMax, - int nRainbowTableIndex, - int nRainbowChainLen, - bool& fNewlyGenerated, - bool setDebug, - string sPrecalc) - { - debug = setDebug; - sPrecalcPathName = sPrecalc; - - if ( m_sHashRoutineName != sHashRoutineName - || m_sPlainCharsetName != sPlainCharsetName - || m_nPlainLenMin != nPlainLenMin - || m_nPlainLenMax != nPlainLenMax - || m_nRainbowTableIndex != nRainbowTableIndex - || m_nRainbowChainLen != nRainbowChainLen) - { - DiscardAll(); - - m_sHashRoutineName = sHashRoutineName; - m_sPlainCharsetName = sPlainCharsetName; - m_nPlainLenMin = nPlainLenMin; - m_nPlainLenMax = nPlainLenMax; - m_nRainbowTableIndex = nRainbowTableIndex; - m_nRainbowChainLen = nRainbowChainLen; - - ChainWalk cw; - memcpy(cw.Hash, pHash, nHashLen); - cw.pIndexE = new uint64[nRainbowChainLen - 1]; - m_lChainWalk.push_back(cw); - - // Only update this list when we search through another rainbow table - updateUsedPrecalcFiles(); - - if (!FindInFile(cw.pIndexE, pHash, nHashLen)) - fNewlyGenerated = true; - else - fNewlyGenerated = false; - return cw.pIndexE; - } - - list::iterator it; - for (it = m_lChainWalk.begin(); it != m_lChainWalk.end(); it++) - { - if (memcmp(it->Hash, pHash, nHashLen) == 0) - { - fNewlyGenerated = false; - return it->pIndexE; - } - } - - ChainWalk cw; - memcpy(cw.Hash, pHash, nHashLen); - cw.pIndexE = new uint64[nRainbowChainLen - 1]; - m_lChainWalk.push_back(cw); - - if (!FindInFile(cw.pIndexE, pHash, nHashLen)) - fNewlyGenerated = true; - else - fNewlyGenerated = false; - return cw.pIndexE; - } - - void CChainWalkSet::DiscardWalk(uint64* pIndexE) - { - list::iterator it; - for (it = m_lChainWalk.begin(); it != m_lChainWalk.end(); it++) - { - if (it->pIndexE == pIndexE) - { - delete it->pIndexE; - m_lChainWalk.erase(it); - return; - } - } - - printf("debug: pIndexE not found\n"); - } + /* + * rcracki_mt is a multithreaded implementation and fork of the original + * RainbowCrack + * + * Copyright (C) Zhu Shuanglei + * Copyright Martin Westergaard Jørgensen + * Copyright 2009, 2010 Daniël Niggebrugge + * Copyright 2009, 2010 James Nobis + * + * This file is part of rcracki_mt. + * + * rcracki_mt is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * rcracki_mt is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with rcracki_mt. If not, see . + */ + + #if defined(_WIN32) && !defined(__GNUC__) + #pragma warning(disable : 4786) + #endif + + #include "ChainWalkSet.h" + + CChainWalkSet::CChainWalkSet() + { + m_sHashRoutineName = ""; + m_sPlainCharsetName = ""; + m_nPlainLenMin = 0; + m_nPlainLenMax = 0; + m_nRainbowTableIndex = 0; + m_nRainbowChainLen = 0; + debug = false; + sPrecalcPathName = ""; + preCalcPart = 0; + } + + CChainWalkSet::~CChainWalkSet() + { + DiscardAll(); + } + + void CChainWalkSet::DiscardAll() + { + //printf("debug: discarding all walk...\n"); + + list::iterator it; + for (it = m_lChainWalk.begin(); it != m_lChainWalk.end(); it++) + delete [] it->pIndexE; + m_lChainWalk.clear(); + } + + string CChainWalkSet::CheckOrRotatePreCalcFile() + { + char sPreCalcFileName[255]; + + // 255 files limit to be sure + for (; preCalcPart < 255; preCalcPart++) + { + sprintf(sPreCalcFileName, "%s.%d", sPrecalcPathName.c_str(), preCalcPart); + string sReturnPreCalcPath(sPreCalcFileName); + - unsigned int fileLen = 0; ++ long fileLen = 0; + + FILE* file = fopen(sReturnPreCalcPath.c_str(), "ab"); + if(file!=NULL) + { + fileLen = GetFileLen(file); + long unsigned int nextFileLen = fileLen + (sizeof(uint64) * (m_nRainbowChainLen-1)); + // Rotate to next file if we are going to pass 2GB filesize + if (nextFileLen < ((unsigned)2 * 1024 * 1024 * 1024)) + { + // We might want to vPrecalcFiles.push_back(sReturnPreCalcPath) if we just created this file + // We don't as only newly generated chainwalksets will be stored to this new file, so we don't have to look there + if (debug) printf("Debug: Using for precalc: %s\n", sReturnPreCalcPath.c_str()); + fclose(file); + return sReturnPreCalcPath; + } + fclose(file); + } + } + + return string(""); + } + + void CChainWalkSet::updateUsedPrecalcFiles() + { + // we might also use this function to search a wildcard path of precalc files + vPrecalcFiles.clear(); + char sPreCalcFileName[255]; + + int i; + // 255 files max + for (i = 0; i < 255; i++) + { + sprintf(sPreCalcFileName, "%s.%d", sPrecalcPathName.c_str(), i); + string sTryPreCalcPath(sPreCalcFileName); + FILE* file = fopen(sTryPreCalcPath.c_str(), "rb"); + if(file!=NULL) { + vPrecalcFiles.push_back(sTryPreCalcPath); + fclose(file); + } + else { + break; + } + } + } + + void CChainWalkSet::removePrecalcFiles() + { + if (debug) printf("Debug: Removing precalc files.\n"); + updateUsedPrecalcFiles(); + string sCurrentPrecalcPathName = ""; + string sCurrentPrecalcIndexPathName = ""; + + int i; + for (i = 0; i < (int)vPrecalcFiles.size(); i++) + { + sCurrentPrecalcPathName = vPrecalcFiles[i]; + sCurrentPrecalcIndexPathName = sCurrentPrecalcPathName + ".index"; + + if (debug) printf("Debug: Removing precalc file: %s\n", sCurrentPrecalcPathName.c_str()); + + if (remove(sCurrentPrecalcPathName.c_str()) != 0) + if (debug) printf("Debug: Failed removing precalc file: %s\n", sCurrentPrecalcPathName.c_str()); + + if (debug) printf("Debug: Removing precalc index file: %s\n", sCurrentPrecalcIndexPathName.c_str()); + + if (remove(sCurrentPrecalcIndexPathName.c_str()) != 0) + if (debug) printf("Debug: Failed removing precalc index file: %s\n", sCurrentPrecalcIndexPathName.c_str()); + + } + } + + bool CChainWalkSet::FindInFile(uint64* pIndexE, unsigned char* pHash, int nHashLen) + { + int gotPrecalcOnLine = -1; + char precalculationLine[255]; + sprintf(precalculationLine, "%s_%s#%d-%d_%d_%d:%s\n", m_sHashRoutineName.c_str(), m_sPlainCharsetName.c_str(), m_nPlainLenMin, m_nPlainLenMax, m_nRainbowTableIndex, m_nRainbowChainLen, HexToStr(pHash, nHashLen).c_str() ); + string precalcString(precalculationLine); + + string sCurrentPrecalcPathName = ""; + string sCurrentPrecalcIndexPathName = ""; + long unsigned int offset; + + int i; + for (i = 0; i < (int)vPrecalcFiles.size() && gotPrecalcOnLine == -1; i++) + { + sCurrentPrecalcPathName = vPrecalcFiles[i]; + sCurrentPrecalcIndexPathName = sCurrentPrecalcPathName + ".index"; + + offset = 0; + + vector precalcLines; + if (ReadLinesFromFile(sCurrentPrecalcIndexPathName.c_str(), precalcLines)) + { + int j; + for (j = 0; j < (int)precalcLines.size(); j++) + { + if (precalcString.compare(0, precalcString.size()-1, precalcLines[j]) == 0) + { + gotPrecalcOnLine = j; + break; + } + + // Parse + vector vPart; + if (SeperateString(precalcLines[j], "___:", vPart)) + { + // add to offset + offset += ((atoi(vPart[3].c_str())-1) * sizeof(uint64)); + } + else { + // corrupt file + printf("Corrupted precalculation file!\n"); + gotPrecalcOnLine = -1; + break; + } + } + } + } + + if (gotPrecalcOnLine > -1) + { + if (debug) printf("Debug: Reading pre calculations from file, line %d offset %lu\n", gotPrecalcOnLine, offset); + + FILE* fp = fopen(sCurrentPrecalcPathName.c_str(), "rb"); + + if (fp!=NULL) { + fseek(fp, offset, SEEK_SET); + + // We should do some verification here, for example by recalculating the middle chain, to catch corrupted files + if(fread(pIndexE, sizeof(uint64), (unsigned long)m_nRainbowChainLen-1, fp) != (unsigned long)m_nRainbowChainLen-1) + printf("File read error."); + fclose(fp); + } + else + printf("Cannot open precalculation file %s.\n", sCurrentPrecalcPathName.c_str()); + + //printf("\npIndexE[0]: %s\n", uint64tostr(pIndexE[0]).c_str()); + //printf("\npIndexE[nRainbowChainLen-2]: %s\n", uint64tostr(pIndexE[m_nRainbowChainLen-2]).c_str()); + + return true; + } + + return false; + } + + void CChainWalkSet::StoreToFile(uint64* pIndexE, unsigned char* pHash, int nHashLen) + { + if (debug) printf("\nDebug: Storing precalc\n"); + + string sCurrentPrecalcPathName = CheckOrRotatePreCalcFile(); + string sCurrentPrecalcIndexPathName = sCurrentPrecalcPathName + ".index"; + + FILE* fp = fopen(sCurrentPrecalcPathName.c_str(), "ab"); + if(fp!=NULL) + { + if(fwrite(pIndexE, sizeof(uint64), (unsigned long)m_nRainbowChainLen-1, fp) != (unsigned long)m_nRainbowChainLen-1) + printf("File write error."); + else + { + FILE* file = fopen(sCurrentPrecalcIndexPathName.c_str(), "a"); + if (file!=NULL) + { + char precalculationLine[255]; + sprintf(precalculationLine, "%s_%s#%d-%d_%d_%d:%s\n", m_sHashRoutineName.c_str(), m_sPlainCharsetName.c_str(), m_nPlainLenMin, m_nPlainLenMax, m_nRainbowTableIndex, m_nRainbowChainLen, HexToStr(pHash, nHashLen).c_str() ); + fputs (precalculationLine, file); + fclose (file); + } + } + fclose(fp); + } + else + printf("Cannot open precalculation file %s\n", sCurrentPrecalcPathName.c_str()); + } + + uint64* CChainWalkSet::RequestWalk(unsigned char* pHash, int nHashLen, + string sHashRoutineName, + string sPlainCharsetName, int nPlainLenMin, int nPlainLenMax, + int nRainbowTableIndex, + int nRainbowChainLen, + bool& fNewlyGenerated, + bool setDebug, + string sPrecalc) + { + debug = setDebug; + sPrecalcPathName = sPrecalc; + + if ( m_sHashRoutineName != sHashRoutineName + || m_sPlainCharsetName != sPlainCharsetName + || m_nPlainLenMin != nPlainLenMin + || m_nPlainLenMax != nPlainLenMax + || m_nRainbowTableIndex != nRainbowTableIndex + || m_nRainbowChainLen != nRainbowChainLen) + { + DiscardAll(); + + m_sHashRoutineName = sHashRoutineName; + m_sPlainCharsetName = sPlainCharsetName; + m_nPlainLenMin = nPlainLenMin; + m_nPlainLenMax = nPlainLenMax; + m_nRainbowTableIndex = nRainbowTableIndex; + m_nRainbowChainLen = nRainbowChainLen; + + ChainWalk cw; + memcpy(cw.Hash, pHash, nHashLen); + cw.pIndexE = new uint64[nRainbowChainLen - 1]; + m_lChainWalk.push_back(cw); + + // Only update this list when we search through another rainbow table + updateUsedPrecalcFiles(); + + if (!FindInFile(cw.pIndexE, pHash, nHashLen)) + fNewlyGenerated = true; + else + fNewlyGenerated = false; + return cw.pIndexE; + } + + list::iterator it; + for (it = m_lChainWalk.begin(); it != m_lChainWalk.end(); it++) + { + if (memcmp(it->Hash, pHash, nHashLen) == 0) + { + fNewlyGenerated = false; + return it->pIndexE; + } + } + + ChainWalk cw; + memcpy(cw.Hash, pHash, nHashLen); + cw.pIndexE = new uint64[nRainbowChainLen - 1]; + m_lChainWalk.push_back(cw); + + if (!FindInFile(cw.pIndexE, pHash, nHashLen)) + fNewlyGenerated = true; + else + fNewlyGenerated = false; + return cw.pIndexE; + } + + void CChainWalkSet::DiscardWalk(uint64* pIndexE) + { + list::iterator it; + for (it = m_lChainWalk.begin(); it != m_lChainWalk.end(); it++) + { + if (it->pIndexE == pIndexE) + { + delete it->pIndexE; + m_lChainWalk.erase(it); + return; + } + } + + printf("debug: pIndexE not found\n"); + } diff --cc Client Applications/rcracki_mt/CrackEngine.cpp index d36ebbc,0a4b181..0343588 --- a/Client Applications/rcracki_mt/CrackEngine.cpp +++ b/Client Applications/rcracki_mt/CrackEngine.cpp @@@ -1,1437 -1,1441 +1,1451 @@@ -/* - * rcracki_mt is a multithreaded implementation and fork of the original - * RainbowCrack - * - * Copyright (C) Zhu Shuanglei - * Copyright Martin Westergaard Jørgensen - * Copyright 2009, 2010 Daniël Niggebrugge - * Copyright 2009, 2010 James Nobis - * Copyright 2010 uroskn - * - * This file is part of rcracki_mt. - * - * rcracki_mt is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 2 of the License, or - * (at your option) any later version. - * - * rcracki_mt is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with rcracki_mt. If not, see . - */ - -#if defined(_WIN32) && !defined(__GNUC__) - #pragma warning(disable : 4786 4267 4018) -#endif - -#include "CrackEngine.h" -#include "RTI2Reader.h" - -#ifndef _WIN32 - #include -#endif - -CCrackEngine::CCrackEngine() -{ - ResetStatistics(); - writeOutput = false; - resumeSession = false; - debug = false; - keepPrecalcFiles = false; - - sSessionPathName = ""; - sProgressPathName = ""; -} - -CCrackEngine::~CCrackEngine() -{ -} - -////////////////////////////////////////////////////////////////////// - -void CCrackEngine::ResetStatistics() -{ - m_fTotalDiskAccessTime = 0.0f; - m_fTotalCryptanalysisTime = 0.0f; - m_fTotalPrecalculationTime = 0.0f; - m_nTotalChainWalkStep = 0; - m_nTotalFalseAlarm = 0; - m_nTotalChainWalkStepDueToFalseAlarm = 0; -// m_nTotalFalseAlarmSkipped = 0; -} - -int CCrackEngine::BinarySearchOld(RainbowChainO* pChain, int nRainbowChainCount, uint64 nIndex) -{ - int nLow = 0; - int nHigh = nRainbowChainCount - 1; - while (nLow <= nHigh) - { - int nMid = (nLow + nHigh) / 2; - if (nIndex == pChain[nMid].nIndexE) - return nMid; - else if (nIndex < pChain[nMid].nIndexE) - nHigh = nMid - 1; - else - nLow = nMid + 1; - } - - return -1; -} - -RainbowChain *CCrackEngine::BinarySearch(RainbowChain *pChain, int nChainCountRead, uint64 nIndex, IndexChain *pIndex, int nIndexSize, int nIndexStart) -{ - uint64 nPrefix = nIndex >> 16; - int nLow, nHigh; - bool found = false; - //int nChains = 0; - - if(nPrefix > (pIndex[nIndexSize-1].nPrefix & 0x000000FFFFFFFFFFULL)) // check if its in the index file - { - return NULL; - } - - int nBLow = 0; - int nBHigh = nIndexSize - 1; - while (nBLow <= nBHigh) - { - int nBMid = (nBLow + nBHigh) / 2; - if (nPrefix == (pIndex[nBMid].nPrefix & 0x000000FFFFFFFFFFULL)) - { - //nLow = nChains; - //int nChains = 0; - - nLow = pIndex[nBMid].nFirstChain; - nHigh = nLow + pIndex[nBMid].nChainCount; - if(nLow >= nIndexStart && nLow <= nIndexStart + nChainCountRead) - { - if(nHigh > nIndexStart + nChainCountRead) - nHigh = nIndexStart + nChainCountRead; - } - else if(nLow < nIndexStart && nHigh >= nIndexStart) - { - nLow = nIndexStart; - } - else break; - found = true; - break; - } - else if (nPrefix < (pIndex[nBMid].nPrefix & 0x000000FFFFFFFFFFULL)) - nBHigh = nBMid - 1; - else - nBLow = nBMid + 1; - } - if(found == true) - { - for(int i = nLow - nIndexStart; i < nHigh - nIndexStart; i++) - { - int nSIndex = ((int)nIndex) & 0x0000FFFF; - - if (nSIndex == pChain[i].nIndexE) - { - return &pChain[i]; - } - else if(pChain[i].nIndexE > nSIndex) - break; - } - } - return NULL; -} - -// not used currently, leaving code for future checkpoints -//bool CCrackEngine::CheckAlarm(RainbowChain* pChain, int nGuessedPos, unsigned char* pHash, CHashSet& hs) -//{ -// CChainWalkContext cwc; -// //uint64 nIndexS = pChain->nIndexS >> 16; -// uint64 nIndexS = pChain->nIndexS & 0x0000FFFFFFFFFFFFULL; // for first 6 bytes -// cwc.SetIndex(nIndexS); -// int nPos; -// for (nPos = 0; nPos < nGuessedPos; nPos++) -// { -// cwc.IndexToPlain(); -// cwc.PlainToHash(); -// cwc.HashToIndex(nPos); -// // Not using checkpoints atm -// /* -// switch(nPos) -// { -// case 5000: -// if((cwc.GetIndex() & 0x00000001) != (pChain->nCheckPoint & 0x00000080) >> 7) -// { -// m_nTotalFalseAlarmSkipped += 10000 - 5000; -//// printf("CheckPoint caught false alarm at position 7600\n"); -// return false; -// } -// break; -// case 6000: -// if((cwc.GetIndex() & 0x00000001) != (pChain->nCheckPoint & 0x00000040) >> 6) -// { -//// printf("CheckPoint caught false alarm at position 8200\n"); -// m_nTotalFalseAlarmSkipped += 10000 - 6000; -// return false; -// } -// break; -// -// case 7600: -// if((cwc.GetIndex() & 0x00000001) != (pChain->nCheckPoint & 0x00000020) >> 5) -// { -//// printf("CheckPoint caught false alarm at position 8700\n"); -// m_nTotalFalseAlarmSkipped += 10000 - 7600; -// return false; -// } -// break; -// -// case 8200: -// if((cwc.GetIndex() & 0x00000001) != (pChain->nCheckPoint & 0x00000010) >> 4) -// { -//// printf("CheckPoint caught false alarm at position 9000\n"); -// m_nTotalFalseAlarmSkipped += 10000 - 8200; -// return false; -// } -// break; -// -// case 8700: -// if((cwc.GetIndex() & 0x00000001) != (pChain->nCheckPoint & 0x00000008) >> 3) -// { -//// printf("CheckPoint caught false alarm at position 9300\n"); -// m_nTotalFalseAlarmSkipped += 10000 - 8700; -// return false; -// } -// -// break; -// case 9000: -// if((cwc.GetIndex() & 0x00000001) != (pChain->nCheckPoint & 0x00000004) >> 2) -// { -//// printf("CheckPoint caught false alarm at position 9600\n"); -// m_nTotalFalseAlarmSkipped += 10000 - 9000; -// return false; -// } -// -// break; -// case 9300: -// if((cwc.GetIndex() & 0x00000001) != (pChain->nCheckPoint & 0x00000002) >> 1) -// { -//// printf("CheckPoint caught false alarm at position 9600\n"); -// m_nTotalFalseAlarmSkipped += 10000 - 9300; -// return false; -// } -// break; -// case 9600: -// if((cwc.GetIndex() & 0x00000001) != (pChain->nCheckPoint & 0x00000001)) -// { -//// printf("CheckPoint caught false alarm at position 9600\n"); -// m_nTotalFalseAlarmSkipped += 10000 - 9600; -// return false; -// } -// break; -// -// }*/ -// } -// cwc.IndexToPlain(); -// cwc.PlainToHash(); -// if (cwc.CheckHash(pHash)) -// { -// printf("plaintext of %s is %s\n", cwc.GetHash().c_str(), cwc.GetPlain().c_str()); -// hs.SetPlain(cwc.GetHash(), cwc.GetPlain(), cwc.GetBinary()); -// return true; -// } -// -// return false; -//} - -//bool CCrackEngine::CheckAlarmOld(RainbowChainO* pChain, int nGuessedPos, unsigned char* pHash, CHashSet& hs) -//{ -// CChainWalkContext cwc; -// cwc.SetIndex(pChain->nIndexS); -// int nPos; -// for (nPos = 0; nPos < nGuessedPos; nPos++) -// { -// cwc.IndexToPlain(); -// cwc.PlainToHash(); -// cwc.HashToIndex(nPos); -// } -// cwc.IndexToPlain(); -// cwc.PlainToHash(); -// if (cwc.CheckHash(pHash)) -// { -// printf("plaintext of %s is %s\n", cwc.GetHash().c_str(), cwc.GetPlain().c_str()); -// hs.SetPlain(cwc.GetHash(), cwc.GetPlain(), cwc.GetBinary()); -// return true; -// } -// -// return false; -//} - -void CCrackEngine::GetChainIndexRangeWithSameEndpoint(RainbowChainO* pChain, - int nRainbowChainCount, - int nMatchingIndexE, - int& nMatchingIndexEFrom, - int& nMatchingIndexETo) -{ - nMatchingIndexEFrom = nMatchingIndexE; - nMatchingIndexETo = nMatchingIndexE; - while (nMatchingIndexEFrom > 0) - { - if (pChain[nMatchingIndexEFrom - 1].nIndexE == pChain[nMatchingIndexE].nIndexE) - nMatchingIndexEFrom--; - else - break; - } - while (nMatchingIndexETo < nRainbowChainCount - 1) - { - if (pChain[nMatchingIndexETo + 1].nIndexE == pChain[nMatchingIndexE].nIndexE) - nMatchingIndexETo++; - else - break; - } -} - -void CCrackEngine::SearchTableChunkOld(RainbowChainO* pChain, int nRainbowChainLen, int nRainbowChainCount, CHashSet& hs) -{ - vector vHash; - hs.GetLeftHashWithLen(vHash, CChainWalkContext::GetHashLen()); - printf("searching for %lu hash%s...\n", (unsigned long)vHash.size(), - vHash.size() > 1 ? "es" : ""); - - int nChainWalkStep = 0; - int nFalseAlarm = 0; - int nChainWalkStepDueToFalseAlarm = 0; - - vector threadPool; - vector pThreads; - - #ifndef _WIN32 - /* - * On linux you cannot set the priority of a thread in the non real time - * scheduling groups. You can set the priority of the process. In - * windows BELOW_NORMAL represents a 1/8th drop in priority and this would - * be 20 * 1/8 on linux or about 2.5 - */ - setpriority( PRIO_PROCESS, 0, 2 ); - #endif - - pthread_attr_t attr; - pthread_attr_init(&attr); - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); - #ifdef _WIN32 - sched_param param; - /* - * windows scheduling is 0 to 32 (low to high) with 8 as normal and 7 as - * BELOW_NORMAL - */ - param.sched_priority = THREAD_PRIORITY_BELOW_NORMAL; - pthread_attr_setschedparam (&attr, ¶m); - #endif - - bool pausing = false; - - UINT4 nHashIndex; - for (nHashIndex = 0; nHashIndex < vHash.size(); nHashIndex++) - { - #ifdef _WIN32 - if (_kbhit()) - { - int ch = _getch(); - ch = toupper(ch); - if (ch == 'P') - { - pausing = true; - printf( "\nPausing, press P again to continue... "); - - timeval tv; - timeval tv2; - timeval final; - gettimeofday( &tv, NULL ); - - while (pausing) - { - if (_kbhit()) - { - ch = _getch(); - ch = toupper(ch); - if (ch == 'P') - { - printf( " [Continuing]\n"); - pausing = false; - gettimeofday( &tv2, NULL ); - final = sub_timeofday( tv2, tv ); - float fTime = 1.0f * final.tv_sec + 1.0f * final.tv_usec / 1000000; - m_fTotalCryptanalysisTime -= fTime; - } - } - Sleep(500); - } - } - else - { - printf( "\nPress 'P' to pause...\n"); - } - } - #else - int c = tty_getchar(); - if (c >= 0) { - tty_flush(); - if (c==112) { // = p - pausing = true; - printf( "\nPausing, press 'p' again to continue... "); - - timeval tv; - timeval tv2; - timeval final; - gettimeofday( &tv, NULL ); - - while (pausing) - { - if ((c = tty_getchar()) >= 0) - { - tty_flush(); - if (c == 112) - { - printf( " [Continuing]\n"); - pausing = false; - gettimeofday( &tv2, NULL ); - final = sub_timeofday( tv2, tv ); - float fTime = 1.0f * final.tv_sec + 1.0f * final.tv_usec / 1000000; - m_fTotalCryptanalysisTime -= fTime; - } - } - usleep(500*1000); - } - } - else { - printf( "\nPress 'p' to pause...\n"); - } - } - #endif - unsigned char TargetHash[MAX_HASH_LEN]; - int nHashLen; - ParseHash(vHash[nHashIndex], TargetHash, nHashLen); - if (nHashLen != CChainWalkContext::GetHashLen()) - printf("debug: nHashLen mismatch\n"); - - // Rqeuest ChainWalk - bool fNewlyGenerated; - uint64* pStartPosIndexE = m_cws.RequestWalk(TargetHash, - nHashLen, - CChainWalkContext::GetHashRoutineName(), - CChainWalkContext::GetPlainCharsetName(), - CChainWalkContext::GetPlainLenMin(), - CChainWalkContext::GetPlainLenMax(), - CChainWalkContext::GetRainbowTableIndex(), - nRainbowChainLen, - fNewlyGenerated, - debug, - sPrecalcPathName); - //printf("debug: using %s walk for %s\n", fNewlyGenerated ? "newly generated" : "existing", - // vHash[nHashIndex].c_str()); - - // Walk - if (fNewlyGenerated) - { - timeval tv; - timeval tv2; - timeval final; - - gettimeofday( &tv, NULL ); - - printf("Pre-calculating hash %lu of %lu.%-20s\r", - (unsigned long)nHashIndex+1, (unsigned long)vHash.size(), ""); - threadPool.clear(); - pThreads.clear(); - - UINT4 thread_ID; - for (thread_ID = 0; thread_ID < (unsigned long)maxThreads; thread_ID++) - { - rcrackiThread* r_Thread = new rcrackiThread(TargetHash, thread_ID, nRainbowChainLen, maxThreads, pStartPosIndexE); - if (r_Thread) - { - pthread_t pThread; - int returnValue = pthread_create( &pThread, &attr, rcrackiThread::rcrackiThreadStaticEntryPointPthread, (void *) r_Thread); - - if( returnValue != 0 ) - { - printf("pThread creation failed, returnValue: %d\n", returnValue); - } - else - { - pThreads.push_back(pThread); - } - - threadPool.push_back(r_Thread); - } - else - { - printf("r_Thread creation failed!\n"); - } - } - - //printf("%d r_Threads created\t\t\n", threadPool.size()); - - for (thread_ID = 0; thread_ID < threadPool.size(); thread_ID++) - { - pthread_t pThread = pThreads[thread_ID]; - int returnValue = pthread_join(pThread, NULL); - if( returnValue != 0 ) - { - printf("pThread join failed, returnValue: %d\n", returnValue); - } - - rcrackiThread* rThread = threadPool[thread_ID]; - nChainWalkStep += rThread->GetChainWalkStep(); - } - - gettimeofday( &tv2, NULL ); - final = sub_timeofday( tv2, tv ); - - float fTime = 1.0f * final.tv_sec + 1.0f * final.tv_usec / 1000000; - - m_fTotalPrecalculationTime += fTime; - m_fTotalCryptanalysisTime -= fTime; - - printf("%-50s\r", ""); - - if ( debug ) - printf("pre-calculation time: %.2f s\n", fTime); - } - - //printf("Checking false alarms for hash %d of %d.\t\t\r", nHashIndex+1, vHash.size()); - printf("Checking false alarms for hash %lu of %lu.%-20s\r", - (unsigned long)nHashIndex+1, (unsigned long)vHash.size(), ""); - - threadPool.clear(); - pThreads.clear(); - - int i; - for (i = 0; i < maxThreads; i++) - { - rcrackiThread* r_Thread = new rcrackiThread(TargetHash, true); - threadPool.push_back(r_Thread); - } - - UINT4 thread_ID = 0; - int nPos; - for (nPos = nRainbowChainLen - 2; nPos >= 0; nPos--) - { - uint64 nIndexEOfCurPos = pStartPosIndexE[nPos]; - - // Search matching nIndexE - int nMatchingIndexE = BinarySearchOld(pChain, nRainbowChainCount, nIndexEOfCurPos); - if (nMatchingIndexE != -1) - { - int nMatchingIndexEFrom, nMatchingIndexETo; - GetChainIndexRangeWithSameEndpoint(pChain, nRainbowChainCount, - nMatchingIndexE, - nMatchingIndexEFrom, nMatchingIndexETo); - int i; - for (i = nMatchingIndexEFrom; i <= nMatchingIndexETo; i++) - { - rcrackiThread* rThread = threadPool[thread_ID]; - rThread->AddAlarmCheckO(pChain + i, nPos); - if (thread_ID < (unsigned long)maxThreads - 1 ) { - thread_ID++; - } else { - thread_ID = 0; - } - } - } - } - - for (thread_ID = 0; thread_ID < (unsigned long)maxThreads; thread_ID++) - { - rcrackiThread* r_Thread = threadPool[thread_ID]; - pthread_t pThread; - - int returnValue = pthread_create( &pThread, &attr, rcrackiThread::rcrackiThreadStaticEntryPointPthread, (void *) r_Thread); - - if( returnValue != 0 ) - { - printf("pThread creation failed, returnValue: %d\n", returnValue); - } - else - { - pThreads.push_back(pThread); - } - } - - //printf("%d r_Threads created\t\t\n", threadPool.size()); - - bool foundHashInThread = false; - for (thread_ID = 0; thread_ID < threadPool.size(); thread_ID++) - { - rcrackiThread* rThread = threadPool[thread_ID]; - pthread_t pThread = pThreads[thread_ID]; - - int returnValue = pthread_join(pThread, NULL); - if( returnValue != 0 ) - { - printf("pThread join failed, returnValue: %d\n", returnValue); - } - - nChainWalkStepDueToFalseAlarm += rThread->GetChainWalkStepDueToFalseAlarm(); - nFalseAlarm += rThread->GetnFalseAlarm(); - - if (rThread->FoundHash() && !foundHashInThread) { - //printf("\t\t\t\t\t\t\r"); - printf("%-50s\r", ""); - - printf("plaintext of %s is %s\n", rThread->GetHash().c_str(), rThread->GetPlain().c_str()); - if (writeOutput) - { - if (!writeResultLineToFile(outputFile, rThread->GetHash(), rThread->GetPlain(), rThread->GetBinary())) - printf("Couldn't write this result to file!\n"); - } - hs.SetPlain(rThread->GetHash(), rThread->GetPlain(), rThread->GetBinary()); - - FILE* file = fopen(sSessionPathName.c_str(), "a"); - if (file!=NULL) - { - string buffer = "sHash=" + rThread->GetHash() + ":" + rThread->GetBinary() + ":" + rThread->GetPlain() + "\n"; - fputs (buffer.c_str(), file); - fclose (file); - } - - m_cws.DiscardWalk(pStartPosIndexE); - foundHashInThread = true; - } - } - - pThreads.clear(); - threadPool.clear(); - } - - //printf("\t\t\t\t\t\t\t\r"); - printf("%-50s\r", ""); - pThreads.clear(); - threadPool.clear(); - pthread_attr_destroy(&attr); - - //printf("debug: chain walk step: %d\n", nChainWalkStep); - //printf("debug: false alarm: %d\n", nFalseAlarm); - //printf("debug: chain walk step due to false alarm: %d\n", nChainWalkStepDueToFalseAlarm); - - m_nTotalChainWalkStep += nChainWalkStep; - m_nTotalFalseAlarm += nFalseAlarm; - m_nTotalChainWalkStepDueToFalseAlarm += nChainWalkStepDueToFalseAlarm; -} - -void CCrackEngine::SearchTableChunk(RainbowChain* pChain, int nRainbowChainLen, int nRainbowChainCount, CHashSet& hs, IndexChain *pIndex, int nIndexSize, int nChainStart) -{ - vector vHash; - //vector vIndices; - //vector vChains; - hs.GetLeftHashWithLen(vHash, CChainWalkContext::GetHashLen()); - printf("searching for %lu hash%s...\n", (unsigned long)vHash.size(), - vHash.size() > 1 ? "es" : ""); - - int nChainWalkStep = 0; - int nFalseAlarm = 0; - int nChainWalkStepDueToFalseAlarm = 0; - - vector threadPool; - vector pThreads; - - #ifndef _WIN32 - /* - * On linux you cannot set the priority of a thread in the non real time - * scheduling groups. You can set the priority of the process. In - * windows BELOW_NORMAL represents a 1/8th drop in priority and this would - * be 20 * 1/8 on linux or about 2.5 - */ - setpriority( PRIO_PROCESS, 0, 2 ); - #endif - - pthread_attr_t attr; - pthread_attr_init(&attr); - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); - #ifdef _WIN32 - sched_param param; - param.sched_priority = THREAD_PRIORITY_BELOW_NORMAL; - pthread_attr_setschedparam (&attr, ¶m); - #endif - // else set it to 5 or something (for linux)? - - bool pausing = false; - - UINT4 nHashIndex; - for (nHashIndex = 0; nHashIndex < vHash.size(); nHashIndex++) - { - #ifdef _WIN32 - if (_kbhit()) - { - int ch = _getch(); - ch = toupper(ch); - if (ch == 'P') - { - pausing = true; - printf( "\nPausing, press P again to continue... "); - - timeval tv; - timeval tv2; - timeval final; - gettimeofday( &tv, NULL ); - - while (pausing) - { - if (_kbhit()) - { - ch = _getch(); - ch = toupper(ch); - if (ch == 'P') - { - printf( " [Continuing]\n"); - pausing = false; - gettimeofday( &tv2, NULL ); - final = sub_timeofday( tv2, tv ); - float fTime = 1.0f * final.tv_sec + 1.0f * final.tv_usec / 1000000; - m_fTotalCryptanalysisTime -= fTime; - } - } - Sleep(500); - } - } - else - { - printf( "\nPress 'P' to pause...\n"); - } - } - #else - int c = tty_getchar(); - if (c >= 0) { - tty_flush(); - if (c==112) { // = p - pausing = true; - printf( "\nPausing, press 'p' again to continue... "); - - timeval tv; - timeval tv2; - timeval final; - gettimeofday( &tv, NULL ); - - while (pausing) - { - if ((c = tty_getchar()) >= 0) - { - tty_flush(); - if (c == 112) - { - printf( " [Continuing]\n"); - pausing = false; - gettimeofday( &tv2, NULL ); - final = sub_timeofday( tv2, tv ); - float fTime = 1.0f * final.tv_sec + 1.0f * final.tv_usec / 1000000; - m_fTotalCryptanalysisTime -= fTime; - } - } - usleep(500*1000); - } - } - else { - printf( "\nPress 'p' to pause...\n"); - } - } - #endif - unsigned char TargetHash[MAX_HASH_LEN]; - int nHashLen; - ParseHash(vHash[nHashIndex], TargetHash, nHashLen); - if (nHashLen != CChainWalkContext::GetHashLen()) - printf("debug: nHashLen mismatch\n"); - - // Request ChainWalk - bool fNewlyGenerated; -// printf("Requesting walk..."); - - - uint64* pStartPosIndexE = m_cws.RequestWalk(TargetHash, - nHashLen, - CChainWalkContext::GetHashRoutineName(), - CChainWalkContext::GetPlainCharsetName(), - CChainWalkContext::GetPlainLenMin(), - CChainWalkContext::GetPlainLenMax(), - CChainWalkContext::GetRainbowTableIndex(), - nRainbowChainLen, - fNewlyGenerated, - debug, - sPrecalcPathName); -// printf("done!\n"); -// printf("debug: using %s walk for %s\n", fNewlyGenerated ? "newly generated" : "existing", -// vHash[nHashIndex].c_str()); - - if (fNewlyGenerated) - { - timeval tv; - timeval tv2; - timeval final; - - gettimeofday( &tv, NULL ); - - printf("Pre-calculating hash %lu of %lu.%-20s\r", - (unsigned long)nHashIndex+1, (unsigned long)vHash.size(), ""); - threadPool.clear(); - pThreads.clear(); - - UINT4 thread_ID; - for (thread_ID = 0; thread_ID < (unsigned long)maxThreads; thread_ID++) - { - rcrackiThread* r_Thread = new rcrackiThread(TargetHash, thread_ID, nRainbowChainLen, maxThreads, pStartPosIndexE); - if (r_Thread) - { - pthread_t pThread; - int returnValue = pthread_create( &pThread, &attr, rcrackiThread::rcrackiThreadStaticEntryPointPthread, (void *) r_Thread); - - if( returnValue != 0 ) - { - printf("pThread creation failed, returnValue: %d\n", returnValue); - } - else - { - pThreads.push_back(pThread); - } - - threadPool.push_back(r_Thread); - } - else - { - printf("r_Thread creation failed!\n"); - } - } - - //printf("%d r_Threads created\t\t\n", threadPool.size()); - - for (thread_ID = 0; thread_ID < threadPool.size(); thread_ID++) - { - pthread_t pThread = pThreads[thread_ID]; - int returnValue = pthread_join(pThread, NULL); - if( returnValue != 0 ) - { - printf("pThread join failed, returnValue: %d\n", returnValue); - } - - rcrackiThread* rThread = threadPool[thread_ID]; - nChainWalkStep += rThread->GetChainWalkStep(); - delete rThread; - } - - m_cws.StoreToFile(pStartPosIndexE, TargetHash, nHashLen); - gettimeofday( &tv2, NULL ); - final = sub_timeofday( tv2, tv ); - - float fTime = 1.0f * final.tv_sec + 1.0f * final.tv_usec / 1000000; - - m_fTotalPrecalculationTime += fTime; - m_fTotalCryptanalysisTime -= fTime; - - //printf("\npStartPosIndexE[0]: %s\n", uint64tostr(pStartPosIndexE[0]).c_str()); - //printf("\npStartPosIndexE[nRainbowChainLen-2]: %s\n", uint64tostr(pStartPosIndexE[nRainbowChainLen-2]).c_str()); - - printf("%-50s\r", ""); - - if ( debug ) - printf("pre-calculation time: %.2f s\n", fTime); - } - - threadPool.clear(); - pThreads.clear(); - - //printf("Checking false alarms for hash %d of %d.\t\t\r", nHashIndex+1, vHash.size()); - printf("Checking false alarms for hash %lu of %lu.%-20s\r", - (unsigned long)nHashIndex+1, (unsigned long)vHash.size(), ""); - - int i; - for (i = 0; i < maxThreads; i++) - { - rcrackiThread* r_Thread = new rcrackiThread(TargetHash); - threadPool.push_back(r_Thread); - } - - UINT4 thread_ID = 0; - int nPos; - for (nPos = nRainbowChainLen - 2; nPos >= 0; nPos--) - { - uint64 nIndexEOfCurPos = pStartPosIndexE[nPos]; - - // Search matching nIndexE - RainbowChain *pChainFound = BinarySearch(pChain, nRainbowChainCount, nIndexEOfCurPos, pIndex, nIndexSize, nChainStart); - if (pChainFound != NULL) // For perfected indexed tables we only recieve 1 result (huge speed increase!) - { - rcrackiThread* rThread = threadPool[thread_ID]; - rThread->AddAlarmCheck(pChainFound, nPos); - if (thread_ID < (unsigned long)maxThreads - 1 ) { - thread_ID++; - } else { - thread_ID = 0; - } - } - } - - for (thread_ID = 0; thread_ID < (unsigned long)maxThreads; thread_ID++) - { - rcrackiThread* r_Thread = threadPool[thread_ID]; - pthread_t pThread; - - int returnValue = pthread_create( &pThread, &attr, rcrackiThread::rcrackiThreadStaticEntryPointPthread, (void *) r_Thread); - - if( returnValue != 0 ) - { - printf("pThread creation failed, returnValue: %d\n", returnValue); - } - else - { - pThreads.push_back(pThread); - } - } - - //printf("%d r_Threads created\t\t\n", threadPool.size()); - - bool foundHashInThread = false; - for (thread_ID = 0; thread_ID < threadPool.size(); thread_ID++) - { - rcrackiThread* rThread = threadPool[thread_ID]; - pthread_t pThread = pThreads[thread_ID]; - - int returnValue = pthread_join(pThread, NULL); - if( returnValue != 0 ) - { - printf("pThread join failed, returnValue: %d\n", returnValue); - } - - nChainWalkStepDueToFalseAlarm += rThread->GetChainWalkStepDueToFalseAlarm(); - nFalseAlarm += rThread->GetnFalseAlarm(); - - if (rThread->FoundHash() && !foundHashInThread) { - //printf("\t\t\t\t\t\t\r"); - printf("%-50s\r", ""); - printf("plaintext of %s is %s\n", rThread->GetHash().c_str(), rThread->GetPlain().c_str()); - if (writeOutput) - { - if (!writeResultLineToFile(outputFile, rThread->GetHash(), rThread->GetPlain(), rThread->GetBinary())) - printf("Couldn't write this result to file!\n"); - } - hs.SetPlain(rThread->GetHash(), rThread->GetPlain(), rThread->GetBinary()); - - FILE* file = fopen(sSessionPathName.c_str(), "a"); - if (file!=NULL) - { - string buffer = "sHash=" + rThread->GetHash() + ":" + rThread->GetBinary() + ":" + rThread->GetPlain() + "\n"; - fputs (buffer.c_str(), file); - fclose (file); - } - - m_cws.DiscardWalk(pStartPosIndexE); - foundHashInThread = true; - } - //pthread - delete rThread; - } - - pThreads.clear(); - threadPool.clear(); - - //printf("\t\t\t\t\r"); - //printf("pChainFounds: %d\n", pChainsFound.size()); -//NEXT_HASH:; - } - //printf("\t\t\t\t\t\t\t\r"); - printf("%-50s\r", ""); - pThreads.clear(); - threadPool.clear(); - pthread_attr_destroy(&attr); - - //printf("debug: chain walk step: %d\n", nChainWalkStep); - //printf("debug: false alarm: %d\n", nFalseAlarm); - //printf("debug: chain walk step due to false alarm: %d\n", nChainWalkStepDueToFalseAlarm); - - m_nTotalChainWalkStep += nChainWalkStep; - m_nTotalFalseAlarm += nFalseAlarm; - m_nTotalChainWalkStepDueToFalseAlarm += nChainWalkStepDueToFalseAlarm; -} - -void CCrackEngine::SearchRainbowTable(string sPathName, CHashSet& hs) -{ - // Did we already go through this file in this session? - if (resumeSession) - { - vector sessionFinishedPathNames; - if (ReadLinesFromFile(sProgressPathName.c_str(), sessionFinishedPathNames)) - { - UINT4 i; - for (i = 0; i < sessionFinishedPathNames.size(); i++) - { - if (sessionFinishedPathNames[i] == sPathName) - { - printf("Skipping %s\n", sPathName.c_str()); - return; - } - } - } - } - - // FileName -#ifdef _WIN32 - string::size_type nIndex = sPathName.find_last_of('\\'); -#else - string::size_type nIndex = sPathName.find_last_of('/'); -#endif - string sFileName; - if (nIndex != string::npos) - sFileName = sPathName.substr(nIndex + 1); - else - sFileName = sPathName; - - // Info - printf("%s:\n", sFileName.c_str()); - - // Setup - int nRainbowChainLen, nRainbowChainCount; - if (!CChainWalkContext::SetupWithPathName(sPathName, nRainbowChainLen, nRainbowChainCount)) - return; - //printf("keyspace: %llu\n", CChainWalkContext::GetPlainSpaceTotal()); - // Already finished? - if (!hs.AnyHashLeftWithLen(CChainWalkContext::GetHashLen())) - { - printf("this table contains hashes with length %d only\n", CChainWalkContext::GetHashLen()); - return; - } - - // Open - FILE* file = fopen(sPathName.c_str(), "rb"); - if (file != NULL) - { - // File length check - bool doOldFormat = CChainWalkContext::isOldFormat(); - bool doRti2Format = CChainWalkContext::isRti2Format(); - UINT4 sizeOfChain; - bool fVerified = false; - UINT4 nFileLen = GetFileLen(file); - - if (doOldFormat) - sizeOfChain = 16; - else - sizeOfChain = 8; - - //if (nFileLen % 8 != 0 || nRainbowChainCount * 8 != nFileLen) - if ( (nFileLen % sizeOfChain != 0 || nRainbowChainCount * sizeOfChain != nFileLen) && doRti2Format == false ) - printf("file length mismatch\n"); - else - { - fseek(file, 0, SEEK_SET); - timeval tv; - timeval tv2; - timeval final; - - unsigned int bytesForChainWalkSet = hs.GetStatHashTotal() * (nRainbowChainLen-1) * 8; - if (debug) printf("Debug: Saving %u bytes of memory for chainwalkset.\n", bytesForChainWalkSet); - - uint64 nAllocatedSize; - - if (doRti2Format || doOldFormat) - { - RTI2Reader *pReader = NULL; - - if(doRti2Format) { - pReader = new RTI2Reader(sPathName); - - } - - if (debug) - { - if ( doOldFormat ) - printf("Debug: This is a table in the old .rt format.\n"); - else if ( doRti2Format ) - printf("Debug: This is a table in the .rti2 format.\n"); - } - - static CMemoryPool mp(bytesForChainWalkSet, debug, maxMem); - RainbowChainO* pChain = NULL; - if(doRti2Format) { - pChain = (RainbowChainO*)mp.Allocate(pReader->GetChainsLeft() * 16, nAllocatedSize); - } else { - pChain = (RainbowChainO*)mp.Allocate(nFileLen, nAllocatedSize); - } - #ifdef _WIN32 - if (debug) printf("Allocated %I64u bytes, filelen %lu\n", nAllocatedSize, (unsigned long)nFileLen); - #else - if (debug) printf("Allocated %llu bytes, filelen %lu\n", nAllocatedSize, (unsigned long)nFileLen); - #endif - - if (pChain != NULL) - { - nAllocatedSize = nAllocatedSize / sizeOfChain * sizeOfChain; // Round to sizeOfChain boundary - - //fseek(file, 0, SEEK_SET); - //bool fVerified = false; - while (true) // Chunk read loop - { - if ((unsigned long)ftell(file) == nFileLen) - break; - - // Load table chunk - if (debug) printf("reading...\n"); - unsigned int nDataRead = 0; - gettimeofday( &tv, NULL ); - if ( doRti2Format ) - { - nDataRead = nAllocatedSize / 16; - if(pReader->GetChainsLeft() <= 0) // No more data - break; - pReader->ReadChains(nDataRead, (RainbowChain*)pChain); - - nDataRead *= 8; // Convert from chains read to bytes - } - else - { - nDataRead = fread(pChain, 1, nAllocatedSize, file); - } - gettimeofday( &tv2, NULL ); - final = sub_timeofday( tv2, tv ); - - float fTime = 1.0f * final.tv_sec + 1.0f * final.tv_usec / 1000000; - printf("%u bytes read, disk access time: %.2f s\n", nDataRead, fTime); - m_fTotalDiskAccessTime += fTime; - - int nRainbowChainCountRead = nDataRead / 16; - - if(doRti2Format) { - nRainbowChainCountRead = nDataRead / 8; - } - - // Verify table chunk - if (!fVerified) - { - printf("verifying the file...\n"); - - // Chain length test - int nIndexToVerify = nRainbowChainCountRead / 2; - CChainWalkContext cwc; - cwc.SetIndex(pChain[nIndexToVerify].nIndexS); - int nPos; - for (nPos = 0; nPos < nRainbowChainLen - 1; nPos++) - { - cwc.IndexToPlain(); - cwc.PlainToHash(); - cwc.HashToIndex(nPos); - } - if (cwc.GetIndex() != pChain[nIndexToVerify].nIndexE) - { - printf("rainbow chain length verify fail\n"); - break; - } - - // Chain sort test - int i; - for (i = 0; i < nRainbowChainCountRead - 1; i++) - { - if (pChain[i].nIndexE > pChain[i + 1].nIndexE) - break; - } - if (i != nRainbowChainCountRead - 1) - { - printf("this file is not sorted\n"); - break; - } - - fVerified = true; - } - - // Search table chunk - gettimeofday( &tv, NULL ); - SearchTableChunkOld(pChain, nRainbowChainLen, nRainbowChainCountRead, hs); - gettimeofday( &tv2, NULL ); - final = sub_timeofday( tv2, tv ); - fTime = 1.0f * final.tv_sec + 1.0f * final.tv_usec / 1000000; - printf("cryptanalysis time: %.2f s\n", fTime); - m_fTotalCryptanalysisTime += fTime; - - // Already finished? - if (!hs.AnyHashLeftWithLen(CChainWalkContext::GetHashLen())) - break; - } - } - else - printf("memory allocation fail\n"); - - //delete pChain; - } - else - { - static CMemoryPool mpIndex(bytesForChainWalkSet, debug, maxMem); - uint64 nAllocatedSizeIndex; - - //int nIndexSize = 0; - //IndexChain *pIndex = NULL; - - FILE* fIndex = fopen(((string)(sPathName + string(".index"))).c_str(), "rb"); - if(fIndex != NULL) - { - // File length check - unsigned int nFileLenIndex = GetFileLen(fIndex); - //unsigned int nRows = nFileLenIndex / 11; - //unsigned int nSize = nRows * sizeof(IndexChain); - //printf("Debug: 8\n"); - if (nFileLenIndex % 11 != 0) - printf("index file length mismatch (%u bytes)\n", nFileLenIndex); - else - { - //printf("index nSize: %d\n", nSize); - //pIndex = (IndexChain*)new unsigned char[nSize]; - IndexChain *pIndex = (IndexChain*)mpIndex.Allocate(nFileLenIndex, nAllocatedSizeIndex); - #ifdef _WIN32 - if (debug) printf("Debug: Allocated %I64u bytes for index with filelen %u\n", nAllocatedSizeIndex, nFileLenIndex); - #else - if (debug) printf("Debug: Allocated %llu bytes for index with filelen %u\n", nAllocatedSizeIndex, nFileLenIndex); - #endif - - static CMemoryPool mp(bytesForChainWalkSet + nAllocatedSizeIndex, debug, maxMem); - - if (pIndex != NULL && nAllocatedSizeIndex > 0) - { - nAllocatedSizeIndex = nAllocatedSizeIndex / sizeof(IndexChain) * sizeof(IndexChain); // Round to sizeOfIndexChain boundary - - fseek(fIndex, 0, SEEK_SET); - - while ( (unsigned long)ftell(fIndex) != nFileLenIndex ) // Index chunk read loop - { - // Load index chunk -#ifdef _WIN32 - if (debug) printf("Debug: Setting index to 0x00 in memory, %I64u bytes\n", nAllocatedSizeIndex); -#else - if (debug) printf("Debug: Setting index to 0x00 in memory, %llu bytes\n", nAllocatedSizeIndex); -#endif - memset(pIndex, 0x00, nAllocatedSizeIndex); - printf("reading index... "); - gettimeofday( &tv, NULL ); - unsigned int nDataRead = fread(pIndex, 1, nAllocatedSizeIndex, fIndex); - gettimeofday( &tv2, NULL ); - final = sub_timeofday( tv2, tv ); - - float fTime = 1.0f * final.tv_sec + 1.0f * final.tv_usec / 1000000; - printf("%u bytes read, disk access time: %.2f s\n", nDataRead, fTime); - m_fTotalDiskAccessTime += fTime; - - //nIndexSize = nFileLenIndex / 11; - int nIndexChainCountRead = nDataRead / sizeof(IndexChain); - //fclose(fIndex); - unsigned int nCoveredRainbowTableChains = 0; - for(int i = 0; i < nIndexChainCountRead; i++) - { - nCoveredRainbowTableChains += pIndex[i].nChainCount; - } - - //RainbowChain* pChain = (RainbowChain*)mp.Allocate(nFileLen, nAllocatedSize); - RainbowChain* pChain = (RainbowChain*)mp.Allocate(nCoveredRainbowTableChains * sizeOfChain, nAllocatedSize); - #ifdef _WIN32 - if (debug) printf("Debug: Allocated %I64u bytes for %u chains, filelen %lu\n", nAllocatedSize, nCoveredRainbowTableChains, (unsigned long)nFileLen); - #else - if (debug) printf("Debug: Allocated %llu bytes for %u chains, filelen %lu\n", nAllocatedSize, nCoveredRainbowTableChains, (unsigned long)nFileLen); - #endif - - if (pChain != NULL && nAllocatedSize > 0) - { - nAllocatedSize = nAllocatedSize / sizeOfChain * sizeOfChain; // Round to sizeOfChain boundary - - //fseek(file, 0, SEEK_SET); - //bool fVerified = false; - UINT4 nProcessedChains = 0; - while ( (unsigned long)ftell(file) != nFileLen - && nProcessedChains < nCoveredRainbowTableChains ) // Chunk read loop - { - // Load table chunk - if (debug) printf("Debug: Setting pChain to 0x00 in memory\n"); - memset(pChain, 0x00, nAllocatedSize); - printf("reading table... "); - gettimeofday( &tv, NULL ); - unsigned int nDataRead = fread(pChain, 1, nAllocatedSize, file); - gettimeofday( &tv2, NULL ); - final = sub_timeofday( tv2, tv ); - - float fTime = 1.0f * final.tv_sec + 1.0f * final.tv_usec / 1000000; - printf("%u bytes read, disk access time: %.2f s\n", nDataRead, fTime); - m_fTotalDiskAccessTime += fTime; - int nRainbowChainCountRead = nDataRead / sizeOfChain; - // Verify table chunk (Too lazy to implement this) - - if (!fVerified) - { - printf("verifying the file... "); - - // Chain length test - int nIndexToVerify = nRainbowChainCountRead / 2; - CChainWalkContext cwc; - uint64 nIndexS; - nIndexS = pChain[nIndexToVerify].nIndexS & 0x0000FFFFFFFFFFFFULL; // for first 6 bytes - - //printf("nIndexS: %s\n", uint64tostr(nIndexS).c_str()); - cwc.SetIndex(nIndexS); - - int nPos; - for (nPos = 0; nPos < nRainbowChainLen - 1; nPos++) - { - cwc.IndexToPlain(); - cwc.PlainToHash(); - cwc.HashToIndex(nPos); - } - - uint64 nEndPoint = 0; - - //for(int i = 0; i < nIndexSize; i++) - for(int i = 0; i < nIndexChainCountRead; i++) - { - if(nIndexToVerify >= pIndex[i].nFirstChain && nIndexToVerify < pIndex[i].nFirstChain + pIndex[i].nChainCount) // We found the matching index - { // Now we need to seek nIndexToVerify into the chains - nEndPoint += (pIndex[i].nPrefix & 0x000000FFFFFFFFFFULL) << 16; // & 0x000000FFFFFFFFFFULL for first 5 bytes - //printf("nPrefix: %s\n", uint64tostr(pIndex[i].nPrefix & 0x000000FFFFFFFFFF).c_str()); - //printf("nFirstChain: %d\n", pIndex[i].nFirstChain); - //printf("nChainCount: %d\n", pIndex[i].nChainCount); - nEndPoint += pChain[nIndexToVerify].nIndexE; - break; - } - } - - if (cwc.GetIndex() != nEndPoint) - { - printf("rainbow chain length verify fail\n"); - break; - } - - fVerified = true; - printf("ok\n"); - } - - // Search table chunk - gettimeofday( &tv, NULL ); - float preTime = m_fTotalCryptanalysisTime; - - SearchTableChunk(pChain, nRainbowChainLen, nRainbowChainCountRead, hs, pIndex, nIndexChainCountRead, nProcessedChains); - float postTime = m_fTotalCryptanalysisTime; - gettimeofday( &tv2, NULL ); - final = sub_timeofday( tv2, tv ); - - fTime = 1.0f * final.tv_sec + 1.0f * final.tv_usec / 1000000; - printf("cryptanalysis time: %.2f s\n", fTime + postTime - preTime); - m_fTotalCryptanalysisTime += fTime; - nProcessedChains += nRainbowChainCountRead; - // Already finished? - if (!hs.AnyHashLeftWithLen(CChainWalkContext::GetHashLen())) - break; - } - } - else printf("memory allocation failed for rainbow table\n"); - - //delete pChain; - } - } - else printf("memory allocation failed for index\n"); - } - } - else - { - printf("Can't load index\n"); - return; - } - fclose(fIndex); - - //delete pIndex; - } - } - fclose(file); - - if (debug) printf("Debug: writing progress to %s\n", sProgressPathName.c_str()); - FILE* file = fopen(sProgressPathName.c_str(), "a"); - if (file!=NULL) - { - string buffer = sPathName + "\n"; - fputs (buffer.c_str(), file); - fclose (file); - } - } - else - printf("can't open file\n"); -} - -void CCrackEngine::Run(vector vPathName, CHashSet& hs, int i_maxThreads, uint64 i_maxMem, bool resume, bool bDebug) -{ -#ifndef _WIN32 - tty_init(); -#endif - resumeSession = resume; - debug = bDebug; - - maxThreads = i_maxThreads; - maxMem = i_maxMem; - // Reset statistics - ResetStatistics(); - - // Sort vPathName (CChainWalkSet need it) - UINT4 i, j; - for (i = 0; i < vPathName.size() - 1; i++) - for (j = 0; j < vPathName.size() - i - 1; j++) - { - if (vPathName[j] > vPathName[j + 1]) - { - string sTemp; - sTemp = vPathName[j]; - vPathName[j] = vPathName[j + 1]; - vPathName[j + 1] = sTemp; - } - } - - // Run - for (i = 0; i < vPathName.size() && hs.AnyhashLeft(); i++) - { - SearchRainbowTable(vPathName[i], hs); - printf("\n"); - } - - // delete precalc files - if (!keepPrecalcFiles) - m_cws.removePrecalcFiles(); - -#ifndef _WIN32 - tty_done(); -#endif -} - -void CCrackEngine::setOutputFile(string sPathName) -{ - writeOutput = true; - outputFile = sPathName; -} - -void CCrackEngine::setSession(string sSession, string sProgress, string sPrecalc, bool keepPrecalc) -{ - sSessionPathName = sSession; - sProgressPathName = sProgress; - sPrecalcPathName = sPrecalc; - keepPrecalcFiles = keepPrecalc; -} - -float CCrackEngine::GetStatTotalDiskAccessTime() -{ - return m_fTotalDiskAccessTime; -} -/*float CCrackEngine::GetWastedTime() -{ - return m_fIndexTime; -}*/ -float CCrackEngine::GetStatTotalCryptanalysisTime() -{ - return m_fTotalCryptanalysisTime; -} - -float CCrackEngine::GetStatTotalPrecalculationTime() -{ - return m_fTotalPrecalculationTime; -} - -int CCrackEngine::GetStatTotalChainWalkStep() -{ - return m_nTotalChainWalkStep; -} - -int CCrackEngine::GetStatTotalFalseAlarm() -{ - return m_nTotalFalseAlarm; -} - -int CCrackEngine::GetStatTotalChainWalkStepDueToFalseAlarm() -{ - return m_nTotalChainWalkStepDueToFalseAlarm; -} +/* + * rcracki_mt is a multithreaded implementation and fork of the original + * RainbowCrack + * + * Copyright (C) Zhu Shuanglei - * Copyright Martin Westergaard Jørgensen ++ * Copyright 2009, 2010 Martin Westergaard Jørgensen + * Copyright 2009, 2010 Daniël Niggebrugge + * Copyright 2009, 2010 James Nobis + * Copyright 2010 uroskn + * + * This file is part of rcracki_mt. + * + * rcracki_mt is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * rcracki_mt is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with rcracki_mt. If not, see . + */ + +#if defined(_WIN32) && !defined(__GNUC__) + #pragma warning(disable : 4786 4267 4018) +#endif + +#include "CrackEngine.h" +#include "RTI2Reader.h" + +#ifndef _WIN32 + #include +#endif + +CCrackEngine::CCrackEngine() +{ + ResetStatistics(); + writeOutput = false; + resumeSession = false; + debug = false; + keepPrecalcFiles = false; + + sSessionPathName = ""; + sProgressPathName = ""; +} + +CCrackEngine::~CCrackEngine() +{ +} + +////////////////////////////////////////////////////////////////////// + +void CCrackEngine::ResetStatistics() +{ + m_fTotalDiskAccessTime = 0.0f; + m_fTotalCryptanalysisTime = 0.0f; + m_fTotalPrecalculationTime = 0.0f; + m_nTotalChainWalkStep = 0; + m_nTotalFalseAlarm = 0; + m_nTotalChainWalkStepDueToFalseAlarm = 0; +// m_nTotalFalseAlarmSkipped = 0; +} + +int CCrackEngine::BinarySearchOld(RainbowChainO* pChain, int nRainbowChainCount, uint64 nIndex) +{ + int nLow = 0; + int nHigh = nRainbowChainCount - 1; + while (nLow <= nHigh) + { + int nMid = (nLow + nHigh) / 2; + if (nIndex == pChain[nMid].nIndexE) + return nMid; + else if (nIndex < pChain[nMid].nIndexE) + nHigh = nMid - 1; + else + nLow = nMid + 1; + } + + return -1; +} + +RainbowChain *CCrackEngine::BinarySearch(RainbowChain *pChain, int nChainCountRead, uint64 nIndex, IndexChain *pIndex, int nIndexSize, int nIndexStart) +{ + uint64 nPrefix = nIndex >> 16; + int nLow, nHigh; + bool found = false; + //int nChains = 0; + + if(nPrefix > (pIndex[nIndexSize-1].nPrefix & 0x000000FFFFFFFFFFULL)) // check if its in the index file + { + return NULL; + } + + int nBLow = 0; + int nBHigh = nIndexSize - 1; + while (nBLow <= nBHigh) + { + int nBMid = (nBLow + nBHigh) / 2; + if (nPrefix == (pIndex[nBMid].nPrefix & 0x000000FFFFFFFFFFULL)) + { + //nLow = nChains; + //int nChains = 0; + + nLow = pIndex[nBMid].nFirstChain; + nHigh = nLow + pIndex[nBMid].nChainCount; + if(nLow >= nIndexStart && nLow <= nIndexStart + nChainCountRead) + { + if(nHigh > nIndexStart + nChainCountRead) + nHigh = nIndexStart + nChainCountRead; + } + else if(nLow < nIndexStart && nHigh >= nIndexStart) + { + nLow = nIndexStart; + } + else break; + found = true; + break; + } + else if (nPrefix < (pIndex[nBMid].nPrefix & 0x000000FFFFFFFFFFULL)) + nBHigh = nBMid - 1; + else + nBLow = nBMid + 1; + } + if(found == true) + { + for(int i = nLow - nIndexStart; i < nHigh - nIndexStart; i++) + { + int nSIndex = ((int)nIndex) & 0x0000FFFF; + + if (nSIndex == pChain[i].nIndexE) + { + return &pChain[i]; + } + else if(pChain[i].nIndexE > nSIndex) + break; + } + } + return NULL; +} + +// not used currently, leaving code for future checkpoints +//bool CCrackEngine::CheckAlarm(RainbowChain* pChain, int nGuessedPos, unsigned char* pHash, CHashSet& hs) +//{ +// CChainWalkContext cwc; +// //uint64 nIndexS = pChain->nIndexS >> 16; +// uint64 nIndexS = pChain->nIndexS & 0x0000FFFFFFFFFFFFULL; // for first 6 bytes +// cwc.SetIndex(nIndexS); +// int nPos; +// for (nPos = 0; nPos < nGuessedPos; nPos++) +// { +// cwc.IndexToPlain(); +// cwc.PlainToHash(); +// cwc.HashToIndex(nPos); +// // Not using checkpoints atm +// /* +// switch(nPos) +// { +// case 5000: +// if((cwc.GetIndex() & 0x00000001) != (pChain->nCheckPoint & 0x00000080) >> 7) +// { +// m_nTotalFalseAlarmSkipped += 10000 - 5000; +//// printf("CheckPoint caught false alarm at position 7600\n"); +// return false; +// } +// break; +// case 6000: +// if((cwc.GetIndex() & 0x00000001) != (pChain->nCheckPoint & 0x00000040) >> 6) +// { +//// printf("CheckPoint caught false alarm at position 8200\n"); +// m_nTotalFalseAlarmSkipped += 10000 - 6000; +// return false; +// } +// break; +// +// case 7600: +// if((cwc.GetIndex() & 0x00000001) != (pChain->nCheckPoint & 0x00000020) >> 5) +// { +//// printf("CheckPoint caught false alarm at position 8700\n"); +// m_nTotalFalseAlarmSkipped += 10000 - 7600; +// return false; +// } +// break; +// +// case 8200: +// if((cwc.GetIndex() & 0x00000001) != (pChain->nCheckPoint & 0x00000010) >> 4) +// { +//// printf("CheckPoint caught false alarm at position 9000\n"); +// m_nTotalFalseAlarmSkipped += 10000 - 8200; +// return false; +// } +// break; +// +// case 8700: +// if((cwc.GetIndex() & 0x00000001) != (pChain->nCheckPoint & 0x00000008) >> 3) +// { +//// printf("CheckPoint caught false alarm at position 9300\n"); +// m_nTotalFalseAlarmSkipped += 10000 - 8700; +// return false; +// } +// +// break; +// case 9000: +// if((cwc.GetIndex() & 0x00000001) != (pChain->nCheckPoint & 0x00000004) >> 2) +// { +//// printf("CheckPoint caught false alarm at position 9600\n"); +// m_nTotalFalseAlarmSkipped += 10000 - 9000; +// return false; +// } +// +// break; +// case 9300: +// if((cwc.GetIndex() & 0x00000001) != (pChain->nCheckPoint & 0x00000002) >> 1) +// { +//// printf("CheckPoint caught false alarm at position 9600\n"); +// m_nTotalFalseAlarmSkipped += 10000 - 9300; +// return false; +// } +// break; +// case 9600: +// if((cwc.GetIndex() & 0x00000001) != (pChain->nCheckPoint & 0x00000001)) +// { +//// printf("CheckPoint caught false alarm at position 9600\n"); +// m_nTotalFalseAlarmSkipped += 10000 - 9600; +// return false; +// } +// break; +// +// }*/ +// } +// cwc.IndexToPlain(); +// cwc.PlainToHash(); +// if (cwc.CheckHash(pHash)) +// { +// printf("plaintext of %s is %s\n", cwc.GetHash().c_str(), cwc.GetPlain().c_str()); +// hs.SetPlain(cwc.GetHash(), cwc.GetPlain(), cwc.GetBinary()); +// return true; +// } +// +// return false; +//} + +//bool CCrackEngine::CheckAlarmOld(RainbowChainO* pChain, int nGuessedPos, unsigned char* pHash, CHashSet& hs) +//{ +// CChainWalkContext cwc; +// cwc.SetIndex(pChain->nIndexS); +// int nPos; +// for (nPos = 0; nPos < nGuessedPos; nPos++) +// { +// cwc.IndexToPlain(); +// cwc.PlainToHash(); +// cwc.HashToIndex(nPos); +// } +// cwc.IndexToPlain(); +// cwc.PlainToHash(); +// if (cwc.CheckHash(pHash)) +// { +// printf("plaintext of %s is %s\n", cwc.GetHash().c_str(), cwc.GetPlain().c_str()); +// hs.SetPlain(cwc.GetHash(), cwc.GetPlain(), cwc.GetBinary()); +// return true; +// } +// +// return false; +//} + +void CCrackEngine::GetChainIndexRangeWithSameEndpoint(RainbowChainO* pChain, + int nRainbowChainCount, + int nMatchingIndexE, + int& nMatchingIndexEFrom, + int& nMatchingIndexETo) +{ + nMatchingIndexEFrom = nMatchingIndexE; + nMatchingIndexETo = nMatchingIndexE; + while (nMatchingIndexEFrom > 0) + { + if (pChain[nMatchingIndexEFrom - 1].nIndexE == pChain[nMatchingIndexE].nIndexE) + nMatchingIndexEFrom--; + else + break; + } + while (nMatchingIndexETo < nRainbowChainCount - 1) + { + if (pChain[nMatchingIndexETo + 1].nIndexE == pChain[nMatchingIndexE].nIndexE) + nMatchingIndexETo++; + else + break; + } +} + +void CCrackEngine::SearchTableChunkOld(RainbowChainO* pChain, int nRainbowChainLen, int nRainbowChainCount, CHashSet& hs) +{ + vector vHash; + hs.GetLeftHashWithLen(vHash, CChainWalkContext::GetHashLen()); + printf("searching for %lu hash%s...\n", (unsigned long)vHash.size(), + vHash.size() > 1 ? "es" : ""); + + int nChainWalkStep = 0; + int nFalseAlarm = 0; + int nChainWalkStepDueToFalseAlarm = 0; + + vector threadPool; + vector pThreads; + + #ifndef _WIN32 + /* + * On linux you cannot set the priority of a thread in the non real time + * scheduling groups. You can set the priority of the process. In + * windows BELOW_NORMAL represents a 1/8th drop in priority and this would + * be 20 * 1/8 on linux or about 2.5 + */ + setpriority( PRIO_PROCESS, 0, 2 ); + #endif + + pthread_attr_t attr; + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); + #ifdef _WIN32 + sched_param param; + /* + * windows scheduling is 0 to 32 (low to high) with 8 as normal and 7 as + * BELOW_NORMAL + */ + param.sched_priority = THREAD_PRIORITY_BELOW_NORMAL; + pthread_attr_setschedparam (&attr, ¶m); + #endif + + bool pausing = false; + + uint32 nHashIndex; + for (nHashIndex = 0; nHashIndex < vHash.size(); nHashIndex++) + { + #ifdef _WIN32 + if (_kbhit()) + { + int ch = _getch(); + ch = toupper(ch); + if (ch == 'P') + { + pausing = true; + printf( "\nPausing, press P again to continue... "); + + timeval tv; + timeval tv2; + timeval final; + gettimeofday( &tv, NULL ); + + while (pausing) + { + if (_kbhit()) + { + ch = _getch(); + ch = toupper(ch); + if (ch == 'P') + { + printf( " [Continuing]\n"); + pausing = false; + gettimeofday( &tv2, NULL ); + final = sub_timeofday( tv2, tv ); + float fTime = 1.0f * final.tv_sec + 1.0f * final.tv_usec / 1000000; + m_fTotalCryptanalysisTime -= fTime; + } + } + Sleep(500); + } + } + else + { + printf( "\nPress 'P' to pause...\n"); + } + } + #else + int c = tty_getchar(); + if (c >= 0) { + tty_flush(); + if (c==112) { // = p + pausing = true; + printf( "\nPausing, press 'p' again to continue... "); + + timeval tv; + timeval tv2; + timeval final; + gettimeofday( &tv, NULL ); + + while (pausing) + { + if ((c = tty_getchar()) >= 0) + { + tty_flush(); + if (c == 112) + { + printf( " [Continuing]\n"); + pausing = false; + gettimeofday( &tv2, NULL ); + final = sub_timeofday( tv2, tv ); + float fTime = 1.0f * final.tv_sec + 1.0f * final.tv_usec / 1000000; + m_fTotalCryptanalysisTime -= fTime; + } + } + usleep(500*1000); + } + } + else { + printf( "\nPress 'p' to pause...\n"); + } + } + #endif + unsigned char TargetHash[MAX_HASH_LEN]; + int nHashLen; + ParseHash(vHash[nHashIndex], TargetHash, nHashLen); + if (nHashLen != CChainWalkContext::GetHashLen()) + printf("debug: nHashLen mismatch\n"); + + // Rqeuest ChainWalk + bool fNewlyGenerated; + uint64* pStartPosIndexE = m_cws.RequestWalk(TargetHash, + nHashLen, + CChainWalkContext::GetHashRoutineName(), + CChainWalkContext::GetPlainCharsetName(), + CChainWalkContext::GetPlainLenMin(), + CChainWalkContext::GetPlainLenMax(), + CChainWalkContext::GetRainbowTableIndex(), + nRainbowChainLen, + fNewlyGenerated, + debug, + sPrecalcPathName); + //printf("debug: using %s walk for %s\n", fNewlyGenerated ? "newly generated" : "existing", + // vHash[nHashIndex].c_str()); + + // Walk + if (fNewlyGenerated) + { + timeval tv; + timeval tv2; + timeval final; + + gettimeofday( &tv, NULL ); + + printf("Pre-calculating hash %lu of %lu.%-20s\r", + (unsigned long)nHashIndex+1, (unsigned long)vHash.size(), ""); + threadPool.clear(); + pThreads.clear(); + + uint32 thread_ID; + for (thread_ID = 0; thread_ID < (unsigned long)maxThreads; thread_ID++) + { + rcrackiThread* r_Thread = new rcrackiThread(TargetHash, thread_ID, nRainbowChainLen, maxThreads, pStartPosIndexE); + if (r_Thread) + { + pthread_t pThread; + int returnValue = pthread_create( &pThread, &attr, rcrackiThread::rcrackiThreadStaticEntryPointPthread, (void *) r_Thread); + + if( returnValue != 0 ) + { + printf("pThread creation failed, returnValue: %d\n", returnValue); + } + else + { + pThreads.push_back(pThread); + } + + threadPool.push_back(r_Thread); + } + else + { + printf("r_Thread creation failed!\n"); + } + } + + //printf("%d r_Threads created\t\t\n", threadPool.size()); + + for (thread_ID = 0; thread_ID < threadPool.size(); thread_ID++) + { + pthread_t pThread = pThreads[thread_ID]; + int returnValue = pthread_join(pThread, NULL); + if( returnValue != 0 ) + { + printf("pThread join failed, returnValue: %d\n", returnValue); + } + + rcrackiThread* rThread = threadPool[thread_ID]; + nChainWalkStep += rThread->GetChainWalkStep(); + } + + gettimeofday( &tv2, NULL ); + final = sub_timeofday( tv2, tv ); + + float fTime = 1.0f * final.tv_sec + 1.0f * final.tv_usec / 1000000; + + m_fTotalPrecalculationTime += fTime; + m_fTotalCryptanalysisTime -= fTime; + + printf("%-50s\r", ""); + + if ( debug ) + printf("pre-calculation time: %.2f s\n", fTime); + } + + //printf("Checking false alarms for hash %d of %d.\t\t\r", nHashIndex+1, vHash.size()); + printf("Checking false alarms for hash %lu of %lu.%-20s\r", + (unsigned long)nHashIndex+1, (unsigned long)vHash.size(), ""); + + threadPool.clear(); + pThreads.clear(); + + int i; + for (i = 0; i < maxThreads; i++) + { + rcrackiThread* r_Thread = new rcrackiThread(TargetHash, true); + threadPool.push_back(r_Thread); + } + + uint32 thread_ID = 0; + int nPos; + for (nPos = nRainbowChainLen - 2; nPos >= 0; nPos--) + { + uint64 nIndexEOfCurPos = pStartPosIndexE[nPos]; + + // Search matching nIndexE + int nMatchingIndexE = BinarySearchOld(pChain, nRainbowChainCount, nIndexEOfCurPos); + if (nMatchingIndexE != -1) + { + int nMatchingIndexEFrom, nMatchingIndexETo; + GetChainIndexRangeWithSameEndpoint(pChain, nRainbowChainCount, + nMatchingIndexE, + nMatchingIndexEFrom, nMatchingIndexETo); + int i; + for (i = nMatchingIndexEFrom; i <= nMatchingIndexETo; i++) + { + rcrackiThread* rThread = threadPool[thread_ID]; + rThread->AddAlarmCheckO(pChain + i, nPos); + if (thread_ID < (unsigned long)maxThreads - 1 ) { + thread_ID++; + } else { + thread_ID = 0; + } + } + } + } + + for (thread_ID = 0; thread_ID < (unsigned long)maxThreads; thread_ID++) + { + rcrackiThread* r_Thread = threadPool[thread_ID]; + pthread_t pThread; + + int returnValue = pthread_create( &pThread, &attr, rcrackiThread::rcrackiThreadStaticEntryPointPthread, (void *) r_Thread); + + if( returnValue != 0 ) + { + printf("pThread creation failed, returnValue: %d\n", returnValue); + } + else + { + pThreads.push_back(pThread); + } + } + + //printf("%d r_Threads created\t\t\n", threadPool.size()); + + bool foundHashInThread = false; + for (thread_ID = 0; thread_ID < threadPool.size(); thread_ID++) + { + rcrackiThread* rThread = threadPool[thread_ID]; + pthread_t pThread = pThreads[thread_ID]; + + int returnValue = pthread_join(pThread, NULL); + if( returnValue != 0 ) + { + printf("pThread join failed, returnValue: %d\n", returnValue); + } + + nChainWalkStepDueToFalseAlarm += rThread->GetChainWalkStepDueToFalseAlarm(); + nFalseAlarm += rThread->GetnFalseAlarm(); + + if (rThread->FoundHash() && !foundHashInThread) { + //printf("\t\t\t\t\t\t\r"); + printf("%-50s\r", ""); + + printf("plaintext of %s is %s\n", rThread->GetHash().c_str(), rThread->GetPlain().c_str()); + if (writeOutput) + { + if (!writeResultLineToFile(outputFile, rThread->GetHash(), rThread->GetPlain(), rThread->GetBinary())) + printf("Couldn't write this result to file!\n"); + } + hs.SetPlain(rThread->GetHash(), rThread->GetPlain(), rThread->GetBinary()); + + FILE* file = fopen(sSessionPathName.c_str(), "a"); + if (file!=NULL) + { + string buffer = "sHash=" + rThread->GetHash() + ":" + rThread->GetBinary() + ":" + rThread->GetPlain() + "\n"; + fputs (buffer.c_str(), file); + fclose (file); + } + + m_cws.DiscardWalk(pStartPosIndexE); + foundHashInThread = true; + } + } + + pThreads.clear(); + threadPool.clear(); + } + + //printf("\t\t\t\t\t\t\t\r"); + printf("%-50s\r", ""); + pThreads.clear(); + threadPool.clear(); + pthread_attr_destroy(&attr); + + //printf("debug: chain walk step: %d\n", nChainWalkStep); + //printf("debug: false alarm: %d\n", nFalseAlarm); + //printf("debug: chain walk step due to false alarm: %d\n", nChainWalkStepDueToFalseAlarm); + + m_nTotalChainWalkStep += nChainWalkStep; + m_nTotalFalseAlarm += nFalseAlarm; + m_nTotalChainWalkStepDueToFalseAlarm += nChainWalkStepDueToFalseAlarm; +} + +void CCrackEngine::SearchTableChunk(RainbowChain* pChain, int nRainbowChainLen, int nRainbowChainCount, CHashSet& hs, IndexChain *pIndex, int nIndexSize, int nChainStart) +{ + vector vHash; + //vector vIndices; + //vector vChains; + hs.GetLeftHashWithLen(vHash, CChainWalkContext::GetHashLen()); + printf("searching for %lu hash%s...\n", (unsigned long)vHash.size(), + vHash.size() > 1 ? "es" : ""); + + int nChainWalkStep = 0; + int nFalseAlarm = 0; + int nChainWalkStepDueToFalseAlarm = 0; + + vector threadPool; + vector pThreads; + + #ifndef _WIN32 + /* + * On linux you cannot set the priority of a thread in the non real time + * scheduling groups. You can set the priority of the process. In + * windows BELOW_NORMAL represents a 1/8th drop in priority and this would + * be 20 * 1/8 on linux or about 2.5 + */ + setpriority( PRIO_PROCESS, 0, 2 ); + #endif + + pthread_attr_t attr; + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); + #ifdef _WIN32 + sched_param param; + param.sched_priority = THREAD_PRIORITY_BELOW_NORMAL; + pthread_attr_setschedparam (&attr, ¶m); + #endif + // else set it to 5 or something (for linux)? + + bool pausing = false; + + uint32 nHashIndex; + for (nHashIndex = 0; nHashIndex < vHash.size(); nHashIndex++) + { + #ifdef _WIN32 + if (_kbhit()) + { + int ch = _getch(); + ch = toupper(ch); + if (ch == 'P') + { + pausing = true; + printf( "\nPausing, press P again to continue... "); + + timeval tv; + timeval tv2; + timeval final; + gettimeofday( &tv, NULL ); + + while (pausing) + { + if (_kbhit()) + { + ch = _getch(); + ch = toupper(ch); + if (ch == 'P') + { + printf( " [Continuing]\n"); + pausing = false; + gettimeofday( &tv2, NULL ); + final = sub_timeofday( tv2, tv ); + float fTime = 1.0f * final.tv_sec + 1.0f * final.tv_usec / 1000000; + m_fTotalCryptanalysisTime -= fTime; + } + } + Sleep(500); + } + } + else + { + printf( "\nPress 'P' to pause...\n"); + } + } + #else + int c = tty_getchar(); + if (c >= 0) { + tty_flush(); + if (c==112) { // = p + pausing = true; + printf( "\nPausing, press 'p' again to continue... "); + + timeval tv; + timeval tv2; + timeval final; + gettimeofday( &tv, NULL ); + + while (pausing) + { + if ((c = tty_getchar()) >= 0) + { + tty_flush(); + if (c == 112) + { + printf( " [Continuing]\n"); + pausing = false; + gettimeofday( &tv2, NULL ); + final = sub_timeofday( tv2, tv ); + float fTime = 1.0f * final.tv_sec + 1.0f * final.tv_usec / 1000000; + m_fTotalCryptanalysisTime -= fTime; + } + } + usleep(500*1000); + } + } + else { + printf( "\nPress 'p' to pause...\n"); + } + } + #endif + unsigned char TargetHash[MAX_HASH_LEN]; + int nHashLen; + ParseHash(vHash[nHashIndex], TargetHash, nHashLen); + if (nHashLen != CChainWalkContext::GetHashLen()) + printf("debug: nHashLen mismatch\n"); + + // Request ChainWalk + bool fNewlyGenerated; +// printf("Requesting walk..."); + + + uint64* pStartPosIndexE = m_cws.RequestWalk(TargetHash, + nHashLen, + CChainWalkContext::GetHashRoutineName(), + CChainWalkContext::GetPlainCharsetName(), + CChainWalkContext::GetPlainLenMin(), + CChainWalkContext::GetPlainLenMax(), + CChainWalkContext::GetRainbowTableIndex(), + nRainbowChainLen, + fNewlyGenerated, + debug, + sPrecalcPathName); +// printf("done!\n"); +// printf("debug: using %s walk for %s\n", fNewlyGenerated ? "newly generated" : "existing", +// vHash[nHashIndex].c_str()); + + if (fNewlyGenerated) + { + timeval tv; + timeval tv2; + timeval final; + + gettimeofday( &tv, NULL ); + + printf("Pre-calculating hash %lu of %lu.%-20s\r", + (unsigned long)nHashIndex+1, (unsigned long)vHash.size(), ""); + threadPool.clear(); + pThreads.clear(); + + uint32 thread_ID; + for (thread_ID = 0; thread_ID < (unsigned long)maxThreads; thread_ID++) + { + rcrackiThread* r_Thread = new rcrackiThread(TargetHash, thread_ID, nRainbowChainLen, maxThreads, pStartPosIndexE); + if (r_Thread) + { + pthread_t pThread; + int returnValue = pthread_create( &pThread, &attr, rcrackiThread::rcrackiThreadStaticEntryPointPthread, (void *) r_Thread); + + if( returnValue != 0 ) + { + printf("pThread creation failed, returnValue: %d\n", returnValue); + } + else + { + pThreads.push_back(pThread); + } + + threadPool.push_back(r_Thread); + } + else + { + printf("r_Thread creation failed!\n"); + } + } + + //printf("%d r_Threads created\t\t\n", threadPool.size()); + + for (thread_ID = 0; thread_ID < threadPool.size(); thread_ID++) + { + pthread_t pThread = pThreads[thread_ID]; + int returnValue = pthread_join(pThread, NULL); + if( returnValue != 0 ) + { + printf("pThread join failed, returnValue: %d\n", returnValue); + } + + rcrackiThread* rThread = threadPool[thread_ID]; + nChainWalkStep += rThread->GetChainWalkStep(); + delete rThread; + } + + m_cws.StoreToFile(pStartPosIndexE, TargetHash, nHashLen); + gettimeofday( &tv2, NULL ); + final = sub_timeofday( tv2, tv ); + + float fTime = 1.0f * final.tv_sec + 1.0f * final.tv_usec / 1000000; + + m_fTotalPrecalculationTime += fTime; + m_fTotalCryptanalysisTime -= fTime; + + //printf("\npStartPosIndexE[0]: %s\n", uint64tostr(pStartPosIndexE[0]).c_str()); + //printf("\npStartPosIndexE[nRainbowChainLen-2]: %s\n", uint64tostr(pStartPosIndexE[nRainbowChainLen-2]).c_str()); + + printf("%-50s\r", ""); + + if ( debug ) + printf("pre-calculation time: %.2f s\n", fTime); + } + + threadPool.clear(); + pThreads.clear(); + + //printf("Checking false alarms for hash %d of %d.\t\t\r", nHashIndex+1, vHash.size()); + printf("Checking false alarms for hash %lu of %lu.%-20s\r", + (unsigned long)nHashIndex+1, (unsigned long)vHash.size(), ""); + + int i; + for (i = 0; i < maxThreads; i++) + { + rcrackiThread* r_Thread = new rcrackiThread(TargetHash); + threadPool.push_back(r_Thread); + } + + uint32 thread_ID = 0; + int nPos; + for (nPos = nRainbowChainLen - 2; nPos >= 0; nPos--) + { + uint64 nIndexEOfCurPos = pStartPosIndexE[nPos]; + + // Search matching nIndexE + RainbowChain *pChainFound = BinarySearch(pChain, nRainbowChainCount, nIndexEOfCurPos, pIndex, nIndexSize, nChainStart); + if (pChainFound != NULL) // For perfected indexed tables we only recieve 1 result (huge speed increase!) + { + rcrackiThread* rThread = threadPool[thread_ID]; + rThread->AddAlarmCheck(pChainFound, nPos); + if (thread_ID < (unsigned long)maxThreads - 1 ) { + thread_ID++; + } else { + thread_ID = 0; + } + } + } + + for (thread_ID = 0; thread_ID < (unsigned long)maxThreads; thread_ID++) + { + rcrackiThread* r_Thread = threadPool[thread_ID]; + pthread_t pThread; + + int returnValue = pthread_create( &pThread, &attr, rcrackiThread::rcrackiThreadStaticEntryPointPthread, (void *) r_Thread); + + if( returnValue != 0 ) + { + printf("pThread creation failed, returnValue: %d\n", returnValue); + } + else + { + pThreads.push_back(pThread); + } + } + + //printf("%d r_Threads created\t\t\n", threadPool.size()); + + bool foundHashInThread = false; + for (thread_ID = 0; thread_ID < threadPool.size(); thread_ID++) + { + rcrackiThread* rThread = threadPool[thread_ID]; + pthread_t pThread = pThreads[thread_ID]; + + int returnValue = pthread_join(pThread, NULL); + if( returnValue != 0 ) + { + printf("pThread join failed, returnValue: %d\n", returnValue); + } + + nChainWalkStepDueToFalseAlarm += rThread->GetChainWalkStepDueToFalseAlarm(); + nFalseAlarm += rThread->GetnFalseAlarm(); + + if (rThread->FoundHash() && !foundHashInThread) { + //printf("\t\t\t\t\t\t\r"); + printf("%-50s\r", ""); + printf("plaintext of %s is %s\n", rThread->GetHash().c_str(), rThread->GetPlain().c_str()); + if (writeOutput) + { + if (!writeResultLineToFile(outputFile, rThread->GetHash(), rThread->GetPlain(), rThread->GetBinary())) + printf("Couldn't write this result to file!\n"); + } + hs.SetPlain(rThread->GetHash(), rThread->GetPlain(), rThread->GetBinary()); + + FILE* file = fopen(sSessionPathName.c_str(), "a"); + if (file!=NULL) + { + string buffer = "sHash=" + rThread->GetHash() + ":" + rThread->GetBinary() + ":" + rThread->GetPlain() + "\n"; + fputs (buffer.c_str(), file); + fclose (file); + } + + m_cws.DiscardWalk(pStartPosIndexE); + foundHashInThread = true; + } + //pthread + delete rThread; + } + + pThreads.clear(); + threadPool.clear(); + + //printf("\t\t\t\t\r"); + //printf("pChainFounds: %d\n", pChainsFound.size()); +//NEXT_HASH:; + } + //printf("\t\t\t\t\t\t\t\r"); + printf("%-50s\r", ""); + pThreads.clear(); + threadPool.clear(); + pthread_attr_destroy(&attr); + + //printf("debug: chain walk step: %d\n", nChainWalkStep); + //printf("debug: false alarm: %d\n", nFalseAlarm); + //printf("debug: chain walk step due to false alarm: %d\n", nChainWalkStepDueToFalseAlarm); + + m_nTotalChainWalkStep += nChainWalkStep; + m_nTotalFalseAlarm += nFalseAlarm; + m_nTotalChainWalkStepDueToFalseAlarm += nChainWalkStepDueToFalseAlarm; +} + +void CCrackEngine::SearchRainbowTable(string sPathName, CHashSet& hs) +{ + // Did we already go through this file in this session? + if (resumeSession) + { + vector sessionFinishedPathNames; + if (ReadLinesFromFile(sProgressPathName.c_str(), sessionFinishedPathNames)) + { + uint32 i; + for (i = 0; i < sessionFinishedPathNames.size(); i++) + { + if (sessionFinishedPathNames[i] == sPathName) + { + printf("Skipping %s\n", sPathName.c_str()); + return; + } + } + } + } + + // FileName +#ifdef _WIN32 + string::size_type nIndex = sPathName.find_last_of('\\'); +#else + string::size_type nIndex = sPathName.find_last_of('/'); +#endif + string sFileName; + if (nIndex != string::npos) + sFileName = sPathName.substr(nIndex + 1); + else + sFileName = sPathName; + + // Info + printf("%s:\n", sFileName.c_str()); + + // Setup + int nRainbowChainLen, nRainbowChainCount; + if (!CChainWalkContext::SetupWithPathName(sPathName, nRainbowChainLen, nRainbowChainCount)) + return; + //printf("keyspace: %llu\n", CChainWalkContext::GetPlainSpaceTotal()); + // Already finished? + if (!hs.AnyHashLeftWithLen(CChainWalkContext::GetHashLen())) + { + printf("this table contains hashes with length %d only\n", CChainWalkContext::GetHashLen()); + return; + } + + // Open + FILE* file = fopen(sPathName.c_str(), "rb"); + if (file != NULL) + { + // File length check + bool doOldFormat = CChainWalkContext::isOldFormat(); + bool doRti2Format = CChainWalkContext::isRti2Format(); + uint32 sizeOfChain; + bool fVerified = false; + long nFileLen = GetFileLen(file); + + if (doOldFormat) + sizeOfChain = 16; + else + sizeOfChain = 8; + + //if (nFileLen % 8 != 0 || nRainbowChainCount * 8 != nFileLen) + if ( ( (unsigned long)nFileLen % sizeOfChain != 0 || nRainbowChainCount * sizeOfChain != (unsigned long)nFileLen) && doRti2Format == false ) + printf("file length mismatch\n"); + else + { + fseek(file, 0, SEEK_SET); + timeval tv; + timeval tv2; + timeval final; + + unsigned int bytesForChainWalkSet = hs.GetStatHashTotal() * (nRainbowChainLen-1) * 8; + if (debug) printf("Debug: Saving %u bytes of memory for chainwalkset.\n", bytesForChainWalkSet); + + uint64 nAllocatedSize; + + if (doRti2Format || doOldFormat) + { + RTI2Reader *pReader = NULL; + + if(doRti2Format) { + pReader = new RTI2Reader(sPathName); + + } + + if (debug) + { + if ( doOldFormat ) + printf("Debug: This is a table in the old .rt format.\n"); + else if ( doRti2Format ) + printf("Debug: This is a table in the .rti2 format.\n"); + } + + static CMemoryPool mp(bytesForChainWalkSet, debug, maxMem); - RainbowChainO* pChain = (RainbowChainO*)mp.Allocate(nFileLen, nAllocatedSize); - #ifdef _WIN32 - if (debug) printf("Allocated %I64u bytes, filelen %lu\n", nAllocatedSize, (unsigned long)nFileLen); ++ RainbowChainO* pChain = NULL; ++ if(doRti2Format) { ++ pChain = (RainbowChainO*)mp.Allocate(pReader->GetChainsLeft() * 16, nAllocatedSize); ++ } else { ++ pChain = (RainbowChainO*)mp.Allocate(nFileLen, nAllocatedSize); ++ } ++ #if defined(_WIN32) && !defined(__GNUC__) ++ if (debug) printf("Allocated %I64 bytes, filelen %ld\n", nAllocatedSize, nFileLen); + #else - if (debug) printf("Allocated %llu bytes, filelen %lu\n", nAllocatedSize, (unsigned long)nFileLen); ++ if (debug) printf("Allocated %llu bytes, filelen %ld\n", nAllocatedSize, nFileLen); + #endif + + if (pChain != NULL) + { + nAllocatedSize = nAllocatedSize / sizeOfChain * sizeOfChain; // Round to sizeOfChain boundary + + //fseek(file, 0, SEEK_SET); + //bool fVerified = false; + while (true) // Chunk read loop + { + if (ftell(file) == nFileLen) + break; + + // Load table chunk + if (debug) printf("reading...\n"); - unsigned int nDataRead = 0, nDataToRead = 0; ++ unsigned int nDataRead = 0; + gettimeofday( &tv, NULL ); + if ( doRti2Format ) + { - nDataToRead = nAllocatedSize / 16; - nDataRead = nDataToRead; ++ nDataRead = nAllocatedSize / 16; ++ if(pReader->GetChainsLeft() <= 0) // No more data ++ break; + pReader->ReadChains(nDataRead, pChain); ++ + nDataRead *= 8; // Convert from chains read to bytes + } + else + { + nDataRead = fread(pChain, 1, nAllocatedSize, file); + } + gettimeofday( &tv2, NULL ); + final = sub_timeofday( tv2, tv ); + + float fTime = 1.0f * final.tv_sec + 1.0f * final.tv_usec / 1000000; + printf("%u bytes read, disk access time: %.2f s\n", nDataRead, fTime); + m_fTotalDiskAccessTime += fTime; + + int nRainbowChainCountRead = nDataRead / 16; + ++ if(doRti2Format) { ++ nRainbowChainCountRead = nDataRead / 8; ++ } ++ + // Verify table chunk + if (!fVerified) + { + printf("verifying the file...\n"); + + // Chain length test + int nIndexToVerify = nRainbowChainCountRead / 2; + CChainWalkContext cwc; + cwc.SetIndex(pChain[nIndexToVerify].nIndexS); + int nPos; + for (nPos = 0; nPos < nRainbowChainLen - 1; nPos++) + { + cwc.IndexToPlain(); + cwc.PlainToHash(); + cwc.HashToIndex(nPos); + } + if (cwc.GetIndex() != pChain[nIndexToVerify].nIndexE) + { + printf("rainbow chain length verify fail\n"); + break; + } + + // Chain sort test + int i; + for (i = 0; i < nRainbowChainCountRead - 1; i++) + { + if (pChain[i].nIndexE > pChain[i + 1].nIndexE) + break; + } + if (i != nRainbowChainCountRead - 1) + { + printf("this file is not sorted\n"); + break; + } + + fVerified = true; + } + + // Search table chunk + gettimeofday( &tv, NULL ); + SearchTableChunkOld(pChain, nRainbowChainLen, nRainbowChainCountRead, hs); + gettimeofday( &tv2, NULL ); + final = sub_timeofday( tv2, tv ); + fTime = 1.0f * final.tv_sec + 1.0f * final.tv_usec / 1000000; + printf("cryptanalysis time: %.2f s\n", fTime); + m_fTotalCryptanalysisTime += fTime; + + // Already finished? + if (!hs.AnyHashLeftWithLen(CChainWalkContext::GetHashLen())) + break; + ++/* ++ // XXX eliminated by PB - check correctness + // finished the current table + if( doRti2Format && nDataToRead > (nDataRead / 8) ) + { + delete pReader; + break; + } ++*/ + } + } + else + printf("memory allocation fail\n"); + + //delete pChain; + } + else + { + static CMemoryPool mpIndex(bytesForChainWalkSet, debug, maxMem); + uint64 nAllocatedSizeIndex; + + //int nIndexSize = 0; + //IndexChain *pIndex = NULL; + + FILE* fIndex = fopen(((string)(sPathName + string(".index"))).c_str(), "rb"); + if(fIndex != NULL) + { + // File length check - unsigned int nFileLenIndex = GetFileLen(fIndex); ++ long nFileLenIndex = GetFileLen(fIndex); + //unsigned int nRows = nFileLenIndex / 11; + //unsigned int nSize = nRows * sizeof(IndexChain); + //printf("Debug: 8\n"); + if (nFileLenIndex % 11 != 0) - printf("index file length mismatch (%u bytes)\n", nFileLenIndex); ++ printf("index file length mismatch (%ld bytes)\n", nFileLenIndex); + else + { + //printf("index nSize: %d\n", nSize); + //pIndex = (IndexChain*)new unsigned char[nSize]; + IndexChain *pIndex = (IndexChain*)mpIndex.Allocate(nFileLenIndex, nAllocatedSizeIndex); + #ifdef _WIN32 - if (debug) printf("Debug: Allocated %I64u bytes for index with filelen %u\n", nAllocatedSizeIndex, nFileLenIndex); ++ if (debug) printf("Debug: Allocated %I64u bytes for index with filelen %ld\n", nAllocatedSizeIndex, nFileLenIndex); + #else - if (debug) printf("Debug: Allocated %llu bytes for index with filelen %u\n", nAllocatedSizeIndex, nFileLenIndex); ++ if (debug) printf("Debug: Allocated %llu bytes for index with filelen %ld\n", nAllocatedSizeIndex, nFileLenIndex); + #endif + + static CMemoryPool mp(bytesForChainWalkSet + nAllocatedSizeIndex, debug, maxMem); + + if (pIndex != NULL && nAllocatedSizeIndex > 0) + { + nAllocatedSizeIndex = nAllocatedSizeIndex / sizeof(IndexChain) * sizeof(IndexChain); // Round to sizeOfIndexChain boundary + + fseek(fIndex, 0, SEEK_SET); + - while ( (unsigned long)ftell(fIndex) != nFileLenIndex ) // Index chunk read loop ++ while ( ftell(fIndex) != nFileLenIndex ) // Index chunk read loop + { + // Load index chunk +#ifdef _WIN32 + if (debug) printf("Debug: Setting index to 0x00 in memory, %I64u bytes\n", nAllocatedSizeIndex); +#else + if (debug) printf("Debug: Setting index to 0x00 in memory, %llu bytes\n", nAllocatedSizeIndex); +#endif + memset(pIndex, 0x00, nAllocatedSizeIndex); + printf("reading index... "); + gettimeofday( &tv, NULL ); + unsigned int nDataRead = fread(pIndex, 1, nAllocatedSizeIndex, fIndex); + gettimeofday( &tv2, NULL ); + final = sub_timeofday( tv2, tv ); + + float fTime = 1.0f * final.tv_sec + 1.0f * final.tv_usec / 1000000; + printf("%u bytes read, disk access time: %.2f s\n", nDataRead, fTime); + m_fTotalDiskAccessTime += fTime; + + //nIndexSize = nFileLenIndex / 11; + int nIndexChainCountRead = nDataRead / sizeof(IndexChain); + //fclose(fIndex); + unsigned int nCoveredRainbowTableChains = 0; + for(int i = 0; i < nIndexChainCountRead; i++) + { + nCoveredRainbowTableChains += pIndex[i].nChainCount; + } + + //RainbowChain* pChain = (RainbowChain*)mp.Allocate(nFileLen, nAllocatedSize); + RainbowChain* pChain = (RainbowChain*)mp.Allocate(nCoveredRainbowTableChains * sizeOfChain, nAllocatedSize); + #ifdef _WIN32 + if (debug) printf("Debug: Allocated %I64u bytes for %u chains, filelen %lu\n", nAllocatedSize, nCoveredRainbowTableChains, (unsigned long)nFileLen); + #else + if (debug) printf("Debug: Allocated %llu bytes for %u chains, filelen %lu\n", nAllocatedSize, nCoveredRainbowTableChains, (unsigned long)nFileLen); + #endif + + if (pChain != NULL && nAllocatedSize > 0) + { + nAllocatedSize = nAllocatedSize / sizeOfChain * sizeOfChain; // Round to sizeOfChain boundary + + //fseek(file, 0, SEEK_SET); + //bool fVerified = false; + uint32 nProcessedChains = 0; - while (ftell(file) != nFileLen ++ while ( ftell(file) != nFileLen + && nProcessedChains < nCoveredRainbowTableChains ) // Chunk read loop + { + // Load table chunk + if (debug) printf("Debug: Setting pChain to 0x00 in memory\n"); + memset(pChain, 0x00, nAllocatedSize); + printf("reading table... "); + gettimeofday( &tv, NULL ); + unsigned int nDataRead = fread(pChain, 1, nAllocatedSize, file); + gettimeofday( &tv2, NULL ); + final = sub_timeofday( tv2, tv ); + + float fTime = 1.0f * final.tv_sec + 1.0f * final.tv_usec / 1000000; + printf("%u bytes read, disk access time: %.2f s\n", nDataRead, fTime); + m_fTotalDiskAccessTime += fTime; + int nRainbowChainCountRead = nDataRead / sizeOfChain; + // Verify table chunk (Too lazy to implement this) + + if (!fVerified) + { + printf("verifying the file... "); + + // Chain length test + int nIndexToVerify = nRainbowChainCountRead / 2; + CChainWalkContext cwc; + uint64 nIndexS; + nIndexS = pChain[nIndexToVerify].nIndexS & 0x0000FFFFFFFFFFFFULL; // for first 6 bytes + + //printf("nIndexS: %s\n", uint64tostr(nIndexS).c_str()); + cwc.SetIndex(nIndexS); + + int nPos; + for (nPos = 0; nPos < nRainbowChainLen - 1; nPos++) + { + cwc.IndexToPlain(); + cwc.PlainToHash(); + cwc.HashToIndex(nPos); + } + + uint64 nEndPoint = 0; + + //for(int i = 0; i < nIndexSize; i++) + for(int i = 0; i < nIndexChainCountRead; i++) + { + if(nIndexToVerify >= pIndex[i].nFirstChain && nIndexToVerify < pIndex[i].nFirstChain + pIndex[i].nChainCount) // We found the matching index + { // Now we need to seek nIndexToVerify into the chains + nEndPoint += (pIndex[i].nPrefix & 0x000000FFFFFFFFFFULL) << 16; // & 0x000000FFFFFFFFFFULL for first 5 bytes + //printf("nPrefix: %s\n", uint64tostr(pIndex[i].nPrefix & 0x000000FFFFFFFFFF).c_str()); + //printf("nFirstChain: %d\n", pIndex[i].nFirstChain); + //printf("nChainCount: %d\n", pIndex[i].nChainCount); + nEndPoint += pChain[nIndexToVerify].nIndexE; + break; + } + } + + if (cwc.GetIndex() != nEndPoint) + { + printf("rainbow chain length verify fail\n"); + break; + } + + fVerified = true; + printf("ok\n"); + } + + // Search table chunk + gettimeofday( &tv, NULL ); + float preTime = m_fTotalCryptanalysisTime; + + SearchTableChunk(pChain, nRainbowChainLen, nRainbowChainCountRead, hs, pIndex, nIndexChainCountRead, nProcessedChains); + float postTime = m_fTotalCryptanalysisTime; + gettimeofday( &tv2, NULL ); + final = sub_timeofday( tv2, tv ); + + fTime = 1.0f * final.tv_sec + 1.0f * final.tv_usec / 1000000; + printf("cryptanalysis time: %.2f s\n", fTime + postTime - preTime); + m_fTotalCryptanalysisTime += fTime; + nProcessedChains += nRainbowChainCountRead; + // Already finished? + if (!hs.AnyHashLeftWithLen(CChainWalkContext::GetHashLen())) + break; + } + } + else printf("memory allocation failed for rainbow table\n"); + + //delete pChain; + } + } + else printf("memory allocation failed for index\n"); + } + } + else + { + printf("Can't load index\n"); + return; + } + fclose(fIndex); + + //delete pIndex; + } + } + fclose(file); + + if (debug) printf("Debug: writing progress to %s\n", sProgressPathName.c_str()); + FILE* file = fopen(sProgressPathName.c_str(), "a"); + if (file!=NULL) + { + string buffer = sPathName + "\n"; + fputs (buffer.c_str(), file); + fclose (file); + } + } + else + printf("can't open file\n"); +} + +void CCrackEngine::Run(vector vPathName, CHashSet& hs, int i_maxThreads, uint64 i_maxMem, bool resume, bool bDebug) +{ +#ifndef _WIN32 + tty_init(); +#endif + resumeSession = resume; + debug = bDebug; + + maxThreads = i_maxThreads; + maxMem = i_maxMem; + // Reset statistics + ResetStatistics(); + + // Sort vPathName (CChainWalkSet need it) + uint32 i, j; + for (i = 0; i < vPathName.size() - 1; i++) + for (j = 0; j < vPathName.size() - i - 1; j++) + { + if (vPathName[j] > vPathName[j + 1]) + { + string sTemp; + sTemp = vPathName[j]; + vPathName[j] = vPathName[j + 1]; + vPathName[j + 1] = sTemp; + } + } + + // Run + for (i = 0; i < vPathName.size() && hs.AnyhashLeft(); i++) + { + SearchRainbowTable(vPathName[i], hs); + printf("\n"); + } + + // delete precalc files + if (!keepPrecalcFiles) + m_cws.removePrecalcFiles(); + +#ifndef _WIN32 + tty_done(); +#endif +} + +void CCrackEngine::setOutputFile(string sPathName) +{ + writeOutput = true; + outputFile = sPathName; +} + +void CCrackEngine::setSession(string sSession, string sProgress, string sPrecalc, bool keepPrecalc) +{ + sSessionPathName = sSession; + sProgressPathName = sProgress; + sPrecalcPathName = sPrecalc; + keepPrecalcFiles = keepPrecalc; +} + +float CCrackEngine::GetStatTotalDiskAccessTime() +{ + return m_fTotalDiskAccessTime; +} +/*float CCrackEngine::GetWastedTime() +{ + return m_fIndexTime; +}*/ +float CCrackEngine::GetStatTotalCryptanalysisTime() +{ + return m_fTotalCryptanalysisTime; +} + +float CCrackEngine::GetStatTotalPrecalculationTime() +{ + return m_fTotalPrecalculationTime; +} + +int CCrackEngine::GetStatTotalChainWalkStep() +{ + return m_nTotalChainWalkStep; +} + +int CCrackEngine::GetStatTotalFalseAlarm() +{ + return m_nTotalFalseAlarm; +} + +int CCrackEngine::GetStatTotalChainWalkStepDueToFalseAlarm() +{ + return m_nTotalChainWalkStepDueToFalseAlarm; +} diff --cc Client Applications/rcracki_mt/HashAlgorithm.cpp index 4206340,db22aeb..4a4cbbb --- a/Client Applications/rcracki_mt/HashAlgorithm.cpp +++ b/Client Applications/rcracki_mt/HashAlgorithm.cpp @@@ -1,420 -1,428 +1,420 @@@ -/* - * rcracki_mt is a multithreaded implementation and fork of the original - * RainbowCrack - * - * Copyright (C) Zhu Shuanglei - * Copyright Martin Westergaard Jørgensen - * Copyright 2009, 2010 Daniël Niggebrugge - * Copyright 2009, 2010 James Nobis - * - * This file is part of rcracki_mt. - * - * rcracki_mt is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 2 of the License, or - * (at your option) any later version. - * - * rcracki_mt is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with rcracki_mt. If not, see . - * - * Changes: not using OpenSSL routines the slow way anymore, as suggested by jci. - */ - -#include "HashAlgorithm.h" - -#include "Public.h" - -#include -//#include -#include -#include -//#include -#include "fast_md5.h" -#include "md4.h" -//#include "sha1.h" -#if defined(_WIN32) && !defined(__GNUC__) - #pragma comment(lib, "libeay32.lib") -#endif - -#ifdef __NetBSD__ - #include -#endif - -#define MSCACHE_HASH_SIZE 16 -void setup_des_key(unsigned char key_56[], des_key_schedule &ks) -{ - des_cblock key; - - key[0] = key_56[0]; - key[1] = (key_56[0] << 7) | (key_56[1] >> 1); - key[2] = (key_56[1] << 6) | (key_56[2] >> 2); - key[3] = (key_56[2] << 5) | (key_56[3] >> 3); - key[4] = (key_56[3] << 4) | (key_56[4] >> 4); - key[5] = (key_56[4] << 3) | (key_56[5] >> 5); - key[6] = (key_56[5] << 2) | (key_56[6] >> 6); - key[7] = (key_56[6] << 1); - - //des_set_odd_parity(&key); - des_set_key(&key, ks); -} - -void HashLM(unsigned char* pPlain, int nPlainLen, unsigned char* pHash) -{ - /* - unsigned char data[7] = {0}; - memcpy(data, pPlain, nPlainLen > 7 ? 7 : nPlainLen); - */ - - int i; - for (i = nPlainLen; i < 7; i++) - pPlain[i] = 0; - - static unsigned char magic[] = {0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25}; - des_key_schedule ks; - //setup_des_key(data, ks); - setup_des_key(pPlain, ks); - des_ecb_encrypt((des_cblock*)magic, (des_cblock*)pHash, ks, DES_ENCRYPT); -} - -void HashLMCHALL(unsigned char* pPlain, int nPlainLen, unsigned char* pHash) -{ - unsigned char pass[14]; - unsigned char pre_lmresp[21]; - static unsigned char magic[] = {0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25}; - static unsigned char spoofed_challange[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}; - des_key_schedule ks; - - memset (pass,0,sizeof(pass)); - memset (pre_lmresp,0,sizeof(pre_lmresp)); - - memcpy (pass,pPlain, nPlainLen); - - setup_des_key(pass, ks); - des_ecb_encrypt((des_cblock*)magic, (des_cblock*)pre_lmresp, ks, DES_ENCRYPT); - - setup_des_key(&pass[7], ks); - des_ecb_encrypt((des_cblock*)magic, (des_cblock*)&pre_lmresp[8], ks, DES_ENCRYPT); - - setup_des_key(pre_lmresp, ks); - des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)pHash, ks, DES_ENCRYPT); - - setup_des_key(&pre_lmresp[7], ks); - des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)&pHash[8], ks, DES_ENCRYPT); - - setup_des_key(&pre_lmresp[14], ks); - des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)&pHash[16], ks, DES_ENCRYPT); - -} - -void HashHALFLMCHALL(unsigned char* pPlain, int nPlainLen, unsigned char* pHash) -{ - unsigned char pre_lmresp[8]; - static unsigned char magic[] = {0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25}; - static unsigned char salt[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}; - - des_key_schedule ks; - unsigned char plain[8] = {0}; - memcpy(plain, pPlain, nPlainLen); - setup_des_key(plain, ks); - des_ecb_encrypt((des_cblock*)magic, (des_cblock*)pre_lmresp, ks, DES_ENCRYPT); - - setup_des_key(pre_lmresp, ks); - des_ecb_encrypt((des_cblock*)salt, (des_cblock*)pHash, ks, DES_ENCRYPT); -} - - - -void HashNTLMCHALL(unsigned char* pPlain, int nPlainLen, unsigned char* pHash) -{ - unsigned char UnicodePlain[MAX_PLAIN_LEN]; - static unsigned char spoofed_challange[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}; - - int len = (nPlainLen < 127) ? nPlainLen : 127; - int i; - - for (i = 0; i < len; i++) - { - UnicodePlain[i * 2] = pPlain[i]; - UnicodePlain[i * 2 + 1] = 0x00; - } - - des_key_schedule ks; - unsigned char lm[21]; - - /*MD4_CTX ctx; - MD4_Init(&ctx); - MD4_Update(&ctx, UnicodePlain, len * 2); - MD4_Final(lm, &ctx); */ - MD4_NEW(UnicodePlain, len * 2, lm); - - //MD4(UnicodePlain, len * 2, lm); - lm[16] = lm[17] = lm[18] = lm[19] = lm[20] = 0; - - setup_des_key(lm, ks); - des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)pHash, ks, DES_ENCRYPT); - - setup_des_key(&lm[7], ks); - des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)&pHash[8], ks, DES_ENCRYPT); - - setup_des_key(&lm[14], ks); - des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)&pHash[16], ks, DES_ENCRYPT); -} - -void HashORACLE(unsigned char* pPlain, int nPlainLen, unsigned char* pHash) -{ - char ToEncrypt[256]; - char temp[256]; - char username[256]; - - DES_cblock iv,iv2; - DES_key_schedule ks1,ks2; - unsigned char deskey_fixed[]={ 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}; - int i,j; -#if defined(_WIN32) && !defined(__GNUC__) - strcpy_s(username, sizeof(username), "SYS"); -#else - strcpy(username, "SYS"); -#endif - int userlen = 3; -#if defined(_WIN32) && !defined(__GNUC__) - _strupr((char*) pPlain); -#else - strupr((char*) pPlain); -#endif - memset (ToEncrypt,0,sizeof(ToEncrypt)); - - for (i=1,j=0; j ascii - 64 */ - "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; - -void _crypt_to64(char *s, unsigned long v, int n) -{ - while (--n >= 0) { - *s++ = itoa64[v&0x3f]; - v >>= 6; - } -} - -void HashPIX(unsigned char* pPlain, int nPlainLen, unsigned char* pHash) -{ - char temp[MD5_DIGEST_LENGTH+1]; - unsigned char final[MD5_DIGEST_LENGTH]; - char* pass = (char*) calloc (nPlainLen+MD5_DIGEST_LENGTH,sizeof(char)); - - memcpy (pass,pPlain,nPlainLen); - - /*MD5_CTX ctx; - MD5_Init(&ctx); - MD5_Update(&ctx, (unsigned char *) pass, MD5_DIGEST_LENGTH); - MD5_Final(final, &ctx);*/ - fast_MD5((unsigned char *) pass, MD5_DIGEST_LENGTH, final); - - char* p = (char*) temp; - _crypt_to64(p,*(unsigned long*) (final+0),4); p += 4; - _crypt_to64(p,*(unsigned long*) (final+4),4); p += 4; - _crypt_to64(p,*(unsigned long*) (final+8),4); p += 4; - _crypt_to64(p,*(unsigned long*) (final+12),4); p += 4; - *p=0; - - memcpy(pHash,temp,MD5_DIGEST_LENGTH); - - free (pass); -} - -#if !defined(_WIN32) || defined(__GNUC__) -char *strupr(char *s1) -{ - char *p = s1; - while(*p) - { - *p = (char) toupper(*p); - p++; - } - return s1; -} -#endif +/* + * rcracki_mt is a multithreaded implementation and fork of the original + * RainbowCrack + * + * Copyright (C) Zhu Shuanglei - * Copyright Martin Westergaard Jørgensen ++ * Copyright 2009, 2010 Martin Westergaard Jørgensen + * Copyright 2009, 2010 Daniël Niggebrugge + * Copyright 2009, 2010 James Nobis + * + * This file is part of rcracki_mt. + * + * rcracki_mt is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * rcracki_mt is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with rcracki_mt. If not, see . + * + * Changes: not using OpenSSL routines the slow way anymore, as suggested by jci. + */ + +#include "HashAlgorithm.h" + +#include "Public.h" + +#include +//#include +#include +#include +//#include +#include "fast_md5.h" +#include "md4.h" +//#include "sha1.h" +#if defined(_WIN32) && !defined(__GNUC__) + #pragma comment(lib, "libeay32.lib") +#endif + +#ifdef __NetBSD__ + #include +#endif + +#define MSCACHE_HASH_SIZE 16 +void setup_des_key(unsigned char key_56[], des_key_schedule &ks) +{ + des_cblock key; + + key[0] = key_56[0]; + key[1] = (key_56[0] << 7) | (key_56[1] >> 1); + key[2] = (key_56[1] << 6) | (key_56[2] >> 2); + key[3] = (key_56[2] << 5) | (key_56[3] >> 3); + key[4] = (key_56[3] << 4) | (key_56[4] >> 4); + key[5] = (key_56[4] << 3) | (key_56[5] >> 5); + key[6] = (key_56[5] << 2) | (key_56[6] >> 6); + key[7] = (key_56[6] << 1); + + //des_set_odd_parity(&key); + des_set_key(&key, ks); +} + +void HashLM(unsigned char* pPlain, int nPlainLen, unsigned char* pHash) +{ + /* + unsigned char data[7] = {0}; + memcpy(data, pPlain, nPlainLen > 7 ? 7 : nPlainLen); + */ + + int i; + for (i = nPlainLen; i < 7; i++) + pPlain[i] = 0; + + static unsigned char magic[] = {0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25}; + des_key_schedule ks; + //setup_des_key(data, ks); + setup_des_key(pPlain, ks); + des_ecb_encrypt((des_cblock*)magic, (des_cblock*)pHash, ks, DES_ENCRYPT); +} + +void HashLMCHALL(unsigned char* pPlain, int nPlainLen, unsigned char* pHash) +{ + unsigned char pass[14]; + unsigned char pre_lmresp[21]; + static unsigned char magic[] = {0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25}; + static unsigned char spoofed_challange[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}; + des_key_schedule ks; + + memset (pass,0,sizeof(pass)); + memset (pre_lmresp,0,sizeof(pre_lmresp)); + + memcpy (pass,pPlain, nPlainLen); + + setup_des_key(pass, ks); + des_ecb_encrypt((des_cblock*)magic, (des_cblock*)pre_lmresp, ks, DES_ENCRYPT); + + setup_des_key(&pass[7], ks); + des_ecb_encrypt((des_cblock*)magic, (des_cblock*)&pre_lmresp[8], ks, DES_ENCRYPT); + + setup_des_key(pre_lmresp, ks); + des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)pHash, ks, DES_ENCRYPT); + + setup_des_key(&pre_lmresp[7], ks); + des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)&pHash[8], ks, DES_ENCRYPT); + + setup_des_key(&pre_lmresp[14], ks); + des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)&pHash[16], ks, DES_ENCRYPT); + +} + +void HashHALFLMCHALL(unsigned char* pPlain, int nPlainLen, unsigned char* pHash) +{ + unsigned char pre_lmresp[8]; + static unsigned char magic[] = {0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25}; + static unsigned char salt[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}; + + des_key_schedule ks; + unsigned char plain[8] = {0}; + memcpy(plain, pPlain, nPlainLen); + setup_des_key(plain, ks); + des_ecb_encrypt((des_cblock*)magic, (des_cblock*)pre_lmresp, ks, DES_ENCRYPT); + + setup_des_key(pre_lmresp, ks); + des_ecb_encrypt((des_cblock*)salt, (des_cblock*)pHash, ks, DES_ENCRYPT); +} + + + +void HashNTLMCHALL(unsigned char* pPlain, int nPlainLen, unsigned char* pHash) +{ + unsigned char UnicodePlain[MAX_PLAIN_LEN]; + static unsigned char spoofed_challange[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}; + + int len = (nPlainLen < 127) ? nPlainLen : 127; + int i; + + for (i = 0; i < len; i++) + { + UnicodePlain[i * 2] = pPlain[i]; + UnicodePlain[i * 2 + 1] = 0x00; + } + + des_key_schedule ks; + unsigned char lm[21]; + + /*MD4_CTX ctx; + MD4_Init(&ctx); + MD4_Update(&ctx, UnicodePlain, len * 2); + MD4_Final(lm, &ctx); */ + MD4_NEW(UnicodePlain, len * 2, lm); + + //MD4(UnicodePlain, len * 2, lm); + lm[16] = lm[17] = lm[18] = lm[19] = lm[20] = 0; + + setup_des_key(lm, ks); + des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)pHash, ks, DES_ENCRYPT); + + setup_des_key(&lm[7], ks); + des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)&pHash[8], ks, DES_ENCRYPT); + + setup_des_key(&lm[14], ks); + des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)&pHash[16], ks, DES_ENCRYPT); +} + +void HashORACLE(unsigned char* pPlain, int nPlainLen, unsigned char* pHash) +{ + char ToEncrypt[256]; + char temp[256]; + char username[256]; + + DES_cblock iv,iv2; + DES_key_schedule ks1,ks2; + unsigned char deskey_fixed[]={ 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}; + int i,j; +#if defined(_WIN32) && !defined(__GNUC__) + strcpy_s(username, sizeof(username), "SYS"); +#else + strcpy(username, "SYS"); +#endif + int userlen = 3; +#if defined(_WIN32) && !defined(__GNUC__) + _strupr((char*) pPlain); +#else + strupr((char*) pPlain); +#endif + memset (ToEncrypt,0,sizeof(ToEncrypt)); + + for (i=1,j=0; j ascii - 64 */ + "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + +void _crypt_to64(char *s, unsigned long v, int n) +{ + while (--n >= 0) { + *s++ = itoa64[v&0x3f]; + v >>= 6; + } +} + +void HashPIX(unsigned char* pPlain, int nPlainLen, unsigned char* pHash) +{ + char temp[MD5_DIGEST_LENGTH+1]; + unsigned char final[MD5_DIGEST_LENGTH]; + char* pass = (char*) calloc (nPlainLen+MD5_DIGEST_LENGTH,sizeof(char)); + + memcpy (pass,pPlain,nPlainLen); + + fast_MD5((unsigned char *) pass, MD5_DIGEST_LENGTH, final); + + char* p = (char*) temp; + _crypt_to64(p,*(unsigned long*) (final+0),4); p += 4; + _crypt_to64(p,*(unsigned long*) (final+4),4); p += 4; + _crypt_to64(p,*(unsigned long*) (final+8),4); p += 4; + _crypt_to64(p,*(unsigned long*) (final+12),4); p += 4; + *p=0; + + memcpy(pHash,temp,MD5_DIGEST_LENGTH); + + free (pass); +} + +#if !defined(_WIN32) || defined(__GNUC__) +char *strupr(char *s1) +{ + char *p = s1; + while(*p) + { + *p = (char) toupper(*p); + p++; + } + return s1; +} +#endif diff --cc Client Applications/rcracki_mt/MemoryPool.cpp index 5c634c0,10e235c..77a4eab --- a/Client Applications/rcracki_mt/MemoryPool.cpp +++ b/Client Applications/rcracki_mt/MemoryPool.cpp @@@ -1,110 -1,110 +1,110 @@@ -/* - * rcracki_mt is a multithreaded implementation and fork of the original - * RainbowCrack - * - * Copyright (C) Zhu Shuanglei - * Copyright Martin Westergaard Jørgensen - * Copyright 2009, 2010 Daniël Niggebrugge - * Copyright 2009, 2010 James Nobis - * Copyright 2010 uroskn - * - * This file is part of rcracki_mt. - * - * rcracki_mt is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 2 of the License, or - * (at your option) any later version. - * - * rcracki_mt is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with rcracki_mt. If not, see . - */ - -#include "MemoryPool.h" -#include "Public.h" - -CMemoryPool::CMemoryPool(unsigned int bytesSaved, bool bDebug, uint64 maxMem) -{ - m_pMem = NULL; - m_nMemSize = 0; - debug = bDebug; - - uint64 nAvailPhys = GetAvailPhysMemorySize(); - - if ( debug ) - { - #ifdef _WIN32 - printf( "Debug: nAvailPhys: %I64u\n", nAvailPhys ); - #else - printf( "Debug: nAvailPhys: %llu\n", nAvailPhys ); - #endif - printf( "Debug: bytesSaved: %d\n", bytesSaved ); - } - - if ( maxMem > 0 && maxMem < nAvailPhys ) - nAvailPhys = maxMem; - - m_nMemMax = nAvailPhys - bytesSaved; // Leave memory for CChainWalkSet - - if (m_nMemMax < 16 * 1024 * 1024) - m_nMemMax = 16 * 1024 * 1024; -} - -CMemoryPool::~CMemoryPool() -{ - if (m_pMem != NULL) - { - delete [] m_pMem; - m_pMem = NULL; - m_nMemSize = 0; - } -} - -unsigned char* CMemoryPool::Allocate(unsigned int nFileLen, uint64& nAllocatedSize) -{ - if (nFileLen <= m_nMemSize) - { - nAllocatedSize = nFileLen; - return m_pMem; - } - - unsigned int nTargetSize; - if (nFileLen < m_nMemMax) - nTargetSize = nFileLen; - else - nTargetSize = m_nMemMax; - - // Free existing memory - if (m_pMem != NULL) - { - delete [] m_pMem; - m_pMem = NULL; - m_nMemSize = 0; - } - - // Allocate new memory - //printf("allocating %u bytes memory\n", nTargetSize); - m_pMem = new (nothrow) unsigned char[nTargetSize]; - while (m_pMem == NULL && nTargetSize >= 32 * 1024 * 1024 ) - { - nTargetSize -= 16 * 1024 * 1024; - m_pMem = new (nothrow) unsigned char[nTargetSize]; - } - - if (m_pMem != NULL) - { - m_nMemSize = nTargetSize; - nAllocatedSize = nTargetSize; - return m_pMem; - } - else - { - m_nMemSize = 0; - nAllocatedSize = 0; - return NULL; - } -} +/* + * rcracki_mt is a multithreaded implementation and fork of the original + * RainbowCrack + * + * Copyright (C) Zhu Shuanglei + * Copyright Martin Westergaard Jørgensen + * Copyright 2009, 2010 Daniël Niggebrugge + * Copyright 2009, 2010 James Nobis + * Copyright 2010 uroskn + * + * This file is part of rcracki_mt. + * + * rcracki_mt is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * rcracki_mt is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with rcracki_mt. If not, see . + */ + +#include "MemoryPool.h" +#include "Public.h" + +CMemoryPool::CMemoryPool(unsigned int bytesSaved, bool bDebug, uint64 maxMem) +{ + m_pMem = NULL; + m_nMemSize = 0; + debug = bDebug; + - uint64 nAvailPhys = GetAvailPhysMemorySize(); ++ unsigned long nAvailPhys = GetAvailPhysMemorySize(); + + if ( debug ) + { - #ifdef _WIN32 ++ #if defined(_WIN32) && !defined(__GNUC__) + printf( "Debug: nAvailPhys: %I64u\n", nAvailPhys ); + #else - printf( "Debug: nAvailPhys: %llu\n", nAvailPhys ); ++ printf( "Debug: nAvailPhys: %lu\n", nAvailPhys ); + #endif + printf( "Debug: bytesSaved: %d\n", bytesSaved ); + } + + if ( maxMem > 0 && maxMem < nAvailPhys ) + nAvailPhys = maxMem; + + m_nMemMax = nAvailPhys - bytesSaved; // Leave memory for CChainWalkSet + + if (m_nMemMax < 16 * 1024 * 1024) + m_nMemMax = 16 * 1024 * 1024; +} + +CMemoryPool::~CMemoryPool() +{ + if (m_pMem != NULL) + { + delete [] m_pMem; + m_pMem = NULL; + m_nMemSize = 0; + } +} + +unsigned char* CMemoryPool::Allocate(unsigned int nFileLen, uint64& nAllocatedSize) +{ + if (nFileLen <= m_nMemSize) + { + nAllocatedSize = nFileLen; + return m_pMem; + } + + unsigned int nTargetSize; + if (nFileLen < m_nMemMax) + nTargetSize = nFileLen; + else + nTargetSize = m_nMemMax; + + // Free existing memory + if (m_pMem != NULL) + { + delete [] m_pMem; + m_pMem = NULL; + m_nMemSize = 0; + } + + // Allocate new memory + //printf("allocating %u bytes memory\n", nTargetSize); + m_pMem = new (nothrow) unsigned char[nTargetSize]; + while (m_pMem == NULL && nTargetSize >= 32 * 1024 * 1024 ) + { + nTargetSize -= 16 * 1024 * 1024; + m_pMem = new (nothrow) unsigned char[nTargetSize]; + } + + if (m_pMem != NULL) + { + m_nMemSize = nTargetSize; + nAllocatedSize = nTargetSize; + return m_pMem; + } + else + { + m_nMemSize = 0; + nAllocatedSize = 0; + return NULL; + } +} diff --cc Client Applications/rcracki_mt/Public.cpp index 946c79d,5324924..941331b --- a/Client Applications/rcracki_mt/Public.cpp +++ b/Client Applications/rcracki_mt/Public.cpp @@@ -1,488 -1,437 +1,488 @@@ -/* - * rcracki_mt is a multithreaded implementation and fork of the original - * RainbowCrack - * - * Copyright (C) Zhu Shuanglei - * Copyright Martin Westergaard Jørgensen - * Copyright 2009, 2010 Daniël Niggebrugge - * Copyright 2009, 2010 James Nobis - * - * This file is part of rcracki_mt. - * - * rcracki_mt is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, version 2 of the License. - * - * rcracki_mt is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with rcracki_mt. If not, see . - */ - -#if defined(_WIN32) && !defined(__GNUC__) - #pragma warning(disable : 4786 4267 4018) -#endif - -#include "Public.h" - -#ifdef _WIN32 - #include -#endif - -#if defined(_WIN32) && !defined(__GNUC__) - #include - #include - - #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) - #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64 - #else - #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL - #endif - - struct timezone - { - int tz_minuteswest; /* minutes W of Greenwich */ - int tz_dsttime; /* type of dst correction */ - }; - - int gettimeofday(struct timeval *tv, struct timezone *tz) - { - // Define a structure to receive the current Windows filetime - FILETIME ft; - - // Initialize the present time to 0 and the timezone to UTC - unsigned __int64 tmpres = 0; - static int tzflag = 0; - - if (NULL != tv) - { - GetSystemTimeAsFileTime(&ft); - - // The GetSystemTimeAsFileTime returns the number of 100 nanosecond - // intervals since Jan 1, 1601 in a structure. Copy the high bits to - // the 64 bit tmpres, shift it left by 32 then or in the low 32 bits. - tmpres |= ft.dwHighDateTime; - tmpres <<= 32; - tmpres |= ft.dwLowDateTime; - - // Convert to microseconds by dividing by 10 - tmpres /= 10; - - // The Unix epoch starts on Jan 1 1970. Need to subtract the difference - // in seconds from Jan 1 1601. - tmpres -= DELTA_EPOCH_IN_MICROSECS; - - // Finally change microseconds to seconds and place in the seconds value. - // The modulus picks up the microseconds. - tv->tv_sec = (long)(tmpres / 1000000UL); - tv->tv_usec = (long)(tmpres % 1000000UL); - } - - if (NULL != tz) - { - if (!tzflag) - { - _tzset(); - tzflag++; - } - - // Adjust for the timezone west of Greenwich - tz->tz_minuteswest = _timezone / 60; - tz->tz_dsttime = _daylight; - } - - return 0; - } - -#elif defined(__APPLE__) || \ - ((defined(__unix__) || defined(unix)) && !defined(USG)) - - #include - - #if defined(BSD) - #include - #elif defined(__linux__) - #include - #else - #error Unsupported Operating System - #endif -#endif - -////////////////////////////////////////////////////////////////////// - -timeval sub_timeofday( timeval tv2, timeval tv ) -{ - timeval final; - - final.tv_usec = tv2.tv_usec - tv.tv_usec; - final.tv_sec = tv2.tv_sec - tv.tv_sec; - - if ( final.tv_usec < 0 ) - { - final.tv_usec += 1000000; - --final.tv_sec; - } - - return final; -} - -unsigned int GetFileLen(FILE* file) -{ - long int pos = ftell(file); - fseek(file, 0, SEEK_END); - long int len = ftell(file); - fseek(file, pos, SEEK_SET); - - return len; -} - -string TrimString(string s) -{ - while (s.size() > 0) - { - if (s[0] == ' ' || s[0] == '\t') - s = s.substr(1); - else - break; - } - - while (s.size() > 0) - { - if (s[s.size() - 1] == ' ' || s[s.size() - 1] == '\t') - s = s.substr(0, s.size() - 1); - else - break; - } - - return s; -} -bool GetHybridCharsets(string sCharset, vector& vCharset) -{ - // Example: hybrid(mixalpha-numeric-all-space#1-6,numeric#1-4) - if(sCharset.substr(0, 6) != "hybrid") // Not hybrid charset - return false; - - string::size_type nEnd = sCharset.rfind(')'); - string::size_type nStart = (int) sCharset.rfind('('); - string sChar = sCharset.substr(nStart + 1, nEnd - nStart - 1); - vector vParts; - SeperateString(sChar, ",", vParts); - for(UINT4 i = 0; i < vParts.size(); i++) - { - tCharset stCharset; - vector vParts2; - SeperateString(vParts[i], "#", vParts2); - stCharset.sName = vParts2[0]; - vector vParts3; - SeperateString(vParts2[1], "-", vParts3); - stCharset.nPlainLenMin = atoi(vParts3[0].c_str()); - stCharset.nPlainLenMax = atoi(vParts3[1].c_str()); - vCharset.push_back(stCharset); - } - return true; -} -bool ReadLinesFromFile(string sPathName, vector& vLine) -{ - vLine.clear(); - - FILE* file = fopen(sPathName.c_str(), "rb"); - if (file != NULL) - { - unsigned int len = GetFileLen(file); - char* data = new char[len + 1]; - fread(data, 1, len, file); - data[len] = '\0'; - string content = data; - content += "\n"; - delete [] data; - - unsigned int i; - for (i = 0; i < content.size(); i++) - { - if (content[i] == '\r') - content[i] = '\n'; - } - - string::size_type n; - while ((n = content.find("\n", 0)) != string::npos) - { - string line = content.substr(0, n); - line = TrimString(line); - if (line != "") - vLine.push_back(line); - content = content.substr(n + 1); - } - - fclose(file); - } - else - return false; - - return true; -} - -bool writeResultLineToFile(string sOutputFile, string sHash, string sPlain, string sBinary) -{ - FILE* file = fopen(sOutputFile.c_str(), "a"); - if (file!=NULL) - { - string buffer = sHash + ":" + sPlain + ":" + sBinary + "\n"; - fputs (buffer.c_str(), file); - fclose (file); - return true; - } - else - return false; -} - -bool SeperateString(string s, string sSeperator, vector& vPart) -{ - vPart.clear(); - - unsigned int i; - for (i = 0; i < sSeperator.size(); i++) - { - string::size_type n; - if ( (n = s.find(sSeperator[i])) != string::npos) - { - vPart.push_back(s.substr(0, n)); - s = s.substr(n + 1); - } - else - { - printf("not found: %c\n", sSeperator[i]); - printf("s: %s\n", s.c_str()); - return false; - } - } - vPart.push_back(s); - - return true; -} - -string uint64tostr(uint64 n) -{ - char str[32]; - -#ifdef _WIN32 - sprintf(str, "%I64u", n); -#else - sprintf(str, "%llu", n); -#endif - - return str; -} - -string uint64tohexstr(uint64 n) -{ - char str[32]; - -#ifdef _WIN32 - sprintf(str, "%016I64x", n); -#else - sprintf(str, "%016llx", n); -#endif - - return str; -} - -string HexToStr(const unsigned char* pData, int nLen) -{ - string sRet; - int i; - for (i = 0; i < nLen; i++) - { - char szByte[3]; - sprintf(szByte, "%02x", pData[i]); - sRet += szByte; - } - - return sRet; -} - -uint64 GetAvailPhysMemorySize() -{ -#if defined(_WIN32) - MEMORYSTATUS ms; - GlobalMemoryStatus(&ms); - return ms.dwAvailPhys; -#elif defined(BSD) - int mib[2] = { CTL_HW, HW_PHYSMEM }; - uint64 physMem; - //XXX warning size_t isn't portable - size_t len; - len = sizeof(physMem); - sysctl(mib, 2, &physMem, &len, NULL, 0); - return physMem; -#elif defined(__linux__) - struct sysinfo info; - sysinfo(&info); - return ( info.freeram + info.bufferram ) * (unsigned long) info.mem_unit; -#else - return 0; - #error Unsupported Operating System -#endif -} - -string GetApplicationPath() -{ - char fullPath[FILENAME_MAX]; - -#ifdef _WIN32 - GetModuleFileName(NULL, fullPath, FILENAME_MAX); -#else - char szTmp[32]; - // XXX linux/proc file system dependen - sprintf(szTmp, "/proc/%d/exe", getpid()); - int bytes = readlink(szTmp, fullPath, FILENAME_MAX); - if(bytes >= 0) - fullPath[bytes] = '\0'; -#endif - - string sApplicationPath = fullPath; -#ifdef _WIN32 - string::size_type nIndex = sApplicationPath.find_last_of('\\'); -#else - string::size_type nIndex = sApplicationPath.find_last_of('/'); -#endif - - if ( nIndex != string::npos ) - sApplicationPath = sApplicationPath.substr(0, nIndex+1); - - //printf ("\n\nDebug: The application directory is %s\n", sApplicationPath.c_str()); - return sApplicationPath; -} - -void ParseHash(string sHash, unsigned char* pHash, int& nHashLen) -{ - UINT4 i; - for (i = 0; i < sHash.size() / 2; i++) - { - string sSub = sHash.substr(i * 2, 2); - int nValue; - sscanf(sSub.c_str(), "%02x", &nValue); - pHash[i] = (unsigned char)nValue; - } - - nHashLen = (int) sHash.size() / 2; -} - -void Logo() -{ - printf("RainbowCrack (improved, multi-threaded) - Making a Faster Cryptanalytic Time-Memory Trade-Off\n"); - printf("by Martin Westergaard \n"); - printf("multi-threaded and enhanced by neinbrucke (version 0.6.3)\n"); - printf("http://www.freerainbowtables.com/\n"); - printf("original code by Zhu Shuanglei \n"); - printf("http://www.antsight.com/zsl/rainbowcrack/\n\n"); -} - -// XXX nmap is GPL2, will check newer releases regarding license -// Code comes from nmap, used for the linux implementation of kbhit() -#ifndef _WIN32 - -static int tty_fd = 0; -struct termios saved_ti; - -int tty_getchar() -{ - int c, numChars; - - if (tty_fd && tcgetpgrp(tty_fd) == getpid()) { - c = 0; - numChars = read(tty_fd, &c, 1); - if (numChars > 0) return c; - } - - return -1; -} - -void tty_done() -{ - if (!tty_fd) return; - - tcsetattr(tty_fd, TCSANOW, &saved_ti); - - close(tty_fd); - tty_fd = 0; -} - -void tty_init() -{ - struct termios ti; - - if (tty_fd) - return; - - if ((tty_fd = open("/dev/tty", O_RDONLY | O_NONBLOCK)) < 0) return; - - tcgetattr(tty_fd, &ti); - saved_ti = ti; - ti.c_lflag &= ~(ICANON | ECHO); - ti.c_cc[VMIN] = 1; - ti.c_cc[VTIME] = 0; - tcsetattr(tty_fd, TCSANOW, &ti); - - atexit(tty_done); -} - -void tty_flush(void) -{ - tcflush(tty_fd, TCIFLUSH); -} -// end nmap code -#endif +/* + * rcracki_mt is a multithreaded implementation and fork of the original + * RainbowCrack + * + * Copyright (C) Zhu Shuanglei + * Copyright Martin Westergaard Jørgensen + * Copyright 2009, 2010 Daniël Niggebrugge + * Copyright 2009, 2010 James Nobis + * + * This file is part of rcracki_mt. + * + * rcracki_mt is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * rcracki_mt is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with rcracki_mt. If not, see . + */ + +#if defined(_WIN32) && !defined(__GNUC__) + #pragma warning(disable : 4786 4267 4018) +#endif + +#include "Public.h" + +#ifdef _WIN32 + #include +#endif + +#if defined(_WIN32) && !defined(__GNUC__) + #include + #include + + #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) + #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64 + #else + #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL + #endif + + struct timezone + { + int tz_minuteswest; /* minutes W of Greenwich */ + int tz_dsttime; /* type of dst correction */ + }; + + int gettimeofday(struct timeval *tv, struct timezone *tz) + { + // Define a structure to receive the current Windows filetime + FILETIME ft; + + // Initialize the present time to 0 and the timezone to UTC + unsigned __int64 tmpres = 0; + static int tzflag = 0; + + if (NULL != tv) + { + GetSystemTimeAsFileTime(&ft); + + // The GetSystemTimeAsFileTime returns the number of 100 nanosecond + // intervals since Jan 1, 1601 in a structure. Copy the high bits to + // the 64 bit tmpres, shift it left by 32 then or in the low 32 bits. + tmpres |= ft.dwHighDateTime; + tmpres <<= 32; + tmpres |= ft.dwLowDateTime; + + // Convert to microseconds by dividing by 10 + tmpres /= 10; + + // The Unix epoch starts on Jan 1 1970. Need to subtract the difference + // in seconds from Jan 1 1601. + tmpres -= DELTA_EPOCH_IN_MICROSECS; + + // Finally change microseconds to seconds and place in the seconds value. + // The modulus picks up the microseconds. + tv->tv_sec = (long)(tmpres / 1000000UL); + tv->tv_usec = (long)(tmpres % 1000000UL); + } + + if (NULL != tz) + { + if (!tzflag) + { + _tzset(); + tzflag++; + } + + // Adjust for the timezone west of Greenwich + tz->tz_minuteswest = _timezone / 60; + tz->tz_dsttime = _daylight; + } + + return 0; + } + +#elif defined(__APPLE__) || \ + ((defined(__unix__) || defined(unix)) && !defined(USG)) + + #include + + #if defined(BSD) + #include + #elif defined(__linux__) + #include + #else + #error Unsupported Operating System + #endif +#endif + +////////////////////////////////////////////////////////////////////// + +timeval sub_timeofday( timeval tv2, timeval tv ) +{ + timeval final; + + final.tv_usec = tv2.tv_usec - tv.tv_usec; + final.tv_sec = tv2.tv_sec - tv.tv_sec; + + if ( final.tv_usec < 0 ) + { + final.tv_usec += 1000000; + --final.tv_sec; + } + + return final; +} + +long GetFileLen(FILE* file) +{ + // XXX on x86/x86_64 linux returns long + // 32-bit this is a problem if the file is > (2^31-1) bytes + long pos = ftell(file); + fseek(file, 0, SEEK_END); + long len = ftell(file); + fseek(file, pos, SEEK_SET); + + return len; +} + +string TrimString(string s) +{ + while (s.size() > 0) + { + if (s[0] == ' ' || s[0] == '\t') + s = s.substr(1); + else + break; + } + + while (s.size() > 0) + { + if (s[s.size() - 1] == ' ' || s[s.size() - 1] == '\t') + s = s.substr(0, s.size() - 1); + else + break; + } + + return s; +} +bool GetHybridCharsets(string sCharset, vector& vCharset) +{ + // Example: hybrid(mixalpha-numeric-all-space#1-6,numeric#1-4) + if(sCharset.substr(0, 6) != "hybrid") // Not hybrid charset + return false; + + string::size_type nEnd = sCharset.rfind(')'); + string::size_type nStart = (int) sCharset.rfind('('); + string sChar = sCharset.substr(nStart + 1, nEnd - nStart - 1); + vector vParts; + SeperateString(sChar, ",", vParts); + for(uint32 i = 0; i < vParts.size(); i++) + { + tCharset stCharset; + vector vParts2; + SeperateString(vParts[i], "#", vParts2); + stCharset.sName = vParts2[0]; + vector vParts3; + SeperateString(vParts2[1], "-", vParts3); + stCharset.nPlainLenMin = atoi(vParts3[0].c_str()); + stCharset.nPlainLenMax = atoi(vParts3[1].c_str()); + vCharset.push_back(stCharset); + } + return true; +} +#ifdef BOINC +bool boinc_ReadLinesFromFile(string sPathName, vector& vLine) +{ + vLine.clear(); + char input_path[512]; + boinc_resolve_filename(sPathName.c_str(), input_path, sizeof(input_path)); + FILE *file = boinc_fopen(input_path, "rb"); + if (!file) { + fprintf(stderr, + "Couldn't find input file, resolved name %s.\n", input_path + ); + exit(-1); + } + + if (file != NULL) + { - unsigned int len = GetFileLen(file); ++ long len = GetFileLen(file); + char* data = new char[len + 1]; + fread(data, 1, len, file); + data[len] = '\0'; + string content = data; + content += "\n"; + delete [] data; + + unsigned int i; + for (i = 0; i < content.size(); i++) + { + if (content[i] == '\r') + content[i] = '\n'; + } + + string::size_type n; + while ((n = content.find("\n", 0)) != string::npos) + { + string line = content.substr(0, n); + line = TrimString(line); + if (line != "") + vLine.push_back(line); + content = content.substr(n + 1); + } + + fclose(file); + } + else + return false; + + return true; +} +#endif +bool ReadLinesFromFile(string sPathName, vector& vLine) +{ + vLine.clear(); + + FILE* file = fopen(sPathName.c_str(), "rb"); + if (file != NULL) + { - unsigned int len = GetFileLen(file); ++ long len = GetFileLen(file); + char* data = new char[len + 1]; + fread(data, 1, len, file); + data[len] = '\0'; + string content = data; + content += "\n"; + delete [] data; + + unsigned int i; + for (i = 0; i < content.size(); i++) + { + if (content[i] == '\r') + content[i] = '\n'; + } + + string::size_type n; + while ((n = content.find("\n", 0)) != string::npos) + { + string line = content.substr(0, n); + line = TrimString(line); + if (line != "") + vLine.push_back(line); + content = content.substr(n + 1); + } + + fclose(file); + } + else + return false; + + return true; +} + +bool writeResultLineToFile(string sOutputFile, string sHash, string sPlain, string sBinary) +{ + FILE* file = fopen(sOutputFile.c_str(), "a"); + if (file!=NULL) + { + string buffer = sHash + ":" + sPlain + ":" + sBinary + "\n"; + fputs (buffer.c_str(), file); + fclose (file); + return true; + } + else + return false; +} + +bool SeperateString(string s, string sSeperator, vector& vPart) +{ + vPart.clear(); + + unsigned int i; + for (i = 0; i < sSeperator.size(); i++) + { + string::size_type n; + if ( (n = s.find(sSeperator[i])) != string::npos) + { + vPart.push_back(s.substr(0, n)); + s = s.substr(n + 1); + } + else + { + printf("not found: %c\n", sSeperator[i]); + printf("s: %s\n", s.c_str()); + return false; + } + } + vPart.push_back(s); + + return true; +} + +string uint64tostr(uint64 n) +{ + char str[32]; + +#ifdef _WIN32 + sprintf(str, "%I64u", n); +#else + sprintf(str, "%llu", n); +#endif + + return str; +} + +string uint64tohexstr(uint64 n) +{ + char str[32]; + +#ifdef _WIN32 + sprintf(str, "%016I64x", n); +#else + sprintf(str, "%016llx", n); +#endif + + return str; +} + +string HexToStr(const unsigned char* pData, int nLen) +{ + string sRet; + int i; + for (i = 0; i < nLen; i++) + { + char szByte[3]; + sprintf(szByte, "%02x", pData[i]); + sRet += szByte; + } + + return sRet; +} + +unsigned long GetAvailPhysMemorySize() +{ - #if defined(_WIN32) ++#ifdef _WIN32 + MEMORYSTATUS ms; + GlobalMemoryStatus(&ms); + return ms.dwAvailPhys; +#elif defined(BSD) + int mib[2] = { CTL_HW, HW_PHYSMEM }; + uint64 physMem; + //XXX warning size_t isn't portable + size_t len; + len = sizeof(physMem); + sysctl(mib, 2, &physMem, &len, NULL, 0); + return physMem; +#elif defined(__linux__) + struct sysinfo info; + sysinfo(&info); + return ( info.freeram + info.bufferram ) * (unsigned long) info.mem_unit; +#else + return 0; + #error Unsupported Operating System +#endif +} + +string GetApplicationPath() +{ + char fullPath[FILENAME_MAX]; + +#ifdef _WIN32 + GetModuleFileName(NULL, fullPath, FILENAME_MAX); +#else + char szTmp[32]; + // XXX linux/proc file system dependent + sprintf(szTmp, "/proc/%d/exe", getpid()); + int bytes = readlink(szTmp, fullPath, FILENAME_MAX); + + if( bytes >= 0 ) + fullPath[bytes] = '\0'; +#endif + + string sApplicationPath = fullPath; +#ifdef _WIN32 + string::size_type nIndex = sApplicationPath.find_last_of('\\'); +#else + string::size_type nIndex = sApplicationPath.find_last_of('/'); +#endif + + if ( nIndex != string::npos ) + sApplicationPath = sApplicationPath.substr(0, nIndex+1); + + return sApplicationPath; +} + +void ParseHash(string sHash, unsigned char* pHash, int& nHashLen) +{ + uint32 i; + for (i = 0; i < sHash.size() / 2; i++) + { + string sSub = sHash.substr(i * 2, 2); + int nValue; + sscanf(sSub.c_str(), "%02x", &nValue); + pHash[i] = (unsigned char)nValue; + } + + nHashLen = (int) sHash.size() / 2; +} + +void Logo() +{ + printf("RainbowCrack (improved, multi-threaded) - Making a Faster Cryptanalytic Time-Memory Trade-Off\n"); + printf("by Martin Westergaard \n"); + printf("multi-threaded and enhanced by neinbrucke (version 0.6.3)\n"); + printf("http://www.freerainbowtables.com/\n"); + printf("original code by Zhu Shuanglei \n"); + printf("http://www.antsight.com/zsl/rainbowcrack/\n\n"); +} + +// XXX nmap is GPL2, will check newer releases regarding license +// Code comes from nmap, used for the linux implementation of kbhit() +#ifndef _WIN32 + +static int tty_fd = 0; +struct termios saved_ti; + +int tty_getchar() +{ + int c, numChars; + + if (tty_fd && tcgetpgrp(tty_fd) == getpid()) { + c = 0; + numChars = read(tty_fd, &c, 1); + if (numChars > 0) return c; + } + + return -1; +} + +void tty_done() +{ + if (!tty_fd) return; + + tcsetattr(tty_fd, TCSANOW, &saved_ti); + + close(tty_fd); + tty_fd = 0; +} + +void tty_init() +{ + struct termios ti; + + if (tty_fd) + return; + + if ((tty_fd = open("/dev/tty", O_RDONLY | O_NONBLOCK)) < 0) return; + + tcgetattr(tty_fd, &ti); + saved_ti = ti; + ti.c_lflag &= ~(ICANON | ECHO); + ti.c_cc[VMIN] = 1; + ti.c_cc[VTIME] = 0; + tcsetattr(tty_fd, TCSANOW, &ti); + + atexit(tty_done); +} + +void tty_flush(void) +{ + tcflush(tty_fd, TCIFLUSH); +} +// end nmap code +#endif diff --cc Client Applications/rcracki_mt/RTI2Reader.cpp index abd8550,0000000..b1d5f96 mode 100644,000000..100644 --- a/Client Applications/rcracki_mt/RTI2Reader.cpp +++ b/Client Applications/rcracki_mt/RTI2Reader.cpp @@@ -1,159 -1,0 +1,163 @@@ +/* + * rcracki_mt is a multithreaded implementation and fork of the original + * RainbowCrack + * + * Copyright 2010 Martin Westergaard Jørgensen + * Copyright 2010 Daniël Niggebrugge + * Copyright 2010 James Nobis + * + * This file is part of rcracki_mt. + * + * rcracki_mt is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * rcracki_mt is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with rcracki_mt. If not, see . + */ + +#include "RTI2Reader.h" + +#include + +RTI2Header *RTI2Reader::m_pHeader = NULL; +RTI2Reader::RTI2Reader(string Filename) +{ + //m_pIndexPos = NULL, m_pChainPos = NULL;; + m_pIndex = NULL; + m_pFile = fopen(Filename.c_str(), "rb"); + if(m_pFile == NULL) + { + printf("Unable to open file %s", Filename.c_str()); + exit(1); + } + FILE *pFileIndex = fopen(Filename.append(".index").c_str(), "rb"); + if(pFileIndex == NULL) + { + printf("Unable to open file %s", Filename.append(".index").c_str()); + exit(1); + } + m_chainPosition = 0; + + long len = GetFileLen(pFileIndex); + fseek(pFileIndex, 0, SEEK_SET); + - m_pIndex = new unsigned char[len]; ++ m_pIndex = new (nothrow) unsigned char[len]; ++ if(m_pIndex == NULL) { ++ printf("Error allocating %ld MB memory for index in RTI2Reader::RTI2Reader()", len / (1024 * 1024)); ++ exit(-2); ++ } + if(fread(m_pIndex, 1, len, pFileIndex) != (unsigned long)len) + { + printf("Error while reading index file"); + exit(1); + } + fclose(pFileIndex); + m_pHeader = new RTI2Header(); + memcpy(m_pHeader, m_pIndex, sizeof(RTI2Header)); + m_pHeader->m_cppos = (unsigned int*)(m_pIndex + 8); + m_pHeader->prefixstart = *(uint64*)(m_pIndex + 8 + (m_pHeader->rti_cplength * 4)); + m_chainsizebytes = (uint32)ceil((float)(m_pHeader->rti_startptlength + m_pHeader->rti_endptlength + m_pHeader->rti_cplength) / 8); // Get the size of each chain in bytes + m_indexrowsizebytes = (uint32)ceil((float)m_pHeader->rti_index_numchainslength / 8); + // Check the filesize + fseek(m_pFile, 0, SEEK_END); + len = ftell(m_pFile); + fseek(m_pFile, 0, SEEK_SET); + if(len % m_chainsizebytes > 0) + { + printf("Invalid filesize %ld\n", len); + return; + } + + +} + +RTI2Reader::~RTI2Reader(void) +{ + if(m_pIndex != NULL) delete m_pIndex; + if(m_pFile != NULL) fclose(m_pFile); + +} + +unsigned int RTI2Reader::GetChainsLeft() +{ + long len = GetFileLen(m_pFile); + return len / m_chainsizebytes - m_chainPosition; +} + +int RTI2Reader::ReadChains(unsigned int &numChains, RainbowChainO *pData) +{ + if(strncmp(m_pHeader->header, "RTI2", 4) != 0) + { + numChains = 0; + return -1; + } + unsigned char *pNumChains = m_pIndex + (m_pHeader->rti_cplength * 4) + 16; // Pointer into the index containing info about how many numbers are in the first chain prefix + unsigned int i = 0; + unsigned int indexRow = 0; // Current offset into the index + unsigned int curRowPosition = 0; + + while(true) // Fast forward to current position + { + // ALERT: Possible problem here if m_indexrowsizebytes > 1 as pNumChains is a unsigned char. + unsigned int NumChainsInRow = (unsigned int)*(pNumChains + indexRow * m_indexrowsizebytes); + if(m_indexrowsizebytes > 1) + { + //XXX Have to find a solution to this problem + printf( "FATAL: m_indexrowsizebytes > 1: %d\n", m_indexrowsizebytes ); + exit(2); + } + if(i + NumChainsInRow > m_chainPosition) + { + curRowPosition = m_chainPosition - i; + break; // The current position is somewhere within this prefix + } + indexRow++; + i += NumChainsInRow; + } + + uint64 chainrow = 0; // Buffer to store a single read chain + unsigned int chainsProcessed = 0; // Number of chains processed + + // XXX: same problem with unsigned char here. + unsigned int NumChainsInRow = *(pNumChains + indexRow); + while(chainsProcessed < numChains && fread(&chainrow, 1, m_chainsizebytes, m_pFile) == m_chainsizebytes) + { + if(curRowPosition >= NumChainsInRow) + { // Skip to next index row position + indexRow++; + curRowPosition = 0; + NumChainsInRow = *(pNumChains + indexRow); + } + while(NumChainsInRow == 0) // We skip forward until we hit a index with > 0 chains + { + indexRow++; + NumChainsInRow = *(pNumChains + indexRow); + curRowPosition = 0; + } + // Load the starting point from the data + pData[chainsProcessed].nIndexS = chainrow << ( 64 - m_pHeader->rti_startptlength ); + pData[chainsProcessed].nIndexS = pData[chainsProcessed].nIndexS >> ( 64 - m_pHeader->rti_startptlength ); + + // Load the ending point prefix + pData[chainsProcessed].nIndexE = ( m_pHeader->prefixstart + indexRow ) << m_pHeader->rti_endptlength; + // Append the ending point suffix +#if defined(_WIN32) && !defined(__GNUC__) + pData[chainsProcessed].nIndexE |= (chainrow & (0xFFFFFFFFFFFFFFFFI64 >> m_pHeader->rti_cplength)) >> m_pHeader->rti_startptlength; +#else + pData[chainsProcessed].nIndexE |= (chainrow & (0xFFFFFFFFFFFFFFFFllu >> m_pHeader->rti_cplength)) >> m_pHeader->rti_startptlength; +#endif + //pData[chainsProcessed].nCheckPoint = (chainrow >> m_pHeader->rti_startptlength + m_pHeader->rti_endptlength); + curRowPosition++; + chainsProcessed++; + } + numChains = chainsProcessed; + m_chainPosition += numChains; + return 0; +} diff --cc Client Applications/rcracki_mt/RTI2Reader.h index 3545035,0000000..e0f5ed4 mode 100644,000000..100644 --- a/Client Applications/rcracki_mt/RTI2Reader.h +++ b/Client Applications/rcracki_mt/RTI2Reader.h @@@ -1,68 -1,0 +1,68 @@@ +/* + * rcracki_mt is a multithreaded implementation and fork of the original + * RainbowCrack + * + * Copyright 2010 Martin Westergaard Jørgensen + * Copyright 2010 James Nobis + * + * This file is part of rcracki_mt. + * + * rcracki_mt is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * rcracki_mt is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with rcracki_mt. If not, see . + */ + +#ifndef __RTI2READER_H__ +#define __RTI2READER_H__ + +#include "Public.h" +#include + +#if defined(_WIN32) && !defined(__GNUC__) + #include +#endif + +#include +#include "BaseRTReader.h" + +using namespace std; + +typedef struct +{ + char header[4]; + unsigned char rti_startptlength, rti_endptlength, rti_cplength, rti_index_numchainslength; + uint64 prefixstart; + unsigned int *m_cppos; +} RTI2Header; + +class RTI2Reader : BaseRTReader +{ +private: + FILE *m_pFile; + uint32 m_chainPosition; + unsigned char *m_pPos, *m_pChainPos; + static RTI2Header *m_pHeader; + unsigned char *m_pIndex; + uint32 m_chainsizebytes; + uint32 m_indexrowsizebytes; + + +public: + RTI2Reader(string Filename); + ~RTI2Reader(void); - int ReadChains(unsigned int &numChains, RainbowChainO *pData); ++ int ReadChains(uint32 &numChains, RainbowChainO *pData); + unsigned int GetChainsLeft(); + static RTI2Header *GetHeader() { return m_pHeader; } +}; + + +#endif diff --cc Client Applications/rti2rto/Makefile index f9ca133,3eadfbf..2494a84 --- a/Client Applications/rti2rto/Makefile +++ b/Client Applications/rti2rto/Makefile @@@ -1,7 -1,7 +1,7 @@@ SHELL = /bin/sh CC = g++ OPTIMIZATION = -O3 --INCLUDES = -I../../Common/rt\ api -I../../Server\ Applications/rsearchi ++INCLUDES = -I../../Common/rt\ api CFLAGS = -Wall -ansi $(INCLUDES) $(OPTIMIZATION) -c $(DEBUG) LFLAGS = -Wall -ansi $(INCLUDES) $(OPTIMIZATION) $(DEBUG) LIBS = diff --cc Client Applications/rti2rto/rti2rto.cpp index e69ed18,086c333..ba7f163 --- a/Client Applications/rti2rto/rti2rto.cpp +++ b/Client Applications/rti2rto/rti2rto.cpp @@@ -108,16 -108,28 +108,28 @@@ void ConvertRainbowTable(string sPathNa printf("Invalid table type '%s'", sType.c_str()); return ; } - - RainbowChainCP* pChain = (RainbowChainCP*)mp.Allocate(reader->GetChainsLeft() * sizeof(RainbowChainCP), nAllocatedSize); + int size = reader->GetChainsLeft() * sizeof(RainbowChain); + #ifdef _MEMORYDEBUG + printf("Starting allocation of %i bytes\n", size); + #endif + RainbowChain* pChain = (RainbowChain*)mp.Allocate(size, nAllocatedSize); + #ifdef _MEMORYDEBUG + printf("Finished. Got %i bytes\n", nAllocatedSize); + #endif if (pChain != NULL) { - nAllocatedSize = nAllocatedSize / sizeof(RainbowChainCP) * sizeof(RainbowChainCP); // Round to boundary - unsigned int nChains = nAllocatedSize / sizeof(RainbowChainCP); + nAllocatedSize = nAllocatedSize / sizeof(RainbowChain) * sizeof(RainbowChain); // Round to boundary + unsigned int nChains = nAllocatedSize / sizeof(RainbowChain); while(reader->GetChainsLeft() > 0) { + #ifdef _MEMORYDEBUG + printf("Grabbing %i chains from file\n", nChains); + #endif reader->ReadChains(nChains, pChain); + #ifdef _MEMORYDEBUG + printf("Recieved %i chains from file\n", nChains); + #endif - for(UINT4 i = 0; i < nChains; i++) + for(uint32 i = 0; i < nChains; i++) { fwrite(&pChain[i], 1, 16, fResult); } diff --cc Common/rt api/MemoryPool.cpp index 053db7a,4c8d5c9..a674507 --- a/Common/rt api/MemoryPool.cpp +++ b/Common/rt api/MemoryPool.cpp @@@ -45,9 -25,11 +45,11 @@@ CMemoryPool::CMemoryPool( CMemoryPool::~CMemoryPool() { - if (m_pMem != NULL) - { + if (m_pMem != NULL) { + #ifdef _MEMORYDEBUG + printf("Freeing %i bytes of memory\n", m_nMemSize); + #endif - delete m_pMem; + delete [] m_pMem; m_pMem = NULL; m_nMemSize = 0; } @@@ -62,15 -43,18 +63,18 @@@ unsigned char* CMemoryPool::Allocate(un } unsigned int nTargetSize; - if (nFileLen < m_nMemMax) + if (nFileLen < m_nMemMax) { nTargetSize = nFileLen; - else + } + else { nTargetSize = m_nMemMax; - + } // Free existing memory - if (m_pMem != NULL) - { + if (m_pMem != NULL) { + #ifdef _MEMORYDEBUG + printf("Freeing %i bytes of memory\n", m_nMemSize); + #endif - delete m_pMem; + delete [] m_pMem; m_pMem = NULL; m_nMemSize = 0; } diff --cc Common/rt api/Public.cpp index 6cd0482,834dea7..b42c954 --- a/Common/rt api/Public.cpp +++ b/Common/rt api/Public.cpp @@@ -209,7 -125,38 +209,40 @@@ bool GetHybridCharsets(string sCharset bool boinc_ReadLinesFromFile(string sPathName, vector& vLine) { vLine.clear(); ++#ifdef 0 + vLine.push_back("byte = []"); + vLine.push_back("alpha = [ABCDEFGHIJKLMNOPQRSTUVWXYZ]"); + vLine.push_back("alpha-space = [ABCDEFGHIJKLMNOPQRSTUVWXYZ ]"); + vLine.push_back("alpha-numeric = [ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]"); + vLine.push_back("alpha-numeric-space = [ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ]"); + vLine.push_back("alpha-numeric-symbol14 = [ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\x21\x40\x23\x24\x25\x5E\x26\x2A\x28\x29\x2D\x5F\x2B\x3D]"); + vLine.push_back("alpha-numeric-symbol14-space= [ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\x21\x40\x23\x24\x25\x5E\x26\x2A\x28\x29\x2D\x5F\x2B\x3D\x20]"); + vLine.push_back("all = [ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\x21\x40\x23\x24\x25\x5E\x26\x2A\x28\x29\x2D\x5F\x2B\x3D\x7E\x60\x5B\x5D\x7B\x7D\x7C\x5C\x3A\x3B\x22\x27\x3C\x3E\x2C\x2E\x3F\x2F]"); + vLine.push_back("all-space = [ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\x21\x40\x23\x24\x25\x5E\x26\x2A\x28\x29\x2D\x5F\x2B\x3D\x7E\x60\x5B\x5D\x7B\x7D\x7C\x5C\x3A\x3B\x22\x27\x3C\x3E\x2C\x2E\x3F\x2F\x20]"); + vLine.push_back("alpha-numeric-symbol32-space = [ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\x21\x40\x23\x24\x25\x5E\x26\x2A\x28\x29\x2D\x5F\x2B\x3D\x7E\x60\x5B\x5D\x7B\x7D\x7C\x5C\x3A\x3B\x22\x27\x3C\x3E\x2C\x2E\x3F\x2F\x20]"); + vLine.push_back("lm-frt-cp437 = [\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x5B\x5C\x5D\x5E\x5F\x60\x7B\x7C\x7D\x7E\x80\x8E\x8F\x90\x92\x99\x9A\x9B\x9C\x9D\x9E\x9F\xA5\xE0\xE1\xE2\xE3\xE4\xE6\xE7\xE8\xE9\xEA\xEB\xEE]"); + vLine.push_back("lm-frt-cp850 = [\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x5B\x5C\x5D\x5E\x5F\x60\x7B\x7C\x7D\x7E\x80\x8E\x8F\x90\x92\x99\x9A\x9C\x9D\x9F\xA5\xB5\xB6\xB7\xBD\xBE\xC7\xCF\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xDE\xE0\xE1\xE2\xE3\xE5\xE6\xE8\xE9\xEA\xEB\xED\xEF]"); + vLine.push_back("lm-frt-cp437-850 = [\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x5B\x5C\x5D\x5E\x5F\x60\x7B\x7C\x7D\x7E\x80\x8E\x8F\x90\x92\x99\x9A\x9B\x9C\x9D\x9E\x9F\xA5\xB5\xB6\xB7\xBD\xBE\xC7\xCF\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xDE\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xED\xEE\xEF]"); + vLine.push_back("numeric = [0123456789]"); + vLine.push_back("numeric-space = [0123456789 ]"); + vLine.push_back("loweralpha = [abcdefghijklmnopqrstuvwxyz]"); + vLine.push_back("loweralpha-space = [abcdefghijklmnopqrstuvwxyz ]"); + vLine.push_back("loweralpha-numeric = [abcdefghijklmnopqrstuvwxyz0123456789]"); + vLine.push_back("loweralpha-numeric-space = [abcdefghijklmnopqrstuvwxyz0123456789 ]"); + vLine.push_back("loweralpha-numeric-symbol14 = [abcdefghijklmnopqrstuvwxyz0123456789\x21\x40\x23\x24\x25\x5E\x26\x2A\x28\x29\x2D\x5F\x2B\x3D]"); + vLine.push_back("loweralpha-numeric-all = [abcdefghijklmnopqrstuvwxyz0123456789\x21\x40\x23\x24\x25\x5E\x26\x2A\x28\x29\x2D\x5F\x2B\x3D\x7E\x60\x5B\x5D\x7B\x7D\x7C\x5C\x3A\x3B\x22\x27\x3C\x3E\x2C\x2E\x3F\x2F]"); + vLine.push_back("loweralpha-numeric-symbol32-space= [abcdefghijklmnopqrstuvwxyz0123456789\x21\x40\x23\x24\x25\x5E\x26\x2A\x28\x29\x2D\x5F\x2B\x3D\x7E\x60\x5B\x5D\x7B\x7D\x7C\x5C\x3A\x3B\x22\x27\x3C\x3E\x2C\x2E\x3F\x2F\x20]"); + vLine.push_back("mixalpha = [abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ]"); + vLine.push_back("mixalpha-space = [abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ]"); + vLine.push_back("mixalpha-numeric = [abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]"); + vLine.push_back("mixalpha-numeric-space = [abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ]"); + vLine.push_back("mixalpha-numeric-symbol14 = [abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\x21\x40\x23\x24\x25\x5E\x26\x2A\x28\x29\x2D\x5F\x2B\x3D]"); + vLine.push_back("mixalpha-numeric-all = [abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\x21\x40\x23\x24\x25\x5E\x26\x2A\x28\x29\x2D\x5F\x2B\x3D\x7E\x60\x5B\x5D\x7B\x7D\x7C\x5C\x3A\x3B\x22\x27\x3C\x3E\x2C\x2E\x3F\x2F]"); + vLine.push_back("mixalpha-numeric-symbol32-space = [abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\x21\x40\x23\x24\x25\x5E\x26\x2A\x28\x29\x2D\x5F\x2B\x3D\x7E\x60\x5B\x5D\x7B\x7D\x7C\x5C\x3A\x3B\x22\x27\x3C\x3E\x2C\x2E\x3F\x2F\x20]"); + vLine.push_back("mixalpha-numeric-all-space = [abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\x21\x40\x23\x24\x25\x5E\x26\x2A\x28\x29\x2D\x5F\x2B\x3D\x7E\x60\x5B\x5D\x7B\x7D\x7C\x5C\x3A\x3B\x22\x27\x3C\x3E\x2C\x2E\x3F\x2F\x20]"); ++#endif + -/* char input_path[512]; + char input_path[512]; boinc_resolve_filename(sPathName.c_str(), input_path, sizeof(input_path)); FILE *file = boinc_fopen(input_path, "rb"); if (!file) { diff --cc Common/rt api/RTI2Reader.cpp index 6fb0cb9,004159c..ad7db3d --- a/Common/rt api/RTI2Reader.cpp +++ b/Common/rt api/RTI2Reader.cpp @@@ -44,11 -20,15 +44,15 @@@ RTI2Reader::RTI2Reader(string Filename } m_chainPosition = 0; - unsigned int len = GetFileLen(pFileIndex); + long len = GetFileLen(pFileIndex); fseek(pFileIndex, 0, SEEK_SET); - m_pIndex = new unsigned char[len]; + m_pIndex = new (nothrow) unsigned char[len]; + if(m_pIndex == NULL) { - printf("Error allocating %u MB memory for index in RTI2Reader::RTI2Reader()", len / (1024 * 1024)); ++ printf("Error allocating %ld MB memory for index in RTI2Reader::RTI2Reader()", len / (1024 * 1024)); + exit(-2); + } - if(fread(m_pIndex, 1, len, pFileIndex) != len) + if(fread(m_pIndex, 1, len, pFileIndex) != (unsigned long)len) { printf("Error while reading index file"); exit(1); @@@ -66,7 -46,7 +70,7 @@@ fseek(m_pFile, 0, SEEK_SET); if(len % m_chainsizebytes > 0) { - printf("Invalid filesize %ld\n", len); - printf("Invalid filesize %u\n", len); ++ printf("Invalid filesize %lu\n", len); return; } diff --cc Common/rt api/RTI2Reader.h index 902344a,0b1f127..db3d446 --- a/Common/rt api/RTI2Reader.h +++ b/Common/rt api/RTI2Reader.h @@@ -59,7 -33,7 +59,7 @@@ private public: RTI2Reader(string Filename); ~RTI2Reader(void); - int ReadChains(unsigned int &numChains, RainbowChainCP *pData); - int ReadChains(UINT4 &numChains, RainbowChain *pData); ++ int ReadChains(uint32 &numChains, RainbowChain *pData); unsigned int GetChainsLeft(); static RTI2Header *GetHeader() { return m_pHeader; } }; diff --cc Common/rt api/RTIReader.cpp index 1a97786,3134e91..374b062 --- a/Common/rt api/RTIReader.cpp +++ b/Common/rt api/RTIReader.cpp @@@ -17,23 -17,38 +17,38 @@@ RTIReader::RTIReader(string Filename m_chainPosition = 0; // Load the index file - unsigned int nIndexFileLen = GetFileLen(pFileIndex); - unsigned int nFileLen = GetFileLen(m_pFile); + long nIndexFileLen = GetFileLen(pFileIndex); + long nFileLen = GetFileLen(m_pFile); unsigned int nTotalChainCount = nFileLen / 8; if (nFileLen % 8 != 0) - printf("file length mismatch (%ld bytes)\n", nFileLen); - printf("file length mismatch (%u bytes)\n", nFileLen); ++ printf("file length mismatch (%lu bytes)\n", nFileLen); else { // File length check if (nIndexFileLen % 11 != 0) - printf("index file length mismatch (%ld bytes)\n", nIndexFileLen); - printf("index file length mismatch (%u bytes)\n", nIndexFileLen); ++ printf("index file length mismatch (%lu bytes)\n", nIndexFileLen); else { - m_pIndex = new IndexChain[nIndexFileLen / 11]; + if(m_pIndex != NULL) { + delete m_pIndex; + m_pIndex = NULL; + } + #ifdef _MEMORYDEBUG + printf("Allocating %u MB memory for RTIReader::m_pIndex", nIndexFileLen / 11 / (1024 * 1024)); + #endif + m_pIndex = new (nothrow) IndexChain[nIndexFileLen / 11]; + if(m_pIndex == NULL) { - printf("\nFailed allocating %i MB memory.\n", nIndexFileLen / 11 / (1024 * 1024)); ++ printf("\nFailed allocating %ld MB memory.\n", nIndexFileLen / 11 / (1024 * 1024)); + exit(-2); + } + #ifdef _MEMORYDEBUG + printf(" - success!\n"); + #endif memset(m_pIndex, 0x00, sizeof(IndexChain) * (nIndexFileLen / 11)); fseek(pFileIndex, 0, SEEK_SET); - int nRows; - int nRead = 0; - UINT4 nRows; -- for(nRows = 0; (nRows * 11) < nIndexFileLen; nRows++) ++ //int nRead = 0; ++ uint32 nRows; ++ for(nRows = 0; (nRows * 11) < (uint32)nIndexFileLen; nRows++) { if(fread(&m_pIndex[nRows].nPrefix, 5, 1, pFileIndex) != 1) break; if(fread(&m_pIndex[nRows].nFirstChain, 4, 1, pFileIndex) != 1) break;