Improved security

This commit is contained in:
Eduard Urbach 2024-08-14 17:49:07 +02:00
parent 074ce25997
commit a80207d105
Signed by: eduard
GPG key ID: 49226B848C78F6C8
11 changed files with 42 additions and 77 deletions

View file

@ -14,7 +14,7 @@ const NumSections = 2
// EXE is the portable executable format used on Windows.
type EXE struct {
DOSHeader
PEHeader
NTHeader
OptionalHeader64
Sections [NumSections]SectionHeader
CodePadding []byte
@ -25,23 +25,23 @@ type EXE struct {
// New creates a new EXE file.
func New(code []byte, data []byte) *EXE {
codeStart := uint32(DOSHeaderSize + PEHeaderSize + OptionalHeader64Size + SectionHeaderSize*NumSections)
codePadding := common.Padding(codeStart, Align)
codeStart := uint32(DOSHeaderSize + NTHeaderSize + OptionalHeader64Size + SectionHeaderSize*NumSections)
codePadding := common.Padding(codeStart, config.Align)
codeStart += codePadding
dataStart := codeStart + uint32(len(code))
dataPadding := common.Padding(dataStart, Align)
dataPadding := common.Padding(dataStart, config.Align)
dataStart += dataPadding
imageSize := uint32(dataStart + uint32(len(data)))
imageSize += common.Padding(imageSize, Align)
imageSize += common.Padding(imageSize, config.Align)
return &EXE{
DOSHeader: DOSHeader{
Magic: [4]byte{'M', 'Z', 0, 0},
PEHeaderOffset: 0x40,
NTHeaderOffset: 0x40,
},
PEHeader: PEHeader{
NTHeader: NTHeader{
Signature: [4]byte{'P', 'E', 0, 0},
Machine: IMAGE_FILE_MACHINE_AMD64,
NumberOfSections: NumSections,
@ -54,9 +54,10 @@ func New(code []byte, data []byte) *EXE {
MinorLinkerVersion: 0x16,
SizeOfCode: uint32(len(code)),
AddressOfEntryPoint: codeStart,
BaseOfCode: codeStart,
ImageBase: config.BaseAddress,
SectionAlignment: Align, // power of 2, must be greater than or equal to FileAlignment
FileAlignment: Align, // power of 2
SectionAlignment: config.Align, // power of 2, must be greater than or equal to FileAlignment
FileAlignment: config.Align, // power of 2
MajorOperatingSystemVersion: 0x06,
MajorSubsystemVersion: 0x06,
SizeOfImage: imageSize,
@ -89,7 +90,7 @@ func New(code []byte, data []byte) *EXE {
},
Sections: [NumSections]SectionHeader{
{
Name: [8]byte{'.', 'c', 'o', 'd', 'e'},
Name: [8]byte{'.', 't', 'e', 'x', 't'},
VirtualSize: uint32(len(code)),
VirtualAddress: codeStart,
RawSize: uint32(len(code)), // must be a multiple of FileAlignment
@ -97,7 +98,7 @@ func New(code []byte, data []byte) *EXE {
Characteristics: IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ,
},
{
Name: [8]byte{'.', 'd', 'a', 't', 'a'},
Name: [8]byte{'.', 'r', 'd', 'a', 't', 'a'},
VirtualSize: uint32(len(data)),
VirtualAddress: dataStart,
RawSize: uint32(len(data)), // must be a multiple of FileAlignment
@ -115,7 +116,7 @@ func New(code []byte, data []byte) *EXE {
// Write writes the EXE file to the given writer.
func (pe *EXE) Write(writer io.Writer) {
binary.Write(writer, binary.LittleEndian, &pe.DOSHeader)
binary.Write(writer, binary.LittleEndian, &pe.PEHeader)
binary.Write(writer, binary.LittleEndian, &pe.NTHeader)
binary.Write(writer, binary.LittleEndian, &pe.OptionalHeader64)
binary.Write(writer, binary.LittleEndian, &pe.Sections)
binary.Write(writer, binary.LittleEndian, &pe.CodePadding)