⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 p_armpe.cpp

📁 UPX 源代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
    }    // new PE header    memcpy(&oh,&ih,sizeof(oh));    oh.filealign = 0x200; // identsplit depends on this    memset(osection,0,sizeof(osection));    oh.entry = upxsection;    oh.objects = 4;    oh.chksum = 0;    // fill the data directory    ODADDR(PEDIR_DEBUG) = 0;    ODSIZE(PEDIR_DEBUG) = 0;    ODADDR(PEDIR_IAT) = 0;    ODSIZE(PEDIR_IAT) = 0;    ODADDR(PEDIR_BOUNDIM) = 0;    ODSIZE(PEDIR_BOUNDIM) = 0;    // tls is put into section 1    ic = s1addr + s1size - sotls;    super::processTls(&rel,&tlsiv,ic);    ODADDR(PEDIR_TLS) = sotls ? ic : 0;    ODSIZE(PEDIR_TLS) = sotls ? 0x18 : 0;    ic += sotls;    // these are put into section 2    ic = ncsection;    // wince wants relocation data at the beginning of a section    processRelocs(&rel);    ODADDR(PEDIR_RELOC) = soxrelocs ? ic : 0;    ODSIZE(PEDIR_RELOC) = soxrelocs;    ic += soxrelocs;    processImports(ic, linker->getSymbolOffset("IATT") + upxsection);    ODADDR(PEDIR_IMPORT) = ic;    ODSIZE(PEDIR_IMPORT) = soimpdlls;    ic += soimpdlls;    processExports(&xport,ic);    ODADDR(PEDIR_EXPORT) = soexport ? ic : 0;    ODSIZE(PEDIR_EXPORT) = soexport;    if (!isdll && opt->win32_pe.compress_exports)    {        ODADDR(PEDIR_EXPORT) = IDADDR(PEDIR_EXPORT);        ODSIZE(PEDIR_EXPORT) = IDSIZE(PEDIR_EXPORT);    }    ic += soexport;    ic = (ic + oam1) &~ oam1;    const unsigned res_start = ic;    if (soresources)        processResources(&res,ic);    ODADDR(PEDIR_RESOURCE) = soresources ? ic : 0;    ODSIZE(PEDIR_RESOURCE) = soresources;    ic += soresources;    const unsigned onam = ncsection + soxrelocs + ih.imagebase;    linker->defineSymbol("start_of_dll_names", onam);    linker->defineSymbol("start_of_imports", ih.imagebase + rvamin + cimports);    linker->defineSymbol("start_of_relocs", crelocs + rvamin + ih.imagebase);    linker->defineSymbol("filter_buffer_end", ih.imagebase + ih.codebase + ih.codesize);    linker->defineSymbol("filter_buffer_start", ih.imagebase + ih.codebase);    linker->defineSymbol("original_entry", ih.entry + ih.imagebase);    linker->defineSymbol("uncompressed_length", ph.u_len);    linker->defineSymbol("start_of_uncompressed", ih.imagebase + rvamin);    linker->defineSymbol("compressed_length", ph.c_len);    linker->defineSymbol("start_of_compressed", ih.imagebase + s1addr + identsize - identsplit);    defineDecompressorSymbols();    relocateLoader();    MemBuffer loader(lsize);    memcpy(loader, getLoader(), lsize);    patchPackHeader(loader, lsize);    // this is computed here, because soxrelocs changes some lines above    const unsigned ncsize = soxrelocs + soimpdlls + soexport;    const unsigned fam1 = oh.filealign - 1;    // fill the sections    strcpy(osection[0].name,"UPX0");    strcpy(osection[1].name,"UPX1");    strcpy(osection[2].name, "UPX2");    strcpy(osection[3].name, ".rsrc");    osection[0].vaddr = rvamin;    osection[1].vaddr = s1addr;    osection[2].vaddr = ncsection;    osection[3].vaddr = res_start;    osection[0].size = 0;    osection[1].size = (s1size + fam1) &~ fam1;    osection[2].size = (ncsize + fam1) &~ fam1;    osection[3].size = (soresources + fam1) &~ fam1;    osection[0].vsize = osection[1].vaddr - osection[0].vaddr;    //osection[1].vsize = (osection[1].size + oam1) &~ oam1;    //osection[2].vsize = (osection[2].size + oam1) &~ oam1;    osection[1].vsize = osection[1].size;    osection[2].vsize = osection[2].size;    osection[3].vsize = osection[3].size;    osection[0].rawdataptr = 0;    osection[1].rawdataptr = (pe_offset + sizeof(oh) + sizeof(osection) + fam1) &~ fam1;    osection[2].rawdataptr = osection[1].rawdataptr + osection[1].size;    osection[3].rawdataptr = osection[2].rawdataptr + osection[2].size;    osection[0].flags = (unsigned) (PEFL_BSS|PEFL_EXEC|PEFL_WRITE|PEFL_READ);    osection[1].flags = (unsigned) (PEFL_DATA|PEFL_EXEC|PEFL_WRITE|PEFL_READ);    osection[2].flags = (unsigned) (PEFL_DATA|PEFL_READ);    osection[3].flags = (unsigned) (PEFL_DATA|PEFL_READ);    oh.imagesize = (osection[3].vaddr + osection[3].vsize + oam1) &~ oam1;    oh.bsssize  = osection[0].vsize;    oh.datasize = osection[2].vsize + osection[3].vsize;    oh.database = osection[2].vaddr;    oh.codesize = osection[1].vsize;    oh.codebase = osection[1].vaddr;    oh.headersize = osection[1].rawdataptr;    if (rvamin < osection[0].rawdataptr)        throwCantPack("object alignment too small");    if (opt->win32_pe.strip_relocs && !isdll)        oh.flags |= RELOCS_STRIPPED;    //for (ic = 0; ic < oh.filealign; ic += 4)    //    set_le32(ibuf + ic,get_le32("UPX "));    ibuf.clear(0, oh.filealign);    info("Image size change: %u -> %u kBytes",         ih.imagesize / 1024, oh.imagesize / 1024);    infoHeader("[Writing compressed file]");    if (soresources == 0)    {        oh.objects = 3;        memset(&osection[3], 0, sizeof(osection[3]));    }    // write loader + compressed file    fo->write(&oh,sizeof(oh));    fo->write(osection,sizeof(osection));    // some alignment    if (identsplit == identsize)    {        unsigned n = osection[1].rawdataptr - fo->getBytesWritten() - identsize;        assert(n <= oh.filealign);        fo->write(ibuf, n);    }    fo->write(loader + codesize,identsize);    infoWriting("loader", fo->getBytesWritten());    fo->write(obuf,c_len);    infoWriting("compressed data", c_len);    fo->write(loader,codesize);    if (opt->debug.dump_stub_loader)        OutputFile::dump(opt->debug.dump_stub_loader, loader, codesize);    if ((ic = fo->getBytesWritten() & 3) != 0)        fo->write(ibuf,4 - ic);    fo->write(otls,sotls);    if ((ic = fo->getBytesWritten() & fam1) != 0)        fo->write(ibuf,oh.filealign - ic);    fo->write(oxrelocs,soxrelocs);    fo->write(oimpdlls,soimpdlls);    fo->write(oexport,soexport);    if ((ic = fo->getBytesWritten() & fam1) != 0)        fo->write(ibuf,oh.filealign - ic);    fo->write(oresources,soresources);    if ((ic = fo->getBytesWritten() & fam1) != 0)        fo->write(ibuf,oh.filealign - ic);#if 0    printf("%-13s: program hdr  : %8ld bytes\n", getName(), (long) sizeof(oh));    printf("%-13s: sections     : %8ld bytes\n", getName(), (long) sizeof(osection));    printf("%-13s: ident        : %8ld bytes\n", getName(), (long) identsize);    printf("%-13s: compressed   : %8ld bytes\n", getName(), (long) c_len);    printf("%-13s: decompressor : %8ld bytes\n", getName(), (long) codesize);    printf("%-13s: tls          : %8ld bytes\n", getName(), (long) sotls);    printf("%-13s: resources    : %8ld bytes\n", getName(), (long) soresources);    printf("%-13s: imports      : %8ld bytes\n", getName(), (long) soimpdlls);    printf("%-13s: exports      : %8ld bytes\n", getName(), (long) soexport);    printf("%-13s: relocs       : %8ld bytes\n", getName(), (long) soxrelocs);#endif    // verify    verifyOverlappingDecompression();    // copy the overlay    copyOverlay(fo, overlay, &obuf);    // finally check the compression ratio    if (!checkFinalCompressionRatio(fo))        throwNotCompressible();}/*************************************************************************// unpack**************************************************************************/int PackArmPe::canUnpack(){    if (!readFileHeader() || (ih.cpu != 0x1c0 && ih.cpu != 0x1c2))        return false;    unsigned objs = ih.objects;    isection = new pe_section_t[objs];    fi->seek(pe_offset+sizeof(ih),SEEK_SET);    fi->readx(isection,sizeof(pe_section_t)*objs);    if (ih.objects < 3)        return -1;    bool is_packed = ((ih.objects == 3 || ih.objects == 4) &&                      (IDSIZE(15) || ih.entry > isection[1].vaddr));    bool found_ph = false;    if (memcmp(isection[0].name,"UPX",3) == 0)    {        // current version        fi->seek(isection[1].rawdataptr - 64, SEEK_SET);        found_ph = readPackHeader(1024);        if (!found_ph)        {            // old versions            fi->seek(isection[2].rawdataptr, SEEK_SET);            found_ph = readPackHeader(1024);        }    }    if (is_packed && found_ph)        return true;    if (!is_packed && !found_ph)        return -1;    if (is_packed && ih.entry < isection[2].vaddr)    {        unsigned char buf[256];        bool x = false;        memset(buf, 0, sizeof(buf));        try {            fi->seek(ih.entry - isection[1].vaddr + isection[1].rawdataptr, SEEK_SET);            fi->read(buf, sizeof(buf));            // FIXME this is for x86            static const unsigned char magic[] = "\x8b\x1e\x83\xee\xfc\x11\xdb";            // mov ebx, [esi];    sub esi, -4;    adc ebx,ebx            int offset = find(buf, sizeof(buf), magic, 7);            if (offset >= 0 && find(buf + offset + 1, sizeof(buf) - offset - 1, magic, 7) >= 0)                x = true;        } catch (...) {            //x = true;        }        if (x)            throwCantUnpack("file is modified/hacked/protected; take care!!!");        else            throwCantUnpack("file is possibly modified/hacked/protected; take care!");        return false;   // not reached    }    // FIXME: what should we say here ?    //throwCantUnpack("file is possibly modified/hacked/protected; take care!");    return false;}void PackArmPe::rebuildImports(upx_byte *& extrainfo){    if (ODADDR(PEDIR_IMPORT) == 0        || ODSIZE(PEDIR_IMPORT) <= sizeof(import_desc))        return;//    const upx_byte * const idata = obuf + get_le32(extrainfo);    OPTR_C(const upx_byte, idata, obuf + get_le32(extrainfo));    const unsigned inamespos = get_le32(extrainfo + 4);    extrainfo += 8;    unsigned sdllnames = 0;//    const upx_byte *import = ibuf + IDADDR(PEDIR_IMPORT) - isection[2].vaddr;//    const upx_byte *p;    IPTR_I(const upx_byte, import, ibuf + IDADDR(PEDIR_IMPORT) - isection[2].vaddr);    OPTR(const upx_byte, p);    for (p = idata; get_le32(p) != 0; ++p)    {        const upx_byte *dname = get_le32(p) + import;        ICHECK(dname, 1);        const unsigned dlen = strlen(dname);        ICHECK(dname, dlen + 1);        sdllnames += dlen + 1;        for (p += 8; *p;)            if (*p == 1)                p += strlen(++p) + 1;            else if (*p == 0xff)                p += 3; // ordinal            else                p += 5;    }    sdllnames = ALIGN_UP(sdllnames,2u);    upx_byte * const Obuf = obuf - rvamin;    import_desc * const im0 = (import_desc*) (Obuf + ODADDR(PEDIR_IMPORT));    import_desc *im = im0;    upx_byte *dllnames = Obuf + inamespos;    upx_byte *importednames = dllnames + sdllnames;    upx_byte * const importednames_start = importednames;    for (p = idata; get_le32(p) != 0; ++p)    {        // restore the name of the dll        const upx_byte *dname = get_le32(p) + import;        ICHECK(dname, 1);        const unsigned dlen = strlen(dname);        ICHECK(dname, dlen + 1);        const unsigned iatoffs = get_le32(p + 4) + rvamin;        if (inamespos)        {            // now I rebuild the dll names            OCHECK(dllnames, dlen + 1);            strcpy(dllnames, dname);            im->dllname = ptr_diff(dllnames,Obuf);            //;;;printf("\ndll: %s:",dllnames);            dllnames += dlen + 1;        }        else        {            OCHECK(Obuf + im->dllname, dlen + 1);            strcpy(Obuf + im->dllname, dname);        }        im->oft = im->iat = iatoffs;//        LE32 *newiat = (LE32 *) (Obuf + iatoffs);        OPTR_I(LE32, newiat, (LE32 *) (Obuf + iatoffs));        // restore the imported names+ordinals        for (p += 8; *p; ++newiat)            if (*p == 1)            {                const unsigned ilen = strlen(++p) + 1;                if (inamespos)                {                    if (ptr_diff(importednames, importednames_start) & 1)                        importednames -= 1;                    omemcpy(importednames + 2, p, ilen);                    //;;;printf(" %s",importednames+2);                    *newiat = ptr_diff(importednames, Obuf);                    importednames += 2 + ilen;                }                else                {                    OCHECK(Obuf + *newiat + 2, ilen + 1);                    strcpy(Obuf + *newiat + 2, p);                }                p += ilen;            }            else if (*p == 0xff)            {                *newiat = get_le16(p + 1) + 0x80000000;                //;;;printf(" %x",(unsigned)*newiat);                p += 3;            }            else            {                *newiat = get_le32(get_le32(p + 1) + import);                assert(*newiat & 0x80000000);                p += 5;            }        *newiat = 0;        im++;    }    //memset(idata,0,p - idata);}/*vi:ts=4:et*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -