uint8 ReadTypeDescriptor(FILE* fp) { /* struct TypeDescriptorPrefix { BYTE is_pointer : 1, is_unique_pointer : 1, is_reference : 1, tag : 5; }; */ uint8 TypeDescriptorPrefix = fget8(fp); uint8 tag = TypeDescriptorPrefix & 0x1f; if (tag <= 17) { // BYTE SimpleTypeDescriptor = fget8(fp); } else if (tag == 18) // InterfaceTypeDescriptor { uint16 interface_index = fget16(fp); } else if (tag == 19) // InterfaceIsTypeDescriptor { uint8 arg_num = fget8(fp); } else { throw -1;//exit(1); } return tag; }
void LoadUEFState(char *StateName) { char errmsg[256]; char UEFId[10]; //-- int CompletionBits=0; // These bits should be filled in //## Unused. long RPos=0,FLength,CPos; unsigned int Block,Length; int Version; strcpy(UEFId,"BlankFile"); UEFState=fopen(StateName,"rb"); if (UEFState != NULL) { //-> fseek(UEFState,NULL,SEEK_END); //++ fseek(UEFState,0,SEEK_END); //<- FLength=ftell(UEFState); fseek(UEFState,0,SEEK_SET); // Get File length for eof comparison. fatal_fread(UEFId,10,1,UEFState); if (strcmp(UEFId,"UEF File!")!=0) { MessageBox(GETHWND,"The file selected is not a UEF File.","BeebEm",MB_ICONERROR|MB_OK); fclose(UEFState); return; } Version=fget16(UEFState); sprintf(errmsg,"UEF Version %x",Version); //MessageBox(GETHWND,errmsg,"BeebEm",MB_OK); RPos=ftell(UEFState); while (ftell(UEFState)<FLength) { Block=fget16(UEFState); Length=fget32(UEFState); CPos=ftell(UEFState); sprintf(errmsg,"Block %04X - Length %d (%04X)",Block,Length,Length); //MessageBox(GETHWND,errmsg,"BeebEm",MB_ICONERROR|MB_OK); if (Block==0x046A) LoadEmuUEF(UEFState,Version); if (Block==0x0460) Load6502UEF(UEFState); if (Block==0x0461) LoadRomRegsUEF(UEFState); if (Block==0x0462) LoadMainMemUEF(UEFState); if (Block==0x0463) LoadShadMemUEF(UEFState); if (Block==0x0464) LoadPrivMemUEF(UEFState); if (Block==0x0465) LoadFileMemUEF(UEFState); if (Block==0x0466) LoadSWRMMemUEF(UEFState); if (Block==0x0467) LoadViaUEF(UEFState); if (Block==0x0468) LoadVideoUEF(UEFState); if (Block==0x046B) LoadSoundUEF(UEFState); if (Block==0x046D) LoadIntegraBHiddenMemUEF(UEFState); if (Block==0x046E) Load8271UEF(UEFState); if (Block==0x046F) Load1770UEF(UEFState,Version); if (Block==0x0470) LoadTubeUEF(UEFState); if (Block==0x0471) Load65C02UEF(UEFState); if (Block==0x0472) Load65C02MemUEF(UEFState); if (Block==0x0473) LoadSerialUEF(UEFState); fseek(UEFState,CPos+Length,SEEK_SET); // Skip unrecognised blocks (and over any gaps) } fclose(UEFState); mainWin->SetRomMenu(); mainWin->SetDiscWriteProtects(); } else { char errstr[256]; sprintf(errstr, "Cannot open state file: %s", StateName); MessageBox(GETHWND,errstr,"BeebEm",MB_ICONERROR|MB_OK); } }
void LoadShadMemUEF(FILE *SUEF) { int SAddr; SAddr=fget16(SUEF); fread(ShadowRAM+SAddr,1,32768,SUEF); }
unsigned long fget32(FILE* fp) { unsigned long a=fget16(fp); unsigned long b=fget16(fp)<<16; return a|b; }
static void process_file(char *filename, char *parentfile) { FILE *fp; int i; struct stat sb; uint16_t versionMajor; uint16_t versionMinor; char path[4096] = { 0 }; uint32_t encryptionFlags; char *buffer, *xmlfile; FILE *ofp; xmlDocPtr doc; xmlNodePtr cur; int spinCount; int pkeBlockSize; int pkeKeyBits; int pkeHashSize; unsigned char *pkeSaltValue; unsigned char encryptedVerifierHashInput[16 + 2]; unsigned char encryptedVerifierHashValue[64 + 2]; int version; xmlChar *spinCountXML; xmlChar *saltSizeXML; xmlChar *pkeBlockSizeXML; xmlChar *pkeKeyBitsXML; xmlChar *pkeHashSizeXML; xmlChar *pkeSaltValueXML; xmlChar *hashAlgorithm; xmlChar *encryptedVerifierHashInputXML; xmlChar *encryptedVerifierHashValueXML; uint32_t headerLength; uint32_t skipFlags; uint32_t sizeExtra; uint32_t algId; uint32_t algHashId; uint32_t keySize; uint32_t providerType; char CSPName[1024]; uint32_t saltSize; char salt[1024]; char encryptedVerifier[16]; uint32_t verifierHashSize; char encryptedVerifierHash[64]; if(stat(filename, &sb) != 0) { } if (!(fp = fopen(filename, "rb"))) { fprintf(stderr, "! %s : %s\n", filename, strerror(errno)); return; } while (!feof(fp)) { versionMajor = fget16(fp); versionMinor = fget16(fp); encryptionFlags = fget32(fp); if (encryptionFlags == fExternal) { fprintf(stderr, "%s : An external cryptographic provider is not supported\n", parentfile); return; } if (versionMinor == 0x04 && versionMajor == 0x04) { /* Office 2010 and 2013 files */ if (encryptionFlags != fAgile) fprintf(stderr, "%s : The encryption flags are not consistent with the encryption type\n", parentfile); /* rest of the data is in XML format, dump it to a file */ strcpy(path, filename); buffer = (char*)malloc(sb.st_size); fread(buffer, sb.st_size - 8, 1, fp); xmlfile = strcat(path, ".xml"); if (!(ofp = fopen(xmlfile, "w"))) { fprintf(stderr, "! %s : %s\n", filename, strerror(errno)); return; } fwrite(buffer, sb.st_size - 8, 1, ofp); fclose(ofp); /* process XML file */ doc = xmlParseFile(xmlfile); if (doc == NULL ) { fprintf(stderr, "Document not parsed successfully. \n"); return; } cur = xmlDocGetRootElement(doc); if (cur == NULL) { fprintf(stderr, "empty document\n"); xmlFreeDoc(doc); return; } cur = cur->xmlChildrenNode; while (cur != NULL) { if ((!xmlStrcmp(cur->name, (const xmlChar *)"keyEncryptors"))) break; cur = cur->next; } cur = cur->xmlChildrenNode; while (cur != NULL) { if ((!xmlStrcmp(cur->name, (const xmlChar *)"keyEncryptor"))) break; cur = cur->next; } cur = cur->xmlChildrenNode; /* we are now at "encryptedKey" node */ spinCountXML = xmlGetProp(cur, "spinCount"); spinCount = atoi(spinCountXML); xmlFree(spinCountXML); saltSizeXML = xmlGetProp(cur, "saltSize"); saltSize = atoi(saltSizeXML); xmlFree(saltSizeXML); pkeBlockSizeXML = xmlGetProp(cur, "blockSize"); pkeBlockSize = atoi(pkeBlockSizeXML); xmlFree(pkeBlockSizeXML); pkeKeyBitsXML = xmlGetProp(cur, "keyBits"); pkeKeyBits = atoi(pkeKeyBitsXML); xmlFree(pkeKeyBitsXML); pkeHashSizeXML = xmlGetProp(cur, "hashSize"); hashAlgorithm = xmlGetProp(cur, "hashAlgorithm"); if(strcmp(hashAlgorithm, "SHA1") == 0) { version = 2010; } else if (strcmp(hashAlgorithm, "SHA512") == 0) { version = 2013; } else { fprintf(stderr, "%s uses un-supported hashing algorithm %s, please file a bug! \n", parentfile, hashAlgorithm); return; } pkeHashSize = atoi(pkeHashSizeXML); xmlFree(pkeHashSizeXML); pkeSaltValueXML = xmlGetProp(cur, "saltValue"); pkeSaltValue = (unsigned char*)malloc(16 + 2); base64_decode(pkeSaltValueXML, strlen(pkeSaltValueXML), pkeSaltValue); xmlFree(pkeSaltValueXML); encryptedVerifierHashInputXML = xmlGetProp(cur, "encryptedVerifierHashInput"); base64_decode(encryptedVerifierHashInputXML, strlen(encryptedVerifierHashInputXML), encryptedVerifierHashInput); xmlFree(encryptedVerifierHashInputXML); encryptedVerifierHashValueXML = xmlGetProp(cur, "encryptedVerifierHashValue"); base64_decode(encryptedVerifierHashValueXML, strlen(encryptedVerifierHashValueXML), encryptedVerifierHashValue); xmlFree(encryptedVerifierHashValueXML); printf("%s:$office$*%d*%d*%d*%d*", parentfile, version, spinCount, pkeKeyBits, saltSize); print_hex(pkeSaltValue, saltSize); printf("*"); print_hex(encryptedVerifierHashInput, 16); printf("*"); print_hex(encryptedVerifierHashValue, 32); printf("\n"); xmlFreeDoc(doc); unlink(xmlfile); return; } /* Office 2007 file processing */ // Encryption header headerLength = fget32(fp); skipFlags = fget32(fp); headerLength -= 4; sizeExtra = fget32(fp); headerLength -= 4; algId = fget32(fp); headerLength -= 4; algHashId = fget32(fp); headerLength -= 4; keySize = fget32(fp); headerLength -= 4; providerType = fget32(fp); headerLength -= 4; (void)fget32(fp); headerLength -= 4; // Reserved 1 (void)fget32(fp); headerLength -= 4; // Reserved 2 fread(CSPName, headerLength, 1, fp); // Encryption verifier saltSize = fget32(fp); fread(salt, saltSize, 1, fp); fread(encryptedVerifier, 16, 1, fp); verifierHashSize = fget32(fp); if(providerType == PTRC4) fread(encryptedVerifierHash, 0x14, 1, fp); else fread(encryptedVerifierHash, 0x20, 1, fp); version = 2007; printf("%s:$office$*%d*%d*%d*%d*", parentfile, version, verifierHashSize, keySize, saltSize); print_hex(salt, saltSize); printf("*"); print_hex(encryptedVerifier, 16); printf("*"); print_hex(encryptedVerifierHash, 32); printf("\n"); break; } }
HRESULT XPTypeLibParse(FILE* fp, IXPTypeLib** ppTypeLib) { CXPTypeLib* pTypeLib = new CXPTypeLib; pTypeLib->fp = fp; int nread = fread(pTypeLib->header.magic, 1, 16, fp); if (nread >= 16 && memcmp(pTypeLib->header.magic, "XPCOM\nTypeLib\r\n\032", 16)==0) { pTypeLib->header.major_version = fget8(fp); pTypeLib->header.minor_version = fget8(fp); pTypeLib->header.num_interfaces = fget16(fp); pTypeLib->header.file_length = fget32(fp); pTypeLib->header.interface_directory = fget32(fp); pTypeLib->header.data_pool = fget32(fp); if (pTypeLib->header.major_version > 1) { // ATLTRACE("XPCOM TypeLib unsupported major version %d\n", pTypeLib->header.major_version); return E_FAIL; } pTypeLib->ifaces = new CXPIFaceInfo*[pTypeLib->header.num_interfaces]; fseek(fp, -1 + pTypeLib->header.interface_directory/* + i*sizeof(InterfaceDirectoryEntry)*/, SEEK_SET); int i; // Read all these in one go for (i = 0; i < pTypeLib->header.num_interfaces; i++) { pTypeLib->ifaces[i] = new CXPIFaceInfo; fread(&pTypeLib->ifaces[i]->m_iface_entry.m_iid, 16, 1, fp); pTypeLib->ifaces[i]->m_iface_entry.m_name = fget32(fp); pTypeLib->ifaces[i]->m_iface_entry.m_namespace = fget32(fp); pTypeLib->ifaces[i]->m_iface_entry.m_interface_descriptor = fget32(fp); } // I currently also read the rest now, but later I may read it only when necessary for (i = 0; i < 44/*pTypeLib->header.num_interfaces*/; i++) { ReadIdentifierFromLocation(fp, -1 + pTypeLib->header.data_pool + pTypeLib->ifaces[i]->m_iface_entry.m_name, pTypeLib->ifaces[i]->m_name); if (pTypeLib->ifaces[i]->m_iface_entry.m_namespace) { ReadIdentifierFromLocation(fp, -1 + pTypeLib->header.data_pool + pTypeLib->ifaces[i]->m_iface_entry.m_namespace, pTypeLib->ifaces[i]->m_namespace); } pTypeLib->ifaces[i]->m_nFuncs = 0; if (pTypeLib->ifaces[i]->m_iface_entry.m_interface_descriptor) { fseek(fp, -1 + pTypeLib->header.data_pool + pTypeLib->ifaces[i]->m_iface_entry.m_interface_descriptor, SEEK_SET); uint16 parent_interface_index = fget16(fp); pTypeLib->ifaces[i]->m_nFuncs = fget16(fp); pTypeLib->ifaces[i]->m_funcs = new XPFUNCDESC*[pTypeLib->ifaces[i]->m_nFuncs]; for (int m = 0; m < pTypeLib->ifaces[i]->m_nFuncs; m++) { pTypeLib->ifaces[i]->m_funcs[m] = new XPFUNCDESC; /* MethodDescriptor { boolean is_getter; boolean is_setter; boolean is_not_xpcom; boolean is_constructor; boolean is_hidden; uint3 reserved; Identifier* name; uint8 num_args; ParamDescriptor params[num_args]; ParamDescriptor result; } */ uint8 flags = fget8(fp); DWORD name = fget32(fp); uint8 num_args = fget8(fp); for (int p = 0; p < num_args; p++) { ReadParamDescriptor(fp); } ReadParamDescriptor(fp); sysstring str; ReadIdentifierFromLocation(fp, -1 + pTypeLib->header.data_pool + name, str); pTypeLib->ifaces[i]->m_funcs[m]->name = str; } uint16 num_constants = fget16(fp); for (int c = 0; c < num_constants; c++) { /* ConstDescriptor { Identifier* name; TypeDescriptor type; <type> value;} */ DWORD name = fget32(fp); uint8 tag = ReadTypeDescriptor(fp); if (tag == 0) { char value = fget8(fp); } else if (tag == 1) { short value = fget16(fp); } else if (tag == 2) { long value = fget32(fp); } else if (tag == 4) { uint8 value = fget8(fp); } else if (tag == 5) { uint16 value = fget16(fp); } else if (tag == 6) { uint32 value = fget32(fp); } else { throw -1;//exit(1); } } /* boolean is_scriptable; boolean is_function; uint6 reserved; */ uint8 flags = fget8(fp); /* InterfaceDescriptor { uint16 parent_interface_index; uint16 num_methods; MethodDescriptor method_descriptors[num_methods]; uint16 num_constants; ConstDescriptor const_descriptors[num_constants]; boolean is_scriptable; boolean is_function; uint6 reserved; } */ } } *ppTypeLib = pTypeLib; return S_OK; } else { return E_FAIL; } }
static void process_file(const char *fname) { unsigned char filename[1024]; FILE *fp; int i; char path[LARGE_ENOUGH]; if (!(fp = fopen(fname, "rb"))) { fprintf(stderr, "! %s : %s\n", fname, strerror(errno)); return; } while (!feof(fp)) { uint32_t id = fget32(fp); if (id == 0x04034b50UL) { /* local header */ uint16_t version = fget16(fp); uint16_t flags = fget16(fp); uint16_t compression_method = fget16(fp); uint16_t lastmod_time = fget16(fp); uint16_t lastmod_date = fget16(fp); uint32_t crc = fget32(fp); uint32_t compressed_size = fget32(fp); uint32_t uncompressed_size = fget32(fp); uint16_t filename_length = fget16(fp); uint16_t extrafield_length = fget16(fp); /* unused variables */ (void) version; (void) lastmod_time; (void) lastmod_date; (void) crc; (void) uncompressed_size; if (filename_length > 250) { fprintf(stderr, "! %s: Invalid zip file, filename length too long!\n", fname); return; } if (fread(filename, 1, filename_length, fp) != filename_length) { fprintf(stderr, "Error, in fread of file data!\n"); goto cleanup; } filename[filename_length] = 0; if (compression_method == 99) { /* AES encryption */ uint16_t efh_id = fget16(fp); uint16_t efh_datasize = fget16(fp); uint16_t efh_vendor_version = fget16(fp); uint16_t efh_vendor_id = fget16(fp); char efh_aes_strength = fgetc(fp); uint16_t actual_compression_method = fget16(fp); unsigned char salt[16]; char *bname; int n = 0; uint16_t password_verification_value; unsigned char *p; strnzcpy(path, fname, sizeof(path)); bname = basename(path); fprintf(stderr, "%s->%s is using AES encryption, extrafield_length is %d\n", fname, filename, extrafield_length); /* unused variables */ (void) efh_id; (void) efh_datasize; (void) efh_vendor_version; (void) efh_vendor_id; (void) efh_aes_strength; (void) actual_compression_method; printf("%s:$zip$*0*%d*", bname, efh_aes_strength); switch (efh_aes_strength) { case 1: n = 8; if (fread(salt, 1, n, fp) != n) { fprintf(stderr, "Error, in fread of file data!\n"); goto cleanup; } break; case 2: n = 12; if (fread(salt, 1, n, fp) != n) { fprintf(stderr, "Error, in fread of file data!\n"); goto cleanup; } break; case 3: n = 16; if (fread(salt, 1, n, fp) != n) { fprintf(stderr, "Error, in fread of file data!\n"); goto cleanup; } break; } for (i = 0; i < n; i++) { printf("%c%c", itoa16[ARCH_INDEX(salt[i] >> 4)], itoa16[ARCH_INDEX(salt[i] & 0x0f)]); } password_verification_value = fget16(fp); p = (unsigned char *) &password_verification_value; printf("*"); for (i = 0; i < 2; i++) { printf("%c%c", itoa16[ARCH_INDEX(p[i] >> 4)], itoa16[ARCH_INDEX(p[i] & 0x0f)]); } printf(":::::%s\n", fname); fseek(fp, 10, SEEK_CUR); } else if (flags & 1) { /* old encryption */
void process_database(char* encryptedDatabase) { long dataStartOffset; unsigned long transformRounds; unsigned char *masterSeed; int masterSeedLength; unsigned char *transformSeed; int transformSeedLength; unsigned char *initializationVectors; int initializationVectorsLength; unsigned char *expectedStartBytes; int expectedStartBytesLength; FILE *fp = fopen(encryptedDatabase, "rb"); if (!fp) { fprintf(stderr, "! %s : %s\n", encryptedDatabase, strerror(errno)); return; } uint32_t uSig1 = fget32(fp); uint32_t uSig2 = fget32(fp); if ((uSig1 == FileSignatureOld1) && (uSig2 == FileSignatureOld2)) { fprintf(stderr, "! %s : Old format, not supported currently\n", encryptedDatabase); fclose(fp); return; } if ((uSig1 == FileSignature1) && (uSig2 == FileSignature2)) { } else if ((uSig1 == FileSignaturePreRelease1) && (uSig2 == FileSignaturePreRelease2)) { } else { fprintf(stderr, "! %s : Unknown format: File signature invalid\n", encryptedDatabase); fclose(fp); return; } uint32_t uVersion = fget32(fp); if ((uVersion & FileVersionCriticalMask) > (FileVersion32 & FileVersionCriticalMask)) { fprintf(stderr, "! %s : Unknown format: File version unsupported\n", encryptedDatabase); fclose(fp); return; } int endReached = 0; while (!endReached) { unsigned char btFieldID = fgetc(fp); uint16_t uSize = fget16(fp); unsigned char *pbData = NULL; if (uSize > 0) { pbData = (unsigned char*)malloc(uSize); fread(pbData, uSize, 1, fp); } enum Kdb4HeaderFieldID kdbID = btFieldID; switch (kdbID) { case EndOfHeader: endReached = 1; // end of header free(pbData); break; case MasterSeed: masterSeed = pbData; masterSeedLength = uSize; break; case TransformSeed: transformSeed = pbData; transformSeedLength = uSize; break; case TransformRounds: transformRounds = BytesToUInt64(pbData); free(pbData); break; case EncryptionIV: initializationVectors = pbData; initializationVectorsLength = uSize; break; case StreamStartBytes: expectedStartBytes = pbData; expectedStartBytesLength = uSize; break; default: free(pbData); break; } } dataStartOffset = ftell(fp); if(transformRounds == 0) { fprintf(stderr, "! %s : transformRounds can't be 0\n", encryptedDatabase); return; } #ifdef KEEPASS_DEBUG fprintf(stderr, "%d, %d, %d, %d\n", masterSeedLength, transformSeedLength, initializationVectorsLength, expectedStartBytesLength); #endif printf("%s:$keepass$*2*%ld*%ld*",encryptedDatabase, transformRounds, dataStartOffset); print_hex(masterSeed, masterSeedLength); printf("*"); print_hex(transformSeed, transformSeedLength); printf("*"); print_hex(initializationVectors, initializationVectorsLength); printf("*"); print_hex(expectedStartBytes, expectedStartBytesLength); printf("\n"); free(masterSeed); free(transformSeed); free(initializationVectors); free(expectedStartBytes); fclose(fp); }