Writing a Windows locker in Python 3.x

Greetings to python lovers. Once I accidentally stuck my finger into ctypes. And you know, I liked it. Especially the keyboard and mouse input block. And the first thing that came to my mind was, "Why not write a locker for Windows in python, with unlocking on a USB flash drive, like a key" (Don't ask why, I don't know myself). And then my imagination went. Now I will write down what you have to face to write it.



Well, let's get started!



The first step is to create a graph part of the locker. We will not make a stupidly white sheet: d

We will name it. ... ... locker.pyw





Why pyw? Yes, because when you start the locker, the console will come out, which a potential bad uncle can close, then the whole locker will be covered, but we do not need it.





import hashlib
import time
import sys
import os
from tkinter import Tk, Entry, Label
import tkinter
import pyautogui
import threading
from lofu import *
      
      



Importing libraries for locker.pyw





* hashlib we need to save the key in a hash form, so there is less chance of scrap (if you don't need it, you don't have to import it!)







* time is just for sleep ()







* sys for nice exit exit ()







* os to start the task of the system () part of the locker







* tkinter this is our graph part







* pyautogui to move the mouse to a corner (let him think about his behavior)







* threading for the second thread of mouse offset (so as not to interfere with the main thread)







* lofu. ... ... ... I put a couple of functions, just in another file and that's it, I'm an artist, as I see it!





Let's make the function of moving the mouse to the side so that the user does not have time to click anywhere.





def mouse_trac(screen_width, screen_height):
	while True:
		pyautogui.moveTo(screen_width, screen_height)
      
      



pyautogui.moveTo(screen_width, screen_height) - ( , )





screen_width, screen_height

Tk(), ,





root = Tk()

root.attributes('-fullscreen', True)
pyautogui.FAILSAFE = False
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
      
      



root.attributes('-fullscreen', True) - .





pyautogui.FAILSAFE = False - , .





screen_width = root.winfo_screenwidth() screen_height = root.winfo_screenheight() - ,





,





x = threading.Thread(target=mouse_trac, args=(screen_width, screen_height), daemon=True)
x.start()
      
      



target=mouse_trac - ,



args=(screen_width, screen_height) -



daemon=True -





Threading









label = Label(root, text='Enter flesh drive with code!', font='Courier 15', fg='#C71585')
label.place(relx=.5, rely=.94, anchor="center")

label3 = Label(root, text='USBCODE waiting...', font='Courier 20', fg='#00BFFF')
label3.place(relx=.5, rely=.40, anchor="center")

label4 = Label(root, text='Powered by .... wait....by who?', font='Courier 15', fg='#C71585')
label4.place(relx=.1, rely=.95, anchor="center")

root.update()
      
      



label = Label(root, text='Enter flesh drive with code!', font='Courier 15', fg='#C71585') label.place(relx=.5, rely=.94, anchor="center") -



root -



text - :G



font -



fg - HTML ( https://colorscheme.ru/html-colors.html )



relx - X



rely - Y



anchor="center" -





root.update() - ( CTRL+S)





Tkinter





.

.





, . , " " , windows. windows " ". , , . ( , , ).





  • " "





  • "" ( )





  • " "





  • , ""





  • ""





  • : " " " (, 1 , )"





  • ""





  • " "





  • ! ? . . .





. input_block.pyw





from ctypes import *
import time
import sys
from lofu import *

while True:
	statusl = status_lock()
	if str(statusl) == '0':
		windll.user32.BlockInput(False)
	elif str(statusl) == '1':
		windll.user32.BlockInput(True)
      
      



lofu





def status_lock():
	filee = open(r'  ', 'r')
	return filee.read()
	filee.close()

def disable_lock():
	filee = open(r'  ', 'w')
	text = 0
	filee.write(str(text))
	filee.close()

def enable_lock():
	filee = open(r'  ', 'w')
	text = 1
	filee.write(str(text))
	filee.close()
      
      



, : " , ? JSON ! !"





json, sqlite3 . . . !





input_block.pyw >> input_block.exe

pyinstaller





pyinstall input_block.pyw --onefile
      
      



? pyinstaller?





pip install pyinstaller
      
      



" ".





" " " " .





locker.pyw





os.system('schtasks.exe /run /tn "     "')
      
      



, ,





while True:
	try:
		enable_lock()
		file = open(r'F:\key.txt','r')
		file_t = str(file.read())
		file.close()
		hash_object = hashlib.sha256(file_t.encode())
		hex_dig = hash_object.hexdigest()
		if ' -' == str(hex_dig):
			label3.configure(text='USBCODE correct. Unlocking...', fg='#00FF00', font="Courier 30")
			root.update()
			time.sleep(4)
			disable_lock()
			sys.exit()
		else:
			label3.configure(text='USBCODE incorrect. Try again!', fg='#FF0000', font="Courier 30")
			enable_lock()
			root.update()
	except SystemExit:
		sys.exit()
	except:
		time.sleep(10)
		label3.configure(text='USBCODE not found! Waiting...', fg='#FF1493', font="Courier 20")
		enable_lock()
		root.update()
      
      



, . ,





enable_lock() - lofu





disable_lock() - lofu -





time.sleep(N) -





file = open(r'F:\key.txt','r')

file_t = str(file.read())

file.close() - ,





hash_object = hashlib.sha256(file_t.encode())

hex_dig = hash_object.hexdigest() -





if ' ' == str(hex_dig): - :d

:

* - ( , , , )

* ( )

* ( - locker.pyw)





, locker.pyw.





, shell:startup, " ".





That's all. My congratulations! We have just made a locker for Windows in Python 3.xc with complete blocking of input through the "Task Scheduler" , with unlocking through a USB flash drive, with a hash key and a graphical interface!



PS You can also put in the locker.pyw shortcut in the " Shortcut " field to bind keys for quick locking.








All Articles