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);
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; |