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

Fix get_memory_mapped_image copy much header data #385

Closed
wants to merge 1 commit into from

Conversation

qux-bbb
Copy link

@qux-bbb qux-bbb commented Nov 25, 2023

Before this changing, some non b"\0" data will be copied to the header.
After this changing, the header will be clear.

@erocarrera
Copy link
Owner

I want to understand better what is the reason for this change? does it mirror the behavior of the OS or is it simply to keep it "clean" of the values outside the header?
I have the recollection (but I have not verified it) that the OS does not clear the values when mapping the file into memory. pefile attempts to mirror that behavior.

@erocarrera erocarrera self-assigned this Jan 11, 2024
@qux-bbb
Copy link
Author

qux-bbb commented Jan 12, 2024

This change want to mirror the behavior of the OS.
The OS just maps PE file to memory. For mapped header, it does not contain latter data.
But before this commit, pefile will map latter data to header in memory.

@erocarrera
Copy link
Owner

This change does not correctly handle the mapped data so far.

@erocarrera erocarrera closed this Aug 26, 2024
@qux-bbb
Copy link
Author

qux-bbb commented Aug 26, 2024

Maybe I should find a sample to prove it.

@erocarrera
Copy link
Owner

Hello, that would be great. It's always good to have cases for the test suite.
But I was talking more about the PR, the code appears to simply take the header and add the padding, disregarding any other previous data what might have been added while processing sections and already in mapped_data.

@erocarrera erocarrera reopened this Aug 26, 2024
@qux-bbb
Copy link
Author

qux-bbb commented Aug 26, 2024

Because I think that if padding_length < 0, then this is a header section. Isn't it?

erocarrera added a commit that referenced this pull request Aug 26, 2024
@erocarrera
Copy link
Owner

Just changing the initial contents of "mapped_data" should lead to the behavior you desire. I've made the change in commit 14a4c71. What do you think?

@qux-bbb
Copy link
Author

qux-bbb commented Aug 26, 2024

14a4c71
That is a good change. Thank you! You can close this issue.
What I cannot understand is that Is there other situation about "padding_length < 0"?

@erocarrera
Copy link
Owner

Yes, out of curiosity I ran over all the files I use for my tests and there are a few with negative padding_length, both when encountering the first section and with later sections as well. It's mostly the case with malware or slightly malformed files.

@erocarrera erocarrera closed this Aug 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants