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

📄 access.c

📁 这是一个开放源代码的与WINNT/WIN2K/WIN2003兼容的操作系统
💻 C
📖 第 1 页 / 共 2 页
字号:
    pLoadedImage->Sections = IMAGE_FIRST_SECTION(NtHeader);
    pLoadedImage->NumberOfSections = NtHeader->FileHeader.NumberOfSections;

    /* Setup other data */
    pLoadedImage->SizeOfImage = NtHeader->OptionalHeader.SizeOfImage;
    pLoadedImage->Characteristics = NtHeader->FileHeader.Characteristics;
    pLoadedImage->LastRvaSection = pLoadedImage->Sections;
    pLoadedImage->fSystemImage = FALSE; /* FIXME */
    pLoadedImage->fDOSImage = FALSE; /* FIXME */
    InitializeListHead(&pLoadedImage->Links);

    /* Check if it was read-only */
    if (ReadOnly)
    {
        /* It was, so close our handle and write it as invalid */
        CloseHandle(hFile);
        pLoadedImage->hFile = INVALID_HANDLE_VALUE;
    }
    else
    {
        /* Write our file handle */
        pLoadedImage->hFile = hFile;
    }

    /* Return Success */
    return TRUE;
}

/***********************************************************************
 *		SetImageConfigInformation (IMAGEHLP.@)
 */
BOOL IMAGEAPI SetImageConfigInformation(
  PLOADED_IMAGE LoadedImage,
  PIMAGE_LOAD_CONFIG_DIRECTORY ImageConfigInformation)
{
  FIXME("(%p, %p): stub\n",
    LoadedImage, ImageConfigInformation
  );
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
  return FALSE;
}

/***********************************************************************
 *		UnMapAndLoad (IMAGEHLP.@)
 */
BOOL IMAGEAPI UnMapAndLoad(PLOADED_IMAGE Image)
{
    PIMAGE_NT_HEADERS NtHeader;
    DWORD HeaderCheckSum, CheckSum;

    /* Check if the image was read-only */
    if (Image->hFile == INVALID_HANDLE_VALUE)
    {
        /* We'll only unmap the view */
        UnmapViewOfFile(Image->MappedAddress);
    }
    else
    {
        /* Calculate the checksum */
        CheckSumMappedFile(Image->MappedAddress,
                           Image->SizeOfImage,
                           &HeaderCheckSum,
                           &CheckSum);

        /* Get the NT Header */
        NtHeader = Image->FileHeader;

        /* Write the new checksum to it */
        NtHeader->OptionalHeader.CheckSum = CheckSum;

        /* Now flush and unmap the image */
        FlushViewOfFile(Image->MappedAddress, Image->SizeOfImage);
        UnmapViewOfFile(Image->MappedAddress);

        /* Check if the size changed */
        if (Image->SizeOfImage != GetFileSize(Image->hFile, NULL))
        {
            /* Update the file pointer */
            SetFilePointer(Image->hFile, Image->SizeOfImage, NULL, FILE_BEGIN);
            SetEndOfFile(Image->hFile);
        }
    }

    /* Check if the image had a valid handle, and close it */
    if (Image->hFile != INVALID_HANDLE_VALUE) CloseHandle(Image->hFile);

    /* Return success */
    return TRUE;
}

PVOID
IMAGEAPI
ImageDirectoryEntryToData32(PVOID Base,
                            BOOLEAN MappedAsImage,
                            USHORT DirectoryEntry,
                            PULONG Size,
                            PIMAGE_SECTION_HEADER *FoundHeader OPTIONAL,
                            PIMAGE_FILE_HEADER FileHeader,
                            PIMAGE_OPTIONAL_HEADER OptionalHeader)
{
    ULONG i;
    PIMAGE_SECTION_HEADER CurrentSection;
    ULONG DirectoryEntryVA;

    /* Check if this entry is invalid */
    if (DirectoryEntry >= OptionalHeader->NumberOfRvaAndSizes)
    {
        /* Nothing found */
        *Size = 0;
        return NULL;
    }

    /* Get the VA of the Directory Requested */
    DirectoryEntryVA = OptionalHeader->DataDirectory[DirectoryEntry].VirtualAddress;
    if (!DirectoryEntryVA)
    {
        /* It doesn't exist */
        *Size = 0;
        return NULL;
    }

    /* Get the size of the Directory Requested */
    *Size = OptionalHeader->DataDirectory[DirectoryEntry].Size;

    /* Check if it was mapped as an image or if the entry is within the headers */
    if ((MappedAsImage) || (DirectoryEntryVA < OptionalHeader->SizeOfHeaders))
    {
        /* No header found */
        if (FoundHeader) *FoundHeader = NULL;
        
        /* And simply return the VA */
        return (PVOID)((ULONG_PTR)Base + DirectoryEntryVA);
    }

    /* Read the first Section */
    CurrentSection = (PIMAGE_SECTION_HEADER)((ULONG_PTR)OptionalHeader +
                                             FileHeader->SizeOfOptionalHeader);
    
    /* Loop through every section*/
    for (i = 0; i < FileHeader->NumberOfSections; i++)
    {    
        /* If the Directory VA is located inside this section's VA, then this section belongs to this Directory */
        if ((DirectoryEntryVA >= CurrentSection->VirtualAddress) && 
            (DirectoryEntryVA < (CurrentSection->VirtualAddress +
                                 CurrentSection->SizeOfRawData)))
        {
            /* Return the section header */
            if (FoundHeader) *FoundHeader = CurrentSection;
            return ((PVOID)((ULONG_PTR)Base +
                            (DirectoryEntryVA - CurrentSection->VirtualAddress) +
                            CurrentSection->PointerToRawData));
        }

        /* Move to the next section */
        CurrentSection++;
    }

    /* If we got here, then we didn't find anything */
    return NULL;
}

/*
 * @unimplemented
 */
DWORD
IMAGEAPI
GetTimestampForLoadedLibrary(HMODULE Module)
{
    UNIMPLEMENTED;
    SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
    return 0;
}

/*
 * @implemented
 */
PVOID 
IMAGEAPI
ImageDirectoryEntryToData(PVOID Base,
                          BOOLEAN MappedAsImage,
                          USHORT DirectoryEntry,
                          PULONG Size)
{
    /* Let the extended function handle it */
    return ImageDirectoryEntryToDataEx(Base,
                                       MappedAsImage,
                                       DirectoryEntry,
                                       Size,
                                       NULL);
}

/*
 * @implemented
 */
PVOID
IMAGEAPI
ImageDirectoryEntryToDataEx(IN PVOID Base,
                            IN BOOLEAN MappedAsImage,
                            IN USHORT DirectoryEntry,
                            OUT PULONG Size,
                            OUT PIMAGE_SECTION_HEADER *FoundSection OPTIONAL)
{
    PIMAGE_NT_HEADERS NtHeader;
    PIMAGE_FILE_HEADER FileHeader;
    PIMAGE_OPTIONAL_HEADER OptionalHeader;

    /* Get the optional header ourselves */
    NtHeader = ImageNtHeader(Base);
    FileHeader = &NtHeader->FileHeader;
    OptionalHeader = &NtHeader->OptionalHeader;

    /* FIXME: Read image type and call appropriate function (32, 64, ROM) */
    return ImageDirectoryEntryToData32(Base,
                                       MappedAsImage,
                                       DirectoryEntry,
                                       Size,
                                       FoundSection,
                                       FileHeader,
                                       OptionalHeader);
}

/*
 * @implemented
 */
PIMAGE_SECTION_HEADER 
IMAGEAPI
ImageRvaToSection(IN PIMAGE_NT_HEADERS NtHeaders,
                  IN PVOID Base,
                  IN ULONG Rva)
{
    PIMAGE_SECTION_HEADER Section;
    ULONG i;

    /* Get the First Section */
    Section = IMAGE_FIRST_SECTION(NtHeaders);

    /* Look through each section */
    for (i = 0; i < NtHeaders->FileHeader.NumberOfSections; i++)
    {
        /* Check if the RVA is in between */
        if ((Rva >= Section->VirtualAddress) && 
            (Rva < (Section->VirtualAddress + Section->SizeOfRawData)))
        {
            /* Return this section */
            return Section;
        }

        /* Move to the next section */
        Section++;
    }

    /* Not Found */
    return NULL;
}

/*
 * @implemented
 */
PIMAGE_NT_HEADERS
IMAGEAPI
ImageNtHeader(PVOID Base)
{
    /* Let RTL do it */
    return RtlImageNtHeader(Base);
}

/*
 * @implemented
 */
PVOID 
IMAGEAPI 
ImageRvaToVa(IN PIMAGE_NT_HEADERS NtHeaders,
             IN PVOID Base,
             IN ULONG Rva,
             IN OUT PIMAGE_SECTION_HEADER *LastRvaSection OPTIONAL)
{
    PIMAGE_SECTION_HEADER Section;

    /* Get the Section Associated */
    Section = ImageRvaToSection(NtHeaders, Base, Rva);

    /* Return it, if specified */
    if (LastRvaSection) *LastRvaSection = Section;

    /* Return the VA */
    return (PVOID)((ULONG_PTR)Base + (Rva - Section->VirtualAddress) +
                                            Section->PointerToRawData);
}

BOOL
IMAGEAPI
UnloadAllImages(VOID)
{
    PLIST_ENTRY Head, Entry;
    PLOADED_IMAGE CurrentImage;

    /* Make sure we're initialized */
    if (!DllListInitialized) return TRUE;

    /* Get the list pointers and loop */
    Head = &ImageLoadListHead;
    Entry = Head->Flink;
    while (Entry != Head)
    {
        /* Get this image */
        CurrentImage = CONTAINING_RECORD(Entry, LOADED_IMAGE, Links);

        /* Move to the next entry */
        Entry = Entry->Flink;

        /* Unload it */
        ImageUnload(CurrentImage);
    }

    /* We are not initialized anymore */
    DllListInitialized = FALSE;
    return TRUE;
}

⌨️ 快捷键说明

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