private static void readSection(PE pe, DataEntry entry, IDataReader dr) throws IOException { SectionTable st = pe.getSectionTable(); SectionHeader sh = st.getHeader(entry.index); SectionData sd = new SectionData(); // Read any preamble - store if non-zero byte[] pa = readPreambleData(sh.getPointerToRawData(), dr); if (pa != null) sd.setPreamble(pa); // Read in the raw data block dr.jumpTo(sh.getPointerToRawData()); byte[] b = new byte[sh.getSizeOfRawData()]; dr.read(b); sd.setData(b); st.put(entry.index, sd); // Check for an image directory within this section int ddc = pe.getOptionalHeader().getDataDirectoryCount(); for (int i = 0; i < ddc; i++) { if (i == ImageDataDirectoryType.CERTIFICATE_TABLE) continue; ImageDataDirectory idd = pe.getOptionalHeader().getDataDirectory(i); if (idd.getSize() > 0) { int vad = sh.getVirtualAddress(); int vex = vad + sh.getVirtualSize(); int dad = idd.getVirtualAddress(); if (dad >= vad && dad < vex) { int off = dad - vad; IDataReader idr = new ByteArrayDataReader(b, off, idd.getSize()); DataEntry de = new DataEntry(i, 0); de.baseAddress = sh.getVirtualAddress(); readImageData(pe, de, idr); } } } }
public static DataEntry findNextEntry(PE pe, int pos) { DataEntry de = new DataEntry(); // Check sections first int ns = pe.getCoffHeader().getNumberOfSections(); for (int i = 0; i < ns; i++) { SectionHeader sh = pe.getSectionTable().getHeader(i); if (sh.getSizeOfRawData() > 0 && sh.getPointerToRawData() >= pos && (de.pointer == 0 || sh.getPointerToRawData() < de.pointer)) { de.pointer = sh.getPointerToRawData(); de.index = i; de.isSection = true; } } // Now check image data directories RVAConverter rvc = pe.getSectionTable().getRVAConverter(); int dc = pe.getOptionalHeader().getDataDirectoryCount(); for (int i = 0; i < dc; i++) { ImageDataDirectory idd = pe.getOptionalHeader().getDataDirectory(i); if (idd.getSize() > 0) { int prd = idd.getVirtualAddress(); // Assume certificate live outside section ? if (i != ImageDataDirectoryType.CERTIFICATE_TABLE && isInsideSection(pe, idd)) { prd = rvc.convertVirtualAddressToRawDataPointer(idd.getVirtualAddress()); } if (prd >= pos && (de.pointer == 0 || prd < de.pointer)) { de.pointer = prd; de.index = i; de.isSection = false; } } } // Check debug ImageData id = pe.getImageData(); DebugDirectory dd = null; if (id != null) dd = id.getDebug(); if (dd != null) { int prd = dd.getPointerToRawData(); if (prd >= pos && (de.pointer == 0 || prd < de.pointer)) { de.pointer = prd; de.index = -1; de.isDebugRawData = true; de.isSection = false; de.baseAddress = prd; } } if (de.pointer == 0) return null; return de; }
public static PE read(IDataReader dr) throws IOException { PE pe = new PE(); pe.setDosHeader(readDos(dr)); // Check if we have an old file type if (pe.getDosHeader().getAddressOfNewExeHeader() == 0 || pe.getDosHeader().getAddressOfNewExeHeader() > 8192) { return pe; } pe.setStub(readStub(pe.getDosHeader(), dr)); pe.setSignature(readSignature(dr)); // Check signature to ensure we have a pe/coff file if (!pe.getSignature().isValid()) { return pe; } pe.setCoffHeader(readCOFF(dr)); pe.setOptionalHeader(readOptional(dr)); pe.setSectionTable(readSectionHeaders(pe, dr)); pe.set64(pe.getOptionalHeader().isPE32plus()); // Now read the rest of the file DataEntry entry = null; while ((entry = findNextEntry(pe, dr.getPosition())) != null) { if (entry.isSection) { readSection(pe, entry, dr); } else if (entry.isDebugRawData) { readDebugRawData(pe, entry, dr); } else { readImageData(pe, entry, dr); } } // Read any trailing data byte[] tb = dr.readAll(); if (tb.length > 0) { pe.getImageData().setTrailingData(tb); } return pe; }
private static void readImageData(PE pe, DataEntry entry, IDataReader dr) throws IOException { // Read any preamble data ImageData id = pe.getImageData(); byte[] pa = readPreambleData(entry.pointer, dr); if (pa != null) id.put(entry.index, pa); // Read the image data ImageDataDirectory idd = pe.getOptionalHeader().getDataDirectory(entry.index); byte[] b = new byte[idd.getSize()]; dr.read(b); switch (entry.index) { case ImageDataDirectoryType.EXPORT_TABLE: id.setExportTable(readExportDirectory(b)); break; case ImageDataDirectoryType.IMPORT_TABLE: id.setImportTable(readImportDirectory(b, entry.baseAddress)); break; case ImageDataDirectoryType.RESOURCE_TABLE: id.setResourceTable(readResourceDirectory(b, entry.baseAddress)); break; case ImageDataDirectoryType.EXCEPTION_TABLE: id.setExceptionTable(b); break; case ImageDataDirectoryType.CERTIFICATE_TABLE: id.setCertificateTable(b); break; case ImageDataDirectoryType.BASE_RELOCATION_TABLE: id.setBaseRelocationTable(b); break; case ImageDataDirectoryType.DEBUG: id.setDebug(readDebugDirectory(b)); break; case ImageDataDirectoryType.ARCHITECTURE: id.setArchitecture(b); break; case ImageDataDirectoryType.GLOBAL_PTR: id.setGlobalPtr(b); break; case ImageDataDirectoryType.TLS_TABLE: id.setTlsTable(b); break; case ImageDataDirectoryType.LOAD_CONFIG_TABLE: id.setLoadConfigTable(readLoadConfigDirectory(pe, b)); break; case ImageDataDirectoryType.BOUND_IMPORT: id.setBoundImports(readBoundImportDirectoryTable(b)); break; case ImageDataDirectoryType.IAT: id.setIat(b); break; case ImageDataDirectoryType.DELAY_IMPORT_DESCRIPTOR: id.setDelayImportDescriptor(b); break; case ImageDataDirectoryType.CLR_RUNTIME_HEADER: id.setClrRuntimeHeader(b); break; case ImageDataDirectoryType.RESERVED: id.setReserved(b); break; } }