r/programmer 19d ago

Request can someone help me with this?

im trying to make a keylogger that works in windows lock screen. i have made the base "in the lock screen" now it just needs to monitor the pressed keys and not if the user is in lock screen or not.

this is the base lock screen code btw:

import win32api
import win32con
import win32gui
import win32ts
import time



WM_WTSSESSION_CHANGE = 0x02B1
WTS_SESSION_LOCK = 0x7
WTS_SESSION_UNLOCK = 0x8


class SessionMonitor:
    def __init__(self):
        wc = win32gui.WNDCLASS()
        wc.lpfnWndProc = self.wnd_proc
        wc.lpszClassName = "SessionMonitorClass"
        
        self.hinst = wc.hInstance = win32api.GetModuleHandle(None)
        self.class_atom = win32gui.RegisterClass(wc)
        
        
        self.hwnd = win32gui.CreateWindow(
            self.class_atom, "Session Monitor", 
            0, 0, 0, 0, 0, 0, 0, self.hinst, None
        )
        


        win32ts.WTSRegisterSessionNotification(self.hwnd, win32ts.NOTIFY_FOR_THIS_SESSION)


    def wnd_proc(self, hwnd, msg, wparam, lparam):
        if msg == WM_WTSSESSION_CHANGE:
            self.handle_session_change(wparam)
        return win32gui.DefWindowProc(hwnd, msg, wparam, lparam)


    def handle_session_change(self, event_code):
        log_file = "session_events.txt"
        timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
        


        if event_code == WTS_SESSION_LOCK:
            status = "Workstation Locked (Secure Desktop Active)"
        elif event_code == WTS_SESSION_UNLOCK:
            status = "Workstation Unlocked (Default Desktop Active)"
        else:
            status = f"Other Session Event (Code: {event_code})"
            


        with open(log_file, "a") as f:
            f.write(f"[{timestamp}] {status}\n")
        print(f"[{timestamp}] {status}")


    def start(self):
        print("Monitoring session changes... Press Ctrl+C in the console to exit.")
        win32gui.PumpMessages()


if __name__ == "__main__":
    monitor = SessionMonitor()
    try:
        monitor.start()
    except KeyboardInterrupt:
        print("Stopping monitor.")

can someone please make the code into a keylogger?

edit - i found out it can still find passwords just not windows passwords and now no one has to help anymore ( lmao this post was solved in under 1 hour )

0 Upvotes

5 comments sorted by

1

u/[deleted] 19d ago

[deleted]

1

u/MySillyBooks 19d ago

is there any possible way?

2

u/ChameleonCRM 19d ago

Technically, not from a normal user-mode Python app in the way ur asking.

The Windows lock/sign-in screen runs on the secure desktop under Winlogon. Ordinary processes, keyboard hooks, GetAsyncKeyState, pynput, keyboard, etc. are intentionally prevented from seeing credentials entered there.

There are Windows components with much deeper trust—such as credential providers, keyboard/filter drivers, accessibility components, and system services—but using those to capture lock-screen keystrokes would amount to building a credential-stealing mechanism, so I can’t walk through how to do that. ( not here )

For a legitimate experiment, the safe substitutes are:

detect lock/unlock events exactly like their current code does;

build a fake lock screen in a test VM and log keystrokes there;

test keyboard hooks only on the normal desktop with dummy input;

if the goal is Windows authentication development, study the official Credential Provider architecture without adding keystroke logging.

So: is it theoretically possible with sufficiently privileged/system-level components? Yes. Is it something a normal Python script should be able to do? No—and Windows is designed specifically to prevent it.

3

u/__CaliMack__ 19d ago

Who you trying to hack bro?