//build an object for the listbox //and fill it with appropriate data: //headline | headline | headline //name of segment start address end address //... ... ... line *build_segm_obj(void) { int i; int seg_qty = get_segm_qty(); line *obj; char addrbuf[10]; segment_t *curseg; //allocate seg_qty lines +1 (for the header line) obj = (line *)malloc(sizeof(line)*(seg_qty+1)); //first line will be filled with headline captions strcpy( (char *)obj[0].segname, headline[0]); strcpy( (char *)obj[0].startEA,headline[1]); strcpy( (char *)obj[0].endEA,headline[2]); //the following lines will contain name of segment, //start- and end address of segments for(i=1;i<=seg_qty;i++) { curseg = getnseg(i-1); strcpy( (char *)obj[i].segname, get_true_segm_name(curseg)); sprintf(addrbuf,"%08X",curseg->startEA); strcpy( (char *)obj[i].startEA, addrbuf); sprintf(addrbuf,"%08X",curseg->endEA); strcpy( (char *)obj[i].endEA, addrbuf); } //return pointer to object return obj; }
//-------------------------------------------------------------------------- void segend(ea_t ea) { segment_t *Sarea = getseg(ea-1); if ( is_spec_segm(Sarea->type) ) return; if ( (ash.uflag & UAS_GNU) == 0 ) { char sname[MAXNAMELEN]; get_true_segm_name(Sarea, sname, sizeof(sname)); if ( strcmp(sname, "XMEM") != 0 ) printf_line(inf.indent, "endsec"); } }
//-------------------------------------------------------------------------- void segstart(ea_t ea) { segment_t *Sarea = getseg(ea); if ( is_spec_segm(Sarea->type) ) return; char sname[MAXNAMELEN]; char sclas[MAXNAMELEN]; get_true_segm_name(Sarea, sname, sizeof(sname)); get_segm_class(Sarea, sclas, sizeof(sclas)); if ( ash.uflag & UAS_GNU ) { const char *const predefined[] = { ".text", // Text section ".data", // Data sections ".rdata", ".comm", }; int i; for ( i=0; i < qnumber(predefined); i++ ) if ( strcmp(sname, predefined[i]) == 0 ) break; if ( i != qnumber(predefined) ) printf_line(inf.indent, COLSTR("%s", SCOLOR_ASMDIR), sname); else printf_line(inf.indent, COLSTR(".section %s", SCOLOR_ASMDIR) " " COLSTR("%s %s", SCOLOR_AUTOCMT), sname, ash.cmnt, sclas); } else { if ( strcmp(sname, "XMEM") == 0 ) { char buf[MAX_NUMBUF]; btoa(buf, sizeof(buf), ea-get_segm_base(Sarea)); printf_line(inf.indent, COLSTR("%s %c:%s", SCOLOR_ASMDIR), ash.origin, tolower(sname[0]), buf); } else { printf_line(inf.indent, COLSTR("section %s", SCOLOR_ASMDIR) " " COLSTR("%s %s", SCOLOR_AUTOCMT), sname, ash.cmnt, sclas); } } }
//-------------------------------------------------------------------------- void segstart(ea_t ea) { const char *predefined[] = { ".text", // Text section ".rdata", // Read-only data section ".data", // Data sections ".lit8", // Data sections ".lit4", // Data sections ".sdata", // Small data section, addressed through register $gp ".sbss", // Small bss section, addressed through register $gp ".bss", // bss (block started by storage) section, which loads zero-initialized data }; segment_t *Sarea = getseg(ea); if ( is_spec_segm(Sarea->type) ) return; char sname[MAXNAMELEN]; char sclas[MAXNAMELEN]; get_true_segm_name(Sarea, sname, sizeof(sname)); get_segm_class(Sarea, sclas, sizeof(sclas)); int i; for ( i=0; i < qnumber(predefined); i++ ) if ( strcmp(sname, predefined[i]) == 0 ) break; if ( i != qnumber(predefined) ) printf_line(inf.indent, COLSTR("%s", SCOLOR_ASMDIR), sname); else printf_line(inf.indent, COLSTR("%s", SCOLOR_ASMDIR) "" COLSTR("%s %s", SCOLOR_AUTOCMT), strcmp(sclas,"CODE") == 0 ? ".text" : strcmp(sclas,"BSS") == 0 ? ".bss" : ".data", ash.cmnt, sname); if ( Sarea->orgbase != 0 ) { char buf[MAX_NUMBUF]; btoa(buf, sizeof(buf), Sarea->orgbase); printf_line(inf.indent, COLSTR("%s %s", SCOLOR_ASMDIR), ash.origin, buf); } }
void Apply_patches(HWND hwnd) { HWND hList = GetDlgItem(hwnd,IDC_LIST_SEGMEN); netnode n("$ Apply SegMen"); char szFilePath[256 * 2] = {0}; strncpy(szFilePath, database_idb, 256); char *lpTmpBuf = strrchr(szFilePath, '\\') + 1; if(lpTmpBuf == (char*)1) { return; } *lpTmpBuf = 0; get_root_filename(lpTmpBuf, 256); msg("=============================\n"); msg("Apply Path:%s\n", szFilePath); if(IsDlgButtonChecked(hwnd, IDC_APPLY_CHECK_BACK)) { char szBackPath[300] = {0}; sprintf(szBackPath, "%s.back", szFilePath); msg("BackFile Path:%s.back\n", szFilePath); CopyFile(szFilePath, szBackPath, FALSE); n.altset(CHECK_BACKFILE_INDEX, 1); } else { n.altset(CHECK_BACKFILE_INDEX, 0); } HANDLE hFile=CreateFile(szFilePath, GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); //获得文件句柄 HANDLE hMapping=CreateFileMapping(hFile,NULL,PAGE_READWRITE,0,0,NULL); //创建内存映射对象 if(INVALID_HANDLE_VALUE == hMapping) { msg("CreateFileMapping :%08X ErrorCode:%d\n", hMapping, GetLastError()); return ; } unsigned char* pvFile=(unsigned char*)MapViewOfFile(hMapping,FILE_MAP_ALL_ACCESS,0,0,0); //创建视图 就是映射文件到内存; int i; segment_t *curseg; int seg_qty = get_segm_qty(); for(i=0 ; i < seg_qty; i++) { char segname[0x100] = {0}; curseg = getnseg(i); get_true_segm_name(curseg, segname, 255); int offset = get_fileregion_offset(curseg->startEA); int nSize = curseg->endEA - curseg->startEA; int nSelectStat = CheckedListBox_GetCheckState(hList, i); n.altset(i, nSelectStat); if(offset > 0 && nSelectStat) { //msg("offset:%X segname:%s EA:%08X, nSize: %X\n", offset, segname, curseg->startEA, nSize); unsigned char *lpMem = (unsigned char*)malloc(nSize + 1); memset(lpMem, 0, nSize + 1); //if(get_many_bytes(curseg->startEA, lpMem, nSize)) if(segReadBuf(curseg->startEA, lpMem, nSize)) { msg("Apply SegMenName: %s\n", segname); SegWriteFile(pvFile, lpMem, nSize, offset); } //msg("lpMem:%X\n", lpMem); free(lpMem); } // msg("Name:%s, StartEA:%08X, Offset:%08X, EndEA:%08X\n", segname, curseg->startEA, offset, curseg->endEA); } CloseHandle(hMapping); // msg("CloseHandle(hMapping)\n"); if(0 == UnmapViewOfFile(pvFile) ) { msg("文件同步失败! ErrorCode:%d\n", GetLastError()); } else { msg("文件同步成功!\n"); msg("=============================\n"); } // msg("UnmapViewOfFile(pvFile);\n"); CloseHandle(hFile); return; }
BOOL Main_Apply_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam) { HWND hList = GetDlgItem(hwnd,IDC_LIST_SEGMEN); CheckedListBox_SetFlatStyleChecks(hList, TRUE); char szFilePath[256 * 2] = {0}; strncpy(szFilePath, database_idb, 256); char *lpTmpBuf = strrchr(szFilePath, '\\') + 1; if(lpTmpBuf == (char*)1) { return FALSE; } *lpTmpBuf = 0; get_root_filename(lpTmpBuf, 256); HANDLE hFile=CreateFile(szFilePath, GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); //获得文件句柄 if(hFile == INVALID_HANDLE_VALUE) { msg("Apply Path:%s\n", szFilePath); msg("失败!目标%s文件不存在 或 文件无法打开 ErrorCode:%d\n", szFilePath, GetLastError()); MoveWindowCenter(g_hwndMain, hwnd); return FALSE; } CloseHandle(hFile); int i; segment_t *curseg; int seg_qty = get_segm_qty(); netnode n("$ Apply SegMen"); if(BADNODE == (nodeidx_t)n) { netnode n("$ Apply SegMen", 0, true); for(i=0 ; i < seg_qty; i++) { BOOL bStats = FALSE; char segname[0x100] = {0}; curseg = getnseg(i); get_true_segm_name(curseg, segname, 255); ListBox_InsertString(hList, -1, segname); bStats = !IsFilterTable(segname); if(bStats) { msg("segname:%s, type:%d\n", segname, curseg->type); bStats = curseg->type == SEG_CODE; } if(get_fileregion_offset(curseg->startEA) > 0) { CheckedListBox_SetCheckState(hList, i, bStats); n.altset(i, bStats); } } n.altset(CHECK_BACKFILE_INDEX, 1); CheckDlgButton( hwnd,IDC_APPLY_CHECK_BACK, BST_CHECKED ); } else { for(i=0 ; i < seg_qty; i++) { char segname[0x100] = {0}; curseg = getnseg(i); get_true_segm_name(curseg, segname, 255); ListBox_InsertString(hList, -1, segname); if(get_fileregion_offset(curseg->startEA) > 0) { CheckedListBox_SetCheckState(hList, i, n.altval(i)); } } if(n.altval(CHECK_BACKFILE_INDEX)) { CheckDlgButton( hwnd, IDC_APPLY_CHECK_BACK, BST_CHECKED ); } } MoveWindowCenter(g_hwndMain, hwnd); return FALSE; }
//-------------------------------------------------------------------------- void segstart(ea_t ea) { char buf[MAXSTR]; char *const end = buf + sizeof(buf); segment_t *Sarea = getseg(ea); if ( is_spec_segm(Sarea->type) ) return; const char *align; switch ( Sarea->align ) { case saAbs: align = "at: "; break; case saRelByte: align = "byte"; break; case saRelWord: align = "word"; break; case saRelPara: align = "para"; break; case saRelPage: align = "page"; break; case saRel4K: align = "4k"; break; case saRel64Bytes: align = "64"; break; default: align = NULL; break; } if ( align == NULL ) { gen_cmt_line("Segment alignment '%s' can not be represented in assembly", get_segment_alignment(Sarea->align)); align = ""; } char sname[MAXNAMELEN]; char sclas[MAXNAMELEN]; get_true_segm_name(Sarea, sname, sizeof(sname)); get_segm_class(Sarea, sclas, sizeof(sclas)); char *ptr = buf + qsnprintf(buf, sizeof(buf), SCOLOR_ON SCOLOR_ASMDIR "%-*s segment %s ", inf.indent-1, sname, align); if ( Sarea->align == saAbs ) { ea_t absbase = get_segm_base(Sarea); ptr += btoa(ptr, end-ptr, absbase); APPCHAR(ptr, end, ' '); } const char *comb; switch ( Sarea->comb ) { case scPub: case scPub2: case scPub3: comb = ""; break; case scCommon: comb = "common"; break; default: comb = NULL; break; } if ( comb == NULL ) { gen_cmt_line("Segment combination '%s' can not be represented in assembly", get_segment_combination(Sarea->comb)); comb = ""; } ptr += qsnprintf(ptr, end-ptr, "%s '%s'", comb, sclas); tag_off(ptr, end, COLOR_ASMDIR); MakeLine(buf, 0); }
void Apply_patches() { char szFilePath[256 * 2] = {0}; strncpy(szFilePath, database_idb, 256); char *lpTmpBuf = strrchr(szFilePath, '\\') + 1; if(lpTmpBuf == (char*)1) { return; } *lpTmpBuf = 0; get_root_filename(lpTmpBuf, 256); msg("Apply Path:%s\n", szFilePath); HANDLE hFile=CreateFile(szFilePath, GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); //获得文件句柄 if(hFile == INVALID_HANDLE_VALUE) { msg("失败!目标%s文件不存在 或 文件无法打开 ErrorCode:%d\n", szFilePath, GetLastError()); return; } HANDLE hMapping=CreateFileMapping(hFile,NULL,PAGE_READWRITE,0,0,NULL); //创建内存映射对象 if(INVALID_HANDLE_VALUE == hMapping) { msg("CreateFileMapping :%08X ErrorCode:%d\n", hMapping, GetLastError()); } unsigned char* pvFile=(unsigned char*)MapViewOfFile(hMapping,FILE_MAP_ALL_ACCESS,0,0,0); //创建视图 就是映射文件到内存; int i; segment_t *curseg; int seg_qty = get_segm_qty(); for(i=0 ; i < seg_qty; i++) { char segname[0x100] = {0}; curseg = getnseg(i); get_true_segm_name(curseg, segname, 255); int offset = get_fileregion_offset(curseg->startEA); int nSize = curseg->endEA - curseg->startEA; if(offset > 0 && !IsFilterTable(segname)) { //msg("offset:%X segname:%s EA:%08X, nSize: %X\n", offset, segname, curseg->startEA, nSize); unsigned char *lpMem = (unsigned char*)malloc(nSize + 1); memset(lpMem, 0, nSize + 1); //if(get_many_bytes(curseg->startEA, lpMem, nSize)) if(segReadBuf(curseg->startEA, lpMem, nSize)) { SegWriteFile(pvFile, lpMem, nSize, offset); } //msg("lpMem:%X\n", lpMem); free(lpMem); } // msg("Name:%s, StartEA:%08X, Offset:%08X, EndEA:%08X\n", segname, curseg->startEA, offset, curseg->endEA); } CloseHandle(hMapping); // msg("CloseHandle(hMapping)\n"); UnmapViewOfFile(pvFile); // msg("UnmapViewOfFile(pvFile);\n"); CloseHandle(hFile); msg("文件同步成功!\n"); return; }
// Plug-in process void CORE_Process(int iArg) { try { char version[16]; sprintf(version, "%u.%u", HIBYTE(MY_VERSION), LOBYTE(MY_VERSION)); msg("\n>> WhatAPIs: v: %s, built: %s, By Sirmabus\n", version, __DATE__); if (!autoIsOk()) { msg("** Must wait for IDA to finish processing before starting plug-in! **\n*** Aborted ***\n\n"); return; } // Show UI refreshUI(); int uiResult = AskUsingForm_c(mainDialog, version, doHyperlink); if (!uiResult) { msg(" - Canceled -\n"); return; } WaitBox::show(); TIMESTAMP startTime = getTimeStamp(); // Build import segment bounds table { msg("Import segments:\n"); refreshUI(); SEGLIST segList; for (int i = 0; i < get_segm_qty(); i++) { if (segment_t *s = getnseg(i)) { if (s->type == SEG_XTRN) { char buffer[64] = { "unknown" }; buffer[SIZESTR(buffer)] = 0; get_true_segm_name(s, buffer, SIZESTR(buffer)); msg(" [%d] \"%s\" "EAFORMAT" - "EAFORMAT"\n", segmentCount, buffer, s->startEA, s->endEA); BOUNDS b = { s->startEA, s->endEA }; segList.push_back(b); segmentCount++; } } } refreshUI(); // Flatten list into an array for speed if (segmentCount) { UINT size = (segmentCount * sizeof(BOUNDS)); if (segmentPtr = (BOUNDS *)_aligned_malloc(size, 16)) { BOUNDS *b = segmentPtr; for (SEGLIST::iterator i = segList.begin(); i != segList.end(); i++, b++) { b->startEA = i->startEA; b->endEA = i->endEA; } } else { msg("\n*** Allocation failure of %u bytes! ***\n", size); refreshUI(); } } } if (segmentCount) { // Make a list of all import names if (int moduleCount = get_import_module_qty()) { for (int i = 0; i < moduleCount; i++) enum_import_names(i, importNameCallback); char buffer[32]; msg("Parsed %s module imports.\n", prettyNumberString(moduleCount, buffer)); refreshUI(); } // Iterate through all functions.. BOOL aborted = FALSE; UINT functionCount = get_func_qty(); char buffer[32]; msg("Processing %s functions.\n", prettyNumberString(functionCount, buffer)); refreshUI(); for (UINT n = 0; n < functionCount; n++) { processFunction(getn_func(n)); if (WaitBox::isUpdateTime()) { if (WaitBox::updateAndCancelCheck((int)(((float)n / (float)functionCount) * 100.0f))) { msg("* Aborted *\n"); break; } } } refresh_idaview_anyway(); WaitBox::hide(); msg("\n"); msg("Done. %s comments add/appended in %s.\n", prettyNumberString(commentCount, buffer), timeString(getTimeStamp() - startTime)); msg("-------------------------------------------------------------\n"); } else msg("\n*** No import segments! ***\n"); if (segmentPtr) { _aligned_free(segmentPtr); segmentPtr = NULL; } apiMap.clear(); } CATCH() }