[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 814
  • Last Modified:

Reading/Writing into PE section

Hey there,

I am trying to write something into a new section of an executable, let's say the name of the section is .bla, I need to a function write a string buffer there.. then using a different function I need to read a buffer from this section..
P.S: I only need to use WINAPI in the reading function
Can anyone help me in such a code?

Thanks
0
circler
Asked:
circler
  • 2
  • 2
  • 2
  • +2
1 Solution
 
circlerAuthor Commented:
bump, no ideas?
0
 
circlerAuthor Commented:
Thanks, I've seen that but it's not what I need :)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
BdLmCommented:
which winapi function do you need ? Is this function in the links above ?
0
 
runaumCommented:
Use file mapping (WinAPI: CreateFileMapping) with SEC_IMAGE flag & PE structures from Windows.pas
<skipped>
var
  hPeFile, hPeFileMap: Cardinal;
  pBaseAddress: PByte;
  pDosHeader: PImageDosHeader;
  pNtHeaders: PImageNtHeaders;
  pSectionHeader: PImageSectionHeader;
  dwOffset: Cardinal;
  pSection: Pointer;
  SectName: String;
begin
  hPeFile := CreateFile(PChar(AFileName), GENERIC_READ, FILE_SHARE_READ,
    nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  if (hPeFile = INVALID_HANDLE_VALUE) then
    Exit;
 
  hPeFileMap := CreateFileMapping(hPeFile, nil, PAGE_READONLY or SEC_IMAGE,
    0, 0, nil);
  if (hPeFileMap = 0) then
  begin
    CloseHandle(hPeFile);
    Exit;
  end;
 
  pBaseAddress := MapViewOfFile(hPeFileMap, FILE_MAP_READ, 0, 0, 0);
  if (pBaseAddress = nil) then
  begin
    CloseHandle(hPeFileMap);
    CloseHandle(hPeFile);
    Exit;
  end;
 
  dwOffset := Cardinal(pBaseAddress);
  pDosHeader := PImageDosHeader(pBaseAddress);
  pNtHeaders := PImageNtHeaders(dwOffset + pDosHeader._lfanew);
 
  // Get sections info
  pSectionHeader := pImageSectionHeader(Cardinal(pNtHeaders) + SizeOf(TImageNtHeaders));
  for i := 1 to pNtHeaders.FileHeader.NumberOfSections do
  begin
    SetString(SectName, PChar(@pSectionHeader.Name), SizeOf(pSectionHeader.Name));
    SectName := Trim(SectName);
 
    pSection := Pointer(dwOffset + pSectionHeader.VirtualAddress);
    if (AnsiLowerCase(SectName) = '.bla') then
    begin
      // ToDo: Get your buffer here!
    end;
  end;
 
  // Don't forget to free resources & close all handles
</skipped>

Open in new window

0
 
cebassoCommented:
Hello,

Here are the solution for you...

http://www.delphibrasil.net/donwload/Bibliotecas/ExeMod.pas 

In the header of the file is explained how to use it...

Anyway, to insert the section to the file:

InsOrReplaceInFile('SECTION', FILEPATH, STRING);

and to Retrieve

ExtractFromFile('SECTION', FILEPATH, STRING_BUFFER_TO_RECEIVE);

Just declare on uses Clause the unit and enjoy!

it's perfect, i never got problem using that!

Regards,
Carlos
0
 
aikimarkCommented:
@circler

What is the context for this problem?
Why limit the reading of the file to Win API and not native Delphi I/O statements?
0
 
aikimarkCommented:
I think the cebasso comment, http:#26397197 should be accepted as the solution.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now