Skip to content

Set correct PE checksum for .exe files (helps with virus false-positives) #5579

Closed

Description

@The-Compiler

Is your feature request related to a problem? Please describe.
As you've probably all seen before, virus scanner false-positives are a recurring problem for PyInstaller-generated executables, as well as PyInstaller's bootloader (see e.g. #5490, #5474, #5479, ...).

One piece of information virus scanners seem to use is the PE checksum. Quoting from that article:

  • 83% of malware had invalid checksums
  • 90% of legitimate files had valid checksums.

It looks like anything generated from PyInstaller (as well as the Windows bootloader in the release?) seems to have a checksum of 00 00 00 00, which is thus invalid.

Describe the solution you'd like
PyInstaller should set a correct PE checksum. This is possible using pefile, which it already seems to depend on:

import pefile
pe = pefile.PE('filename.exe')
pe.OPTIONAL_HEADER.CheckSum = pe.generate_checksum()
pe.close()
pe.write('filename.exe')

(You can use pe.verify_checksum() to verify that it is correct)

After doing this, e.g. PyInstaller's Windows-64bit/runw.exe (in the 4.2 release) goes down from 12 false-positives to only 5 - most notably, McAfee and Avast (which are the two affected engines I've actually heard of before...) now are happy with the file.

Doing this with my own project (which is built with PyInstaller), @bitraid found that:

the number of false positives for v2.0.2 goes down from 31 to 7. Similarly v2.0.0 goes down to 6 from 29 and v2.0.1 goes down to 8 from 15.

Describe alternatives you've considered
Submitting false-positive reports to antivirus vendors. A pain.

Also, doing this after PyInstaller has generated the exe - that works, but it seems to me like this should be done in PyInstaller itself.

Additional context
See qutebrowser/qutebrowser#6194

Activity

  1. bwoodsend commented on Feb 22, 2021

    @bwoodsend
    Member

    Ah yes! Nice one. How that makes an executable any more trustworthy seeing as anyone could do it is beyond me but if it shuts up the bogus AV software then great.

  2. bwoodsend commented on Feb 22, 2021

    @bwoodsend
    Member

    @The-Compiler Fancy making the changes or shall I?

  3. The-Compiler commented on Feb 22, 2021

    @The-Compiler
    ContributorAuthor

    Feel free! I don't think I've contributed before (looks like I did 😆)

    I'm not really sure what the best place for such a change would be - but I also try to avoid working on Windows stuff if I can, so if you don't mind, go ahead. 👍

  4. bwoodsend commented on Feb 22, 2021

    @bwoodsend
    Member

    Alright then. I'll blow the cobwebs off my own Windows partition and do it...

  5. self-assigned this
    on Feb 22, 2021
  6. bwoodsend commented on Feb 23, 2021

    @bwoodsend
    Member

    I've done it (#5580). EXEs built by PyInstaller will now have the checksum set. The bootloaders won't until we rebuild them so unless you rebuild the bootloaders yourself, there is no point submitting those to virustotal yet. Instead submit your built applications. I have tried rebuilding a bootloader then submitting it and it came back all green.

  7. andyshinn commented on Feb 27, 2021

    @andyshinn

    I tried with the new PE changes and it's still blocked in Defender and flagged by a bunch in Virus Total, unfortunately.

    https://www.virustotal.com/gui/file/a70a7c7b5682abe70dfe7bd186e6f48b636e2a59908bcbdfc0184a4e52cc1635/detection

  8. Legorooj commented on Feb 27, 2021

    @Legorooj
    Member

    Interesting, using commit f078351, I get the following result:

    https://www.virustotal.com/gui/file/f5587397e6ce11d659e3b4cfe1637991b9b5547a3f280873ef48948e606f7434/detection

    Just a build of print('Hello world!') as a onefile was what I used.

  9. Legorooj commented on Feb 27, 2021

    @Legorooj
    Member

    @sjackman that's Internet Archive flag is on version 3.6... a little out of date.

  10. srkn commented on Mar 23, 2021

    @srkn

    I've done it (#5580). EXEs built by PyInstaller will now have the checksum set. The bootloaders won't until we rebuild them so unless you rebuild the bootloaders yourself, there is no point submitting those to virustotal yet. Instead submit your built applications. I have tried rebuilding a bootloader then submitting it and it came back all green.

    Would you please give a little more detail regarding this? How should we rebuild bootloader to prevent being flagged as malware?

  11. bwoodsend commented on Mar 23, 2021

    @bwoodsend
    Member

    Rebuilding the bootloader only affects people who can't install pyinstaller. If that includes you then the docs are here.

  12. locked as resolved and limited conversation to collaborators on Nov 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

featureFeature request

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions