Python reading a PE file and changing resource section
I am trying to open a Windows PE file and alter some strings in the resource section.
f = open('c:testfile.exe', 'rb')
file = f.read()
if b'A'*10 in file:
s = file.replace(b'A'*10, newstring)
In the resource section I have a string that is just:
AAAAAAAAAA
And I want to replace that with something else. When I read the file I get:
x00Ax00Ax00Ax00Ax00Ax00Ax00Ax00Ax00Ax00A
I have tried opening with UTF-16 and decoding as UTF-16 but then I run into a error:
UnicodeDecodeError: 'utf-16-le' codec can't decode bytes in position 1604-1605: illegal encoding
Everyone I seen who had the same issue fixed by decoding to UTF-16. I am not sure why this doesn't work for me.
python python-3.x
add a comment |
I am trying to open a Windows PE file and alter some strings in the resource section.
f = open('c:testfile.exe', 'rb')
file = f.read()
if b'A'*10 in file:
s = file.replace(b'A'*10, newstring)
In the resource section I have a string that is just:
AAAAAAAAAA
And I want to replace that with something else. When I read the file I get:
x00Ax00Ax00Ax00Ax00Ax00Ax00Ax00Ax00Ax00A
I have tried opening with UTF-16 and decoding as UTF-16 but then I run into a error:
UnicodeDecodeError: 'utf-16-le' codec can't decode bytes in position 1604-1605: illegal encoding
Everyone I seen who had the same issue fixed by decoding to UTF-16. I am not sure why this doesn't work for me.
python python-3.x
What codes are those, in that position 1604-1605? Windows does not use UTF-16, it has a multibyte wide encoding of its own. Presumably – just like with a UTF-8 file that contains nothing higher than a tilde – those others for whom it worked just got lucky.
– usr2564301
Nov 21 '18 at 13:20
add a comment |
I am trying to open a Windows PE file and alter some strings in the resource section.
f = open('c:testfile.exe', 'rb')
file = f.read()
if b'A'*10 in file:
s = file.replace(b'A'*10, newstring)
In the resource section I have a string that is just:
AAAAAAAAAA
And I want to replace that with something else. When I read the file I get:
x00Ax00Ax00Ax00Ax00Ax00Ax00Ax00Ax00Ax00A
I have tried opening with UTF-16 and decoding as UTF-16 but then I run into a error:
UnicodeDecodeError: 'utf-16-le' codec can't decode bytes in position 1604-1605: illegal encoding
Everyone I seen who had the same issue fixed by decoding to UTF-16. I am not sure why this doesn't work for me.
python python-3.x
I am trying to open a Windows PE file and alter some strings in the resource section.
f = open('c:testfile.exe', 'rb')
file = f.read()
if b'A'*10 in file:
s = file.replace(b'A'*10, newstring)
In the resource section I have a string that is just:
AAAAAAAAAA
And I want to replace that with something else. When I read the file I get:
x00Ax00Ax00Ax00Ax00Ax00Ax00Ax00Ax00Ax00A
I have tried opening with UTF-16 and decoding as UTF-16 but then I run into a error:
UnicodeDecodeError: 'utf-16-le' codec can't decode bytes in position 1604-1605: illegal encoding
Everyone I seen who had the same issue fixed by decoding to UTF-16. I am not sure why this doesn't work for me.
python python-3.x
python python-3.x
asked Nov 21 '18 at 13:06
DoritosDoritos
326
326
What codes are those, in that position 1604-1605? Windows does not use UTF-16, it has a multibyte wide encoding of its own. Presumably – just like with a UTF-8 file that contains nothing higher than a tilde – those others for whom it worked just got lucky.
– usr2564301
Nov 21 '18 at 13:20
add a comment |
What codes are those, in that position 1604-1605? Windows does not use UTF-16, it has a multibyte wide encoding of its own. Presumably – just like with a UTF-8 file that contains nothing higher than a tilde – those others for whom it worked just got lucky.
– usr2564301
Nov 21 '18 at 13:20
What codes are those, in that position 1604-1605? Windows does not use UTF-16, it has a multibyte wide encoding of its own. Presumably – just like with a UTF-8 file that contains nothing higher than a tilde – those others for whom it worked just got lucky.
– usr2564301
Nov 21 '18 at 13:20
What codes are those, in that position 1604-1605? Windows does not use UTF-16, it has a multibyte wide encoding of its own. Presumably – just like with a UTF-8 file that contains nothing higher than a tilde – those others for whom it worked just got lucky.
– usr2564301
Nov 21 '18 at 13:20
add a comment |
1 Answer
1
active
oldest
votes
If resource inside binary file is encoded to utf-16, you shouldn't change encoding.
try this
f = open('c:\test\file.exe', 'rb')
file = f.read()
unicode_str = u'AAAAAAAAAA'
encoded_str = unicode_str.encode('UTF-16')
if encoded_str in file:
s = file.replace(encoded_str, new_utf_string.encode('UTF-16'))
inside binary file everything is encoded, keep in mind
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53412728%2fpython-reading-a-pe-file-and-changing-resource-section%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If resource inside binary file is encoded to utf-16, you shouldn't change encoding.
try this
f = open('c:\test\file.exe', 'rb')
file = f.read()
unicode_str = u'AAAAAAAAAA'
encoded_str = unicode_str.encode('UTF-16')
if encoded_str in file:
s = file.replace(encoded_str, new_utf_string.encode('UTF-16'))
inside binary file everything is encoded, keep in mind
add a comment |
If resource inside binary file is encoded to utf-16, you shouldn't change encoding.
try this
f = open('c:\test\file.exe', 'rb')
file = f.read()
unicode_str = u'AAAAAAAAAA'
encoded_str = unicode_str.encode('UTF-16')
if encoded_str in file:
s = file.replace(encoded_str, new_utf_string.encode('UTF-16'))
inside binary file everything is encoded, keep in mind
add a comment |
If resource inside binary file is encoded to utf-16, you shouldn't change encoding.
try this
f = open('c:\test\file.exe', 'rb')
file = f.read()
unicode_str = u'AAAAAAAAAA'
encoded_str = unicode_str.encode('UTF-16')
if encoded_str in file:
s = file.replace(encoded_str, new_utf_string.encode('UTF-16'))
inside binary file everything is encoded, keep in mind
If resource inside binary file is encoded to utf-16, you shouldn't change encoding.
try this
f = open('c:\test\file.exe', 'rb')
file = f.read()
unicode_str = u'AAAAAAAAAA'
encoded_str = unicode_str.encode('UTF-16')
if encoded_str in file:
s = file.replace(encoded_str, new_utf_string.encode('UTF-16'))
inside binary file everything is encoded, keep in mind
answered Nov 21 '18 at 13:26
hamilyonhamilyon
36818
36818
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53412728%2fpython-reading-a-pe-file-and-changing-resource-section%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What codes are those, in that position 1604-1605? Windows does not use UTF-16, it has a multibyte wide encoding of its own. Presumably – just like with a UTF-8 file that contains nothing higher than a tilde – those others for whom it worked just got lucky.
– usr2564301
Nov 21 '18 at 13:20