Skip to content

Automate packing with TELock

Alex edited this page Feb 12, 2022 · 4 revisions

Here is the script that was used to generate the TELock samples using GUI automation:

import codext
import os
import pyautogui as pag
import time
from pywinauto.application import Application

HOME = os.path.join(os.path.expanduser("~"), "Desktop")
READY_IMG_B64 = b"""iVBORw0KGgoAAAANSUhEUgAAAEYAAAAXCAYAAAC2/DnWAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJ
cEhZcwAADsMAAA7DAcdvqGQAAAErSURBVFhH7ZTRDcMgDESZinnYhjXyzy7MwgZuCZgYYldqC1FTOdKTgNgovpxtUkqgnFFhBMy2baCcUccI
qDACKozA98JED9YYMBQX+NhPCG6/0wXm3UKmCdM+fHYhfyNM3Vsfy74WhtACgyMusx5iPY/eHvEOhYngLY0L4HLMTHcS1jqGfWfBx7rGIsdz
koPi5X0RrMaNd09myYxBt9A/T2nFDLlH8Q4C3t8JVVzS4ojLZjPXMXXdC1P/8JDXvSPFvxamOsj5va1auy5geit1BbNClaJLi5R1Oe/F7YQg
exSKbSlpnfPeZP6MGYdiK4QUQ/L28+eAzTlcCx7Dd7gf2+hnhbmcIszKNsrcSpjDSWQGLeKGjrkGFUZAhREwoA/7qGMEVBgBFYYlwQNt1G77
WkCtwwAAAABJRU5ErkJggg=="""
READY_IMG = os.path.join(HOME, "telock-ready.png")
with open(READY_IMG, 'wb') as f:
	f.write(codext.decode(READY_IMG_B64, "base64"))

app = Application(backend="win32").start(os.path.join(HOME, "telock.exe")
dlg = app.window(title="tElock v0.98")

def pack(filename):
    fin = os.path.join(HOME, "not-packed", filename)
    bak = fin + ".bak"
    fout = os.path.join(HOME, "TELock", "telock_" + filename)
    # open the input file
    dlg.menu_select("File->Open")
    dlg2 = app.window(title="Select a File...")
    dlg2.Edit1.type_keys(fin)
    dlg2.Open.click()
    del dlg2
    # click on "Lock File"
    # (with PyAutoGUI, as trying to click an object from the main dialog box through PyWinAuto crashes)
    pag.click(620, 590)
    # wait for the status to become "Ready..." and then go further
    button = None
    while not button:
        try:
            dlg3 = app.window(title="Error")
            dlg3.Button.click()
            del dlg3
        except:
            pass
        try:
            dlg3 = app.window(title="Confirm")
            dlg3.Yes.click()
            del dlg3
        except:
            pass
        button = pag.locateCenterOnScreen(os.path.join(HOME, "telock-ready.png"), grayscale=True)
    # now rename and move files
    # if the size is the same between the locked and the backup files, then it failed
    while True:
        try:
            if os.stat(bak).st_size == os.stat(fin).st_size:
                os.remove(fin)
                print("FAILURE")
                break
            else:
                os.rename(fin, fout)
                print("SUCCESS")
                break
        except PermissionError:
            time.sleep(1)
        except FileNotFoundError:
            break
    os.rename(bak, fin)

last_one = None
for f in os.listdir("not-packed"):
    fout = os.path.join(HOME, "TELock", "telock_" + f)
    if os.path.exists(fout):
        last_one = f

for f in os.listdir("not-packed"):
    if last_one:
        if f == last_one:
            last_one = None
        continue
    m = "Packing '%s'..." % f
    m += " " * (40 - len(f))
    print(m, end="\t")
    pack(f)

os.remove(READY_IMG)