Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter out invalid UTF-8 characters from section names. #455

Merged
merged 1 commit into from
Jun 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 40 additions & 29 deletions src/AsmResolver.PE.File/Headers/SectionHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,6 @@ public class SectionHeader : SegmentBase, IOffsetConverter

private string _name;

/// <summary>
/// Reads a single section header at the current position of the provided input stream.
/// </summary>
/// <param name="reader">The input stream to read from.</param>
/// <returns>The section header that was read.</returns>
public static SectionHeader FromReader(ref BinaryStreamReader reader)
{
ulong offset = reader.Offset;
uint rva = reader.Rva;

var nameBytes = new byte[8];
reader.ReadBytes(nameBytes, 0, nameBytes.Length);

return new SectionHeader(Encoding.UTF8.GetString(nameBytes).Replace("\0", ""), 0)
{
Offset = offset,
Rva = rva,
VirtualSize = reader.ReadUInt32(),
VirtualAddress = reader.ReadUInt32(),
SizeOfRawData = reader.ReadUInt32(),
PointerToRawData = reader.ReadUInt32(),
PointerToRelocations = reader.ReadUInt32(),
PointerToLineNumbers = reader.ReadUInt32(),
NumberOfRelocations = reader.ReadUInt16(),
NumberOfLineNumbers = reader.ReadUInt16(),
Characteristics = (SectionFlags) reader.ReadUInt32()
};
}

/// <summary>
/// Creates a new section header with the provided name.
/// </summary>
Expand Down Expand Up @@ -194,6 +165,46 @@ public SectionFlags Characteristics
set;
}

/// <summary>
/// Reads a single section header at the current position of the provided input stream.
/// </summary>
/// <param name="reader">The input stream to read from.</param>
/// <returns>The section header that was read.</returns>
public static SectionHeader FromReader(ref BinaryStreamReader reader)
{
ulong offset = reader.Offset;
uint rva = reader.Rva;

// Read name field.
byte[] nameBytes = new byte[8];
reader.ReadBytes(nameBytes, 0, nameBytes.Length);

// Interpret as UTF-8, discarding all invalid UTF-8 characters.
string name = Encoding.UTF8.GetString(nameBytes).Replace("\xfffd", "");

// Trim to last null-byte if it exists.
int index = name.IndexOf('\0');
if (index >= 0)
name = name.Remove(index);

return new SectionHeader(name, 0)
{
Offset = offset,
Rva = rva,

// Read remainder of section header.
VirtualSize = reader.ReadUInt32(),
VirtualAddress = reader.ReadUInt32(),
SizeOfRawData = reader.ReadUInt32(),
PointerToRawData = reader.ReadUInt32(),
PointerToRelocations = reader.ReadUInt32(),
PointerToLineNumbers = reader.ReadUInt32(),
NumberOfRelocations = reader.ReadUInt16(),
NumberOfLineNumbers = reader.ReadUInt16(),
Characteristics = (SectionFlags) reader.ReadUInt32()
};
}

/// <inheritdoc />
public override uint GetPhysicalSize() => SectionHeaderSize;

Expand Down
14 changes: 14 additions & 0 deletions test/AsmResolver.PE.File.Tests/PEFileTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,5 +240,19 @@ public void RemoveExistingEofData()
var newFile = PEFile.FromBytes(newFileBytes);
Assert.Null(newFile.EofData);
}

[Fact]
public void ReadSections()
{
var file = PEFile.FromBytes(Properties.Resources.HelloWorld);
Assert.Equal(new[] {".text", ".rsrc", ".reloc"}, file.Sections.Select(x => x.Name));
}

[Fact]
public void ReadInvalidSectionName()
{
var file = PEFile.FromBytes(Properties.Resources.HelloWorld_InvalidSectionName);
Assert.Equal(new[] {".text", ".rsrc", ".reloc"}, file.Sections.Select(x => x.Name));
}
}
}
10 changes: 10 additions & 0 deletions test/AsmResolver.PE.File.Tests/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions test/AsmResolver.PE.File.Tests/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@
<data name="HelloWorld_EOF" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\HelloWorld.EOF.exe;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="NativeMemoryDemos" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\NativeMemoryDemos.exe;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="HelloWorld_InvalidSectionName" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\HelloWorld.InvalidSectionName.exe;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="NativeMemoryDemos" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\NativeMemoryDemos.exe;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
</root>
Binary file not shown.