#include "stdafx.h"
#include <windows.h>
#include <stdlib.h>
char* GetHash(
HANDLE file,const char* pattern)
{
DWORD bytes=(DWORD)strlen(pattern)+1;
char* buffer=new char[bytes];
DWORD pbytes=0;
UINT offset=0;
do{
if (SetFilePointer(file,(LONG)offset++,0,FILE_BEGIN)==INVALID_SET_FILE_POINTER && GetLastError()!=NO_ERROR)
{ _tprintf_s(_T(" error - invalid set file pointer (system error code %d)\n"),GetLastError());
delete [] buffer;
buffer=NULL;
return NULL;
}
if (ReadFile(file,(LPVOID)buffer,bytes,&pbytes,NULL)==NULL && GetLastError()!=NO_ERROR)
{ _tprintf_s(_T(" error - could not read file (system error code %d)\n"),GetLastError());
delete [] buffer;
buffer=NULL;
return NULL;
}
*(buffer+bytes-1)=NULL;
}while(strcmp(buffer,pattern)&&pbytes!=0);
if(pbytes==0)
{ delete [] buffer;
buffer=NULL;
return NULL;
}
delete [] buffer;
buffer=NULL;
DWORD offsett=offset+bytes;
UINT counter=0;
buffer=new char[2];
do{
if (SetFilePointer(file,offsett++,0,FILE_BEGIN)==INVALID_SET_FILE_POINTER&&GetLastError()!=NO_ERROR)
{ _tprintf_s(_T(" error - invalid set file pointer (system error code %d)\n"),GetLastError());
delete [] buffer;
buffer=NULL;
return NULL;
}
if (ReadFile(file,(LPVOID)buffer,(DWORD)1,&pbytes,NULL)==NULL&&GetLastError()!=NO_ERROR)
{ _tprintf_s(_T(" error - could not read file (system error code %d)\n"),GetLastError());
delete [] buffer;
buffer=NULL;
return NULL;
}
if((*buffer!=0)&&(pbytes>0)) counter++;
}while(*buffer!=0&&pbytes!=0);
if(pbytes==0)
{ delete [] buffer;
buffer=NULL;
return NULL;
}
delete [] buffer;
buffer=NULL;
char * value=new char[counter+1];
offset=offset+bytes;
if (SetFilePointer(file,offset,0,FILE_BEGIN)==INVALID_SET_FILE_POINTER&&GetLastError()!=NO_ERROR)
{ _tprintf_s(_T(" error - invalid set file pointer (system error code %d)\n"),GetLastError());
delete [] value;
value=NULL;
return NULL;
}
if (ReadFile(file,(LPVOID)value,(DWORD)(counter+1),&pbytes,NULL)==NULL&&GetLastError()!=NO_ERROR)
{ _tprintf_s(_T(" error - could not read file (system error code %d)\n"),GetLastError());
delete [] value;
value=NULL;
return NULL;
}
return (char*)value;
}//-----------------
char* ServerPasswordDecode(const char* hash)
{
size_t passsize=strlen(hash)/2+1;
char* pass=new char[passsize];
char* t=new char[2];
*t=NULL;
*pass=NULL;
*(t+1)=NULL;
for(UINT i=0;i<strlen(hash);i++)
if(i%2)
{ if(*(hash+i)=='D')
{ *t=*(hash+i-1)-17;
strcat_s(pass,passsize,t);
}else
if(*(hash+i)=='G')
{ *t=*(hash+i-1)+31;
strcat_s(pass,passsize,t);
}else
if(*(hash+i)=='H')
{ *t=*(hash+i-1)+47;
strcat_s(pass,passsize,t);
}
}
delete [] t;
return pass;
}//---------
char* ProfilePasswordDecode(const char* hash)
{
size_t passsize=strlen(hash)+1;
char* pass=new char[passsize];
*pass=NULL;
for(size_t i=0;i<passsize-1;i++)
{ pass[i]=(~hash[i]);
pass[i+1]=NULL;
}
return pass;
}//-----------------
BOOL CheckGG(
HANDLE file)
{
char buffer[5];
DWORD rbytes=0;
if (SetFilePointer(file,(LONG)0,0,FILE_BEGIN)==INVALID_SET_FILE_POINTER && GetLastError()!=NO_ERROR)
return FALSE;
if (ReadFile(file,(LPVOID)buffer,(DWORD)5,&rbytes,NULL)==NULL && GetLastError()!=NO_ERROR && rbytes!=0)
return FALSE;
buffer[4]=NULL;
if(strcmp(buffer,"cfg1"))
return FALSE;
else
return TRUE;
}//-------------------------
int _tmain(int argc, TCHAR **argv)
{
_tprintf_s(_T("\n %s v1.01 - gadu-gadu config file decoder\n"),*argv);
_tprintf_s(_T(" copyright (c) g-n-d.net www.g-n-d.net\n\n"));
if (argc<2||(_tcsstr(*(argv+1),_T("config.dat"))==NULL))
{ _tprintf_s(_T(" usage: %s <gadu-gadu config.dat file>\n"),*argv);
return 0;
}
HANDLE hFile;
hFile=CreateFile(*(argv+1), // file to open
GENERIC_READ, // open for reading
FILE_SHARE_READ, // share for reading
NULL, // default security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template
if (hFile == INVALID_HANDLE_VALUE)
{ _tprintf_s(_T(" could not open file %s (system error code %d)\n"),*(argv+1),GetLastError());
return 0;
}
if(!CheckGG(hFile))
{ _tprintf_s(_T(" given %s file is not the gadu-gadu config file\n"),*(argv+1));
return 0;
}
char* hash=NULL;
char* value=NULL;
hash=GetHash(hFile,"Number");
if(hash!=NULL)
{ DWORD *p=(DWORD*)hash;
printf_s(" identity number: %ld\n",*p);
}
delete [] hash;
hash=GetHash(hFile,"Password2");
if(hash!=NULL)
{ value=ServerPasswordDecode(hash);
printf_s(" password for server: %s\n",value);
delete [] value;
}
delete [] hash;
hash=GetHash(hFile,"passwordstr");
if(hash!=NULL)
{ value=ProfilePasswordDecode(hash);
printf_s(" password for profile: %s\n",value);
delete [] value;
}
delete [] hash;
CloseHandle(hFile);
return 0;
}