示例#1
0
 private static void readDebugRawData(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.setDebugRawDataPreamble(pa);
   DebugDirectory dd = id.getDebug();
   byte[] b = new byte[dd.getSizeOfData()];
   dr.read(b);
   id.setDebugRawData(b);
 }
示例#2
0
  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;
  }
示例#3
0
  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;
  }
示例#4
0
  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;
    }
  }