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

olevba: UnicodeEncodeError when redirecting output to a file on Windows with Python 3 #667

Open
decalage2 opened this issue Mar 10, 2021 · 1 comment

Comments

@decalage2
Copy link
Owner

When the code of a VBA macro contains non-ASCII characters, olevba triggers a UnicodeEncodeError when the console output is redirected to a file, on Windows 10 with Python 3. The same file is processed properly when doing the same with Python 2, or when the output is printed on the console instead of being redirected.
Sample file:
test.zip (borrowed from https://github.com/kirk-sayre-work/talks/blob/master/test.docm)
Output:

c:\...>ovba3 test.docm >olevba.txt
Traceback (most recent call last):
  File "c:\Users\x\oletools\oletools\olevba.py", line 4077, in process_file
    print(vba_code_filtered)
  File "C:\Program Files\Python39\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\ufffd' in position 1197: character maps to <undefined>
ERROR    Error processing file test.docm ('charmap' codec can't encode character '\ufffd' in position 1197: character maps to <undefined>)!
Traceback (most recent call last):
  File "c:\Users\x\oletools\oletools\olevba.py", line 4077, in process_file
    print(vba_code_filtered)
  File "C:\Program Files\Python39\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\ufffd' in position 1197: character maps to <undefined>
@SystemsModelling
Copy link

SystemsModelling commented Jun 7, 2022

Philippe, may I suggest adding an option
-o outputfile
so that we don't have to use > redirection.
This makes it easy to use in commands like
FOR /R %F IN (*.xls *.xlsb *.xlsm) DO olevba "%F" -o "%F".VBA

And maybe set the errorlevel on exit so
0 = no VBA or macros found
1 = XLM macros
2 = VBA macros
4 = malicious suspect
etc

and then I could chain a command & IF errorlevel 0 DEL "%F".VBA
or maybe && DEL "%F".VBA if I have that syntax right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants