Autor Beitrag
scrooge
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 200



BeitragVerfasst: Fr 15.04.05 14:40 
Hi,

hab mal ne Frgae zu einem Source den ich geschrieben hab...aber nicht richtig funzt.
Mein Programmierstilist milde geagt besch*** (so dass es vielleicht doch en bischen mühe kostet sich das anzugucken, wäre aber nett, wenn sich trotzdem jemand erbarmen würde)

Das programm soll in der Lage sein neue Sections zu einem vorhandenden image hinzuzufügen.

Dummer scheints mit den Infos die ich in die neuen Sectiontable-einträge eintrage nicht lauffähig zu sein..vieellcht hakts aber trotzdem auch noch woanders

zu unten (wenns ums modifizieren der Checksumme geht... soweit ich das mitgekriegt hab ist die noch nichtmal nötig und habs nur noch schnell drangefügt (also wenn jemand fragt warum ich das da so blöd mache (im Endeffkt doppelt das image speichern))

einen teil der proc hab ich wegelassen weil der wohl außer wenn man so ziemlich die maximale Anzahl an möglichern Sections (96 wenn ich richtig liege ??) ausnutzt nie eintritt (führt ne dann nötige modifikation der Virt. Addressen durch).


dei arrays sectionflags,sectionflagsupdate und sectionvirtual beinhalten einmal
die sectionflags aller Sectins (auch der neuen), nur der alten und einmal die Vituellen Addresse und Größen aller Sections (auch der neuen).

hier mal der code:

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
 procedure Tform1.ModifySectionflags;
var savestream,loadstream :tfilestream;
i :Integer;
i2 :Cardinal;
i4 :Cardinal;
i3 :Cardinal;
i5 :Cardinal;
relocation :Cardinal;
dos :Pimagedosheader;
nt :Pimagentheaders;
Exefile :pointer;
newexefile,point :pointer;
Mappedfile :Cardinal;
NamePointerTable: ^Cardinal;
importdirectory :Pimage_import_entry;
Pseudosection : array of Image_section_header;
section :PImage_section_header;
exportSymbols :PIMAGE_EXPORT_DIRECTORY;
ExportAddrTable :^cardinal;
newnt :pimagentheaders;
thefile :Cardinal;
oldcheck,newchecksum :PDWORD;
mappedfile3 :pointer;
begin

if Savedialog1.Execute then
begin

  loadstream := tfilestream.Create(Exefilename,fmopenread);
  getmem(exefile,loadstream.size );
  loadstream.read(exefile^,loadstream.Size);
  dos := exefile;
  nt := Pointer(Integer(dos) + dos^._lfanew);
  i2 :=  high(sectionflags) - high(sectionflagsupdate);

  for i := 0 to high(sectionflagsupdate) do
  begin
  nt^.OptionalHeader.Sections[i].Characteristics := Sectionflags[i];
  end;

  if high(sectionflags) <> high(sectionflagsupdate) then
  begin

    setlength(pseudosection,i2);


    Zeromemory(@pseudosection[0],sizeof(image_section_header) * i2);

    getmem(newExefile,loadstream.size + i2 * sizeof(Image_section_header) );
    zeromemory(newexefile,loadstream.size + i2 * sizeof(Image_section_header));
    i4 := 4 + sizeof(image_file_Header) + nt^.FileHeader.SizeOfOptionalHeader + dos^._lfanew + nt^.FileHeader.NumberOfSections * sizeof(image_section_Header);
    copyMemory(newexefile,exefile,i4);



    // + i2 * sizeof(image_section_header);
    relocation := Cardinal((((Cardinal(i4 + i2 * sizeof(image_section_header)) mod nt^.OptionalHeader.SectionAlignment) > 0) = true)) * nt^.OptionalHeader.sectionAlignment + (Cardinal(i4 + i2 * sizeof(image_section_header)) div nt^.OptionalHeader.SectionAlignment) * nt^.OptionalHeader.SectionAlignment;

    relocation := relocation - Cardinal((((i4 mod nt^.OptionalHeader.SectionAlignment)) > 0) = true) * nt^.OptionalHeader.SectionAlignment - (i4 div nt^.OptionalHeader.SectionAlignment) * nt^.OptionalHeader.SectionAlignment;
    i3 := 0;

    for i := 0 to i2 - 1 do
    begin
    pseudosection[i].Virtualaddress := sectionvirtual[i + high(sectionflagsupdate) + 1][1] + Cardinal(relocation);
    pseudosection[i].VirtualSize := sectionvirtual[i + high(sectionflagsupdate) + 1][0];
    pseudosection[i].Characteristics := sectionflags[i + high(sectionflagsupdate) + 1];
    i3 := i3 + pseudosection[i].VirtualSize;
    end;

    for i := 0 to High(sectionflagsupdate) do
    begin
    i3 := i3 + sectionvirtual[i][0];
    end;



    CopyMemory(Pointer(Cardinal(newexefile) + i4),@pseudosection[0],sizeof(Image_section_header) * i2);



    Newnt := pointer(Integer(newexefile) + dos^._lfanew);
    newnt^.OptionalHeader.SizeOfImage := Cardinal((((Cardinal(newnt^.OptionalHeader.SizeOfImage + relocation) mod nt^.OptionalHeader.SectionAlignment) > 0) = true)) * nt^.OptionalHeader.SectionAlignment + (Cardinal(newnt^.OptionalHeader.sizeofimage + relocation) div nt^.OptionalHeader.sectionalignment) * nt^.OptionalHeader.SectionAlignment;
    newnt^.FileHeader.NumberOfSections := high(sectionflags) + 1;



    i3 := loadstream.size;

    if (newnt^.OptionalHeader.Sections[0].PointerToRawData) < (i2 *sizeof(image_section_header) + i4) then
    begin

      section := Pointer(Cardinal(newnt)+ sizeof(image_file_Header) + 228);

      section^.PointerToRawData :=  Cardinal((((i2 * sizeof(image_section_header) + i4) mod newnt^.OptionalHeader.SectionAlignment) > 0) = true) * newnt^.OptionalHeader.SectionAlignment + ((i2 * sizeof(image_section_header) + i4) div newnt^.OptionalHeader.SectionAlignment) * newnt^.OptionalHeader.SectionAlignment;
      i3 := section^.PointerToRawData;
      i4 := section^.SizeOfRawData;

      inc(section);

      for i := 1 to high(sectionflagsupdate) do
      begin
      section^.PointerToRawData := Cardinal((((i3 + i4) mod nt^.OptionalHeader.FileAlignment) <> 0) = true) * nt^.OptionalHeader.FileAlignment +  ((i3 + i4) div nt^.OptionalHeader.FileAlignment) * nt^.OptionalHeader.FileAlignment;
      i4 := section^.SizeOfRawData;
      i3 := section^.PointerToRawData;

      inc(section);
      end;
      dec(section);

      getmem(newexefile,i3 + i4 - 1);
      newnt := Pointer(Integer(dos));
      dos := newexefile;
      nt := Pointer(integer(dos) + dos^._lfanew);
      dos := Pointer(Integer(newnt));
      newnt := nt;
      nt := Pointer(integer(dos) + dos^._lfanew);
      i3 := i3 + i4 - 1;

    end;


    for i := 0 to high(sectionflagsupdate) do
    begin

    copymemory(Pointer(Cardinal(newexefile) + newnt^.OptionalHeader.Sections[i].PointerToRawData),Pointer(cardinal(dos) + nt^.OptionalHeader.Sections[i].PointerToRawData),nt^.OptionalHeader.sections[i].SizeOfRawData);
    end;

    savestream := tfilestream.Create(Savedialog1.FileName,fmcreate or fmOpenwrite or fmshareexclusive);
    savestream.Write(newexefile^,i3);
    loadstream.Free;
    thefile := CreateFile(Pchar(exefilename),GENERIC_Read, FILE_SHARE_READ or FILE_SHARE_WRITE,nil,open_always,FILE_ATTRIBUTE_NORMAL,0);
    mappedfile := createFilemapping(thefile,nil,PAGE_READONLY,0,0,nil);

    mappedfile3 := mapviewofFile(mappedfile,FILE_MAP_READ,0,0,i4);
    getmem(newchecksum,4);
    getmem(oldcheck,4);
    imagehlp.CheckSumMappedFile(mappedfile3,i4,oldcheck,newchecksum);
    newnt^.OptionalHeader.CheckSum := newchecksum^;
    savestream.Seek(0,SObeginning);
    savestream.Write(newexefile^,i4);
    savestream.free;

  end
  else
  begin


    savestream := tfilestream.Create(Savedialog1.FileName,fmcreate or fmOpenwrite or fmsharedenyread);
    savestream.Write(exefile^,loadstream.Size);

    savestream.free;
  end;

end;
end;


wenn ich das gespeichert hab zeigt erm ir auch das Icon und so an, aber sofort nach dem Öffnen sagt der Loader mir, sie sei nicht gültig (vielleicht hilfts das einzuschränken)

..wenn mir jemand da helfen könnte

schonmal danke im Vorraus


Zuletzt bearbeitet von scrooge am Fr 15.04.05 15:33, insgesamt 1-mal bearbeitet
Keldorn
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 2266
Erhaltene Danke: 4

Vista
D6 Prof, D 2005 Pro, D2007 Pro, DelphiXE2 Pro
BeitragVerfasst: Fr 15.04.05 15:09 
Bevor du das nicht ordentlich formatiert hast (sprich Einrückungen) wird sich das kaum jemand anschauen, da sieht ja keiner durch.

Mfg Frank

_________________
Lükes Grundlage der Programmierung: Es wird nicht funktionieren.
(Murphy)
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Fr 15.04.05 15:10 
Hallo!

Bitte ändere den Titel des Topics, da er wenig über das eigentlich Thema verrät. Hier der entsprechende Absatz aus den Delphi-Forum.de Richtlinien:

Richtlinien:
1.2 Beiträge
Bitte formuliere den Betreff Deiner Beiträge so, dass andere Mitglieder anhand dieser bereits das eigentliche Thema festmachen können. Beiträge wie etwa "Eine Anfängerfrage" oder "Weiß jemand, wie das geht?" lassen den Leser im Unklaren darüber, was das Thema der Diskussion ist.[...]

Einfach oben bei Deinem ersten Beitrag auf "Edit" klicken und den Titel ändern. Dank Dir!

Grüße
Christian :-)

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
scrooge Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 200



BeitragVerfasst: Fr 15.04.05 15:21 
wei rückt man nochmal ein ??
Keldorn
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 2266
Erhaltene Danke: 4

Vista
D6 Prof, D 2005 Pro, D2007 Pro, DelphiXE2 Pro
BeitragVerfasst: Fr 15.04.05 15:22 
in Delphi?

Codeblock markieren und mit STRG+Shift+I gehts nach rechts, mit STRG+Shift+U gehts ab nach links.

_________________
Lükes Grundlage der Programmierung: Es wird nicht funktionieren.
(Murphy)
scrooge Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 200



BeitragVerfasst: Fr 15.04.05 15:37 
so ok ??