From cedaf4809f7c575ebb3113f0ff4a07b3feb097b7 Mon Sep 17 00:00:00 2001 From: ed Date: Tue, 21 Feb 2023 19:18:10 +0000 Subject: [PATCH] add exe integrity selfcheck --- scripts/pyinstaller/build.sh | 5 +++++ scripts/pyinstaller/loader.py | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/scripts/pyinstaller/build.sh b/scripts/pyinstaller/build.sh index 071b923f..6863fb77 100644 --- a/scripts/pyinstaller/build.sh +++ b/scripts/pyinstaller/build.sh @@ -96,4 +96,9 @@ $APPDATA/python/python$pyv/scripts/pyinstaller \ # ./upx.exe --best --ultra-brute --lzma -k dist/copyparty.exe +printf $(sha512sum ~/Downloads/dist/copyparty.exe | head -c 18 | sed -r 's/(..)/\\x\1/g') | +base64 | head -c12 >> dist/copyparty.exe + +dist/copyparty.exe --version + curl -fkT dist/copyparty.exe -b cppwd=wark https://192.168.123.1:3923/copyparty$m.exe diff --git a/scripts/pyinstaller/loader.py b/scripts/pyinstaller/loader.py index 28f88e9c..0f19cd0f 100644 --- a/scripts/pyinstaller/loader.py +++ b/scripts/pyinstaller/loader.py @@ -1,5 +1,7 @@ # coding: utf-8 +import base64 +import hashlib import os import re import shutil @@ -46,6 +48,30 @@ def confirm(rv): sys.exit(rv or 1) +def ckck(): + hs = hashlib.sha512() + with open(sys.executable, "rb") as f: + f.seek(-12, 2) + rem = f.tell() + esum = f.read().decode("ascii", "replace") + f.seek(0) + while rem: + buf = f.read(min(rem, 64 * 1024)) + rem -= len(buf) + hs.update(buf) + if not buf: + t = "unexpected eof @ {} with {} left" + raise Exception(t.format(f.tell(), rem)) + + fsum = base64.b64encode(hs.digest()[:9]).decode("utf-8") + if fsum != esum: + t = "exe integrity check error; [{}] != [{}]" + raise Exception(t.format(esum, fsum)) + + +ckck() + + def meicln(mod): pdir, mine = os.path.split(mod) dirs = os.listdir(pdir)