Initial commit: LAN RDP with keylogger support
This commit is contained in:
commit
c4adcbbe0f
33 files changed
+4335
No files matched your search
Executable
+504
@@ -0,0 +1,504 @@
|
||||
// Rendering and input capture for the viewer window. This is plain Win32
|
||||
// (RegisterClassEx / CreateWindowEx / a GetMessage loop) reached via
|
||||
// syscalls through golang.org/x/sys/windows — no cgo, so the binary stays a
|
||||
// single portable executable.
|
||||
package main
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"sync/atomic"
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
|
||||
"lanrdp/internal/protocol"
|
||||
)
|
||||
|
||||
var (
|
||||
user32 = windows.NewLazySystemDLL("user32.dll")
|
||||
gdi32 = windows.NewLazySystemDLL("gdi32.dll")
|
||||
kernel32 = windows.NewLazySystemDLL("kernel32.dll")
|
||||
shell32 = windows.NewLazySystemDLL("shell32.dll")
|
||||
|
||||
procGetModuleHandleW = kernel32.NewProc("GetModuleHandleW")
|
||||
|
||||
procRegisterClassExW = user32.NewProc("RegisterClassExW")
|
||||
procUnregisterClass = user32.NewProc("UnregisterClassW")
|
||||
procCreateWindowExW = user32.NewProc("CreateWindowExW")
|
||||
procDefWindowProcW = user32.NewProc("DefWindowProcW")
|
||||
procShowWindow = user32.NewProc("ShowWindow")
|
||||
procUpdateWindow = user32.NewProc("UpdateWindow")
|
||||
procGetMessageW = user32.NewProc("GetMessageW")
|
||||
procTranslateMessage = user32.NewProc("TranslateMessage")
|
||||
procDispatchMessageW = user32.NewProc("DispatchMessageW")
|
||||
procPostQuitMessage = user32.NewProc("PostQuitMessage")
|
||||
procDestroyWindow = user32.NewProc("DestroyWindow")
|
||||
procInvalidateRect = user32.NewProc("InvalidateRect")
|
||||
procBeginPaint = user32.NewProc("BeginPaint")
|
||||
procEndPaint = user32.NewProc("EndPaint")
|
||||
procGetClientRect = user32.NewProc("GetClientRect")
|
||||
procLoadCursorW = user32.NewProc("LoadCursorW")
|
||||
procAdjustWindowRect = user32.NewProc("AdjustWindowRect")
|
||||
procGetSystemMetrics = user32.NewProc("GetSystemMetrics")
|
||||
procRegisterHotKey = user32.NewProc("RegisterHotKey")
|
||||
procUnregisterHotKey = user32.NewProc("UnregisterHotKey")
|
||||
procSetWindowTextW = user32.NewProc("SetWindowTextW")
|
||||
|
||||
procStretchDIBits = gdi32.NewProc("StretchDIBits")
|
||||
procCreateSolidBrush = gdi32.NewProc("CreateSolidBrush")
|
||||
procDeleteObject = gdi32.NewProc("DeleteObject")
|
||||
procFillRect = user32.NewProc("FillRect")
|
||||
|
||||
procDragAcceptFiles = shell32.NewProc("DragAcceptFiles")
|
||||
procDragQueryFileW = shell32.NewProc("DragQueryFileW")
|
||||
procDragFinish = shell32.NewProc("DragFinish")
|
||||
)
|
||||
|
||||
const (
|
||||
wmDestroy = 0x0002
|
||||
wmPaint = 0x000F
|
||||
wmClose = 0x0010
|
||||
wmEraseBkgnd = 0x0014
|
||||
wmKeyDown = 0x0100
|
||||
wmKeyUp = 0x0101
|
||||
wmMouseMove = 0x0200
|
||||
wmLButtonDown = 0x0201
|
||||
wmLButtonUp = 0x0202
|
||||
wmRButtonDown = 0x0204
|
||||
wmRButtonUp = 0x0205
|
||||
wmMButtonDown = 0x0207
|
||||
wmMButtonUp = 0x0208
|
||||
wmMouseWheel = 0x020A
|
||||
wmHotkey = 0x0312
|
||||
wmDropFiles = 0x0233
|
||||
|
||||
modAlt = 0x0001
|
||||
modControl = 0x0002
|
||||
vkS = 0x53
|
||||
|
||||
streamModeHotkeyID = 1
|
||||
|
||||
wsOverlappedWindow = 0x00CF0000
|
||||
wsVisible = 0x10000000
|
||||
cwUseDefault = 0x80000000
|
||||
|
||||
swShow = 5
|
||||
|
||||
idcArrow = 32512
|
||||
|
||||
srcCopyROP = 0x00CC0020
|
||||
biRGB = 0
|
||||
dibRGBColors = 0
|
||||
|
||||
smCXScreen = 0
|
||||
smCYScreen = 1
|
||||
)
|
||||
|
||||
type wndClassExW struct {
|
||||
cbSize uint32
|
||||
style uint32
|
||||
lpfnWndProc uintptr
|
||||
cbClsExtra int32
|
||||
cbWndExtra int32
|
||||
hInstance windows.Handle
|
||||
hIcon windows.Handle
|
||||
hCursor windows.Handle
|
||||
hbrBackground windows.Handle
|
||||
lpszMenuName *uint16
|
||||
lpszClassName *uint16
|
||||
hIconSm windows.Handle
|
||||
}
|
||||
|
||||
type rectT struct {
|
||||
Left, Top, Right, Bottom int32
|
||||
}
|
||||
|
||||
type pointT struct {
|
||||
X, Y int32
|
||||
}
|
||||
|
||||
type msgT struct {
|
||||
Hwnd uintptr
|
||||
Message uint32
|
||||
WParam uintptr
|
||||
LParam uintptr
|
||||
Time uint32
|
||||
Pt pointT
|
||||
}
|
||||
|
||||
type paintStruct struct {
|
||||
Hdc windows.Handle
|
||||
FErase int32
|
||||
RcPaint rectT
|
||||
FRestore int32
|
||||
FIncUpdate int32
|
||||
RgbReserved [32]byte
|
||||
}
|
||||
|
||||
type bitmapInfoHeader struct {
|
||||
Size uint32
|
||||
Width int32
|
||||
Height int32
|
||||
Planes uint16
|
||||
BitCount uint16
|
||||
Compression uint32
|
||||
SizeImage uint32
|
||||
XPelsPerMeter int32
|
||||
YPelsPerMeter int32
|
||||
ClrUsed uint32
|
||||
ClrImportant uint32
|
||||
}
|
||||
|
||||
var currentHwnd atomic.Uintptr
|
||||
|
||||
func invalidateWindow(a *appState) {
|
||||
h := currentHwnd.Load()
|
||||
if h == 0 {
|
||||
return
|
||||
}
|
||||
procInvalidateRect.Call(h, 0, 0)
|
||||
}
|
||||
|
||||
func getClientSize(hwnd uintptr) (int, int) {
|
||||
var r rectT
|
||||
procGetClientRect.Call(hwnd, uintptr(unsafe.Pointer(&r)))
|
||||
w := int(r.Right - r.Left)
|
||||
h := int(r.Bottom - r.Top)
|
||||
if w <= 0 {
|
||||
w = 1
|
||||
}
|
||||
if h <= 0 {
|
||||
h = 1
|
||||
}
|
||||
return w, h
|
||||
}
|
||||
|
||||
func onPaint(hwnd uintptr) {
|
||||
var ps paintStruct
|
||||
hdc, _, _ := procBeginPaint.Call(hwnd, uintptr(unsafe.Pointer(&ps)))
|
||||
defer procEndPaint.Call(hwnd, uintptr(unsafe.Pointer(&ps)))
|
||||
|
||||
clientW, clientH := getClientSize(hwnd)
|
||||
|
||||
app.fbMu.Lock()
|
||||
fbW, fbH := app.fbW, app.fbH
|
||||
fb := app.fb
|
||||
app.fbMu.Unlock()
|
||||
|
||||
if fbW <= 0 || fbH <= 0 || len(fb) < fbW*fbH*4 {
|
||||
return
|
||||
}
|
||||
|
||||
hdr := bitmapInfoHeader{
|
||||
Width: int32(fbW),
|
||||
Height: -int32(fbH),
|
||||
Planes: 1,
|
||||
BitCount: 32,
|
||||
Compression: biRGB,
|
||||
}
|
||||
hdr.Size = uint32(unsafe.Sizeof(hdr))
|
||||
|
||||
procStretchDIBits.Call(
|
||||
hdc,
|
||||
0, 0, uintptr(clientW), uintptr(clientH),
|
||||
0, 0, uintptr(fbW), uintptr(fbH),
|
||||
uintptr(unsafe.Pointer(&fb[0])),
|
||||
uintptr(unsafe.Pointer(&hdr)),
|
||||
uintptr(dibRGBColors),
|
||||
uintptr(srcCopyROP),
|
||||
)
|
||||
|
||||
drawModeIndicator(hdc, clientW, clientH)
|
||||
}
|
||||
|
||||
// streamModeBorderColor is a COLORREF (0x00BBGGRR) for a vivid orange,
|
||||
// chosen to be unmistakable against arbitrary desktop content.
|
||||
const streamModeBorderColor = 0x0000A5FF
|
||||
|
||||
const streamModeBorderPx = 6
|
||||
|
||||
// drawModeIndicator paints a thick colored border around the video when
|
||||
// stream mode is active, so the current mode is visible at a glance
|
||||
// without relying on the window title (easy to miss, especially
|
||||
// maximized). Nothing is drawn in normal mode.
|
||||
func drawModeIndicator(hdc uintptr, clientW, clientH int) {
|
||||
if !app.streamMode {
|
||||
return
|
||||
}
|
||||
brush, _, _ := procCreateSolidBrush.Call(uintptr(streamModeBorderColor))
|
||||
if brush == 0 {
|
||||
return
|
||||
}
|
||||
defer procDeleteObject.Call(brush)
|
||||
|
||||
edges := []rectT{
|
||||
{0, 0, int32(clientW), streamModeBorderPx}, // top
|
||||
{0, int32(clientH - streamModeBorderPx), int32(clientW), int32(clientH)}, // bottom
|
||||
{0, 0, streamModeBorderPx, int32(clientH)}, // left
|
||||
{int32(clientW - streamModeBorderPx), 0, int32(clientW), int32(clientH)}, // right
|
||||
}
|
||||
for _, r := range edges {
|
||||
procFillRect.Call(hdc, uintptr(unsafe.Pointer(&r)), brush)
|
||||
}
|
||||
}
|
||||
|
||||
func mouseCoords(hwnd uintptr, lparam uintptr) (normX, normY uint16) {
|
||||
x := int(int16(uint16(lparam & 0xFFFF)))
|
||||
y := int(int16(uint16((lparam >> 16) & 0xFFFF)))
|
||||
clientW, clientH := getClientSize(hwnd)
|
||||
if x < 0 {
|
||||
x = 0
|
||||
}
|
||||
if y < 0 {
|
||||
y = 0
|
||||
}
|
||||
if x >= clientW {
|
||||
x = clientW - 1
|
||||
}
|
||||
if y >= clientH {
|
||||
y = clientH - 1
|
||||
}
|
||||
nx := x * 65535 / max(clientW-1, 1)
|
||||
ny := y * 65535 / max(clientH-1, 1)
|
||||
return uint16(nx), uint16(ny)
|
||||
}
|
||||
|
||||
// onMouseEvent forwards a mouse event to the host, unless stream mode is
|
||||
// active — stream mode is meant for watching the host's own activity
|
||||
// without interfering with it, so mouse control is suppressed while it's
|
||||
// on (screen updates keep flowing either way).
|
||||
func onMouseEvent(hwnd uintptr, action protocol.MouseAction, lparam uintptr) {
|
||||
if app.streamMode {
|
||||
return
|
||||
}
|
||||
nx, ny := mouseCoords(hwnd, lparam)
|
||||
_ = app.send(protocol.MsgInputMouse, protocol.EncodeInputMouse(protocol.InputMouse{
|
||||
Action: action, NormX: nx, NormY: ny,
|
||||
}))
|
||||
}
|
||||
|
||||
func onWheel(wparam uintptr) {
|
||||
if app.streamMode {
|
||||
return
|
||||
}
|
||||
delta := int16(int32(wparam) >> 16)
|
||||
_ = app.send(protocol.MsgInputMouse, protocol.EncodeInputMouse(protocol.InputMouse{
|
||||
Action: protocol.MouseWheel, WheelDelta: delta,
|
||||
}))
|
||||
}
|
||||
|
||||
const maxDropPath = 32768 // long-path-safe buffer size for DragQueryFileW
|
||||
|
||||
// handleDropFiles is called on WM_DROPFILES. File transfer only makes
|
||||
// sense in normal (RDP) mode — stream mode is meant to be view-only — so
|
||||
// drops are silently ignored while stream mode is on. Multiple dropped
|
||||
// files are sent one at a time in a single background goroutine rather
|
||||
// than concurrently, since interleaving two transfers' chunks on the same
|
||||
// connection would corrupt both on the host's receiving end.
|
||||
func handleDropFiles(hwnd uintptr, hDrop uintptr) {
|
||||
defer procDragFinish.Call(hDrop)
|
||||
|
||||
if app.streamMode {
|
||||
return
|
||||
}
|
||||
|
||||
count, _, _ := procDragQueryFileW.Call(hDrop, 0xFFFFFFFF, 0, 0)
|
||||
if count == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
paths := make([]string, 0, count)
|
||||
for i := uintptr(0); i < count; i++ {
|
||||
buf := make([]uint16, maxDropPath)
|
||||
n, _, _ := procDragQueryFileW.Call(hDrop, i, uintptr(unsafe.Pointer(&buf[0])), uintptr(len(buf)))
|
||||
if n == 0 {
|
||||
continue
|
||||
}
|
||||
paths = append(paths, windows.UTF16ToString(buf[:n]))
|
||||
}
|
||||
|
||||
go func() {
|
||||
for _, p := range paths {
|
||||
sendFile(p)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// toggleStreamMode flips between the host's normal and low-latency
|
||||
// capture presets (Ctrl+Alt+S) and reflects the current mode in the
|
||||
// window title, since there's no other visible indicator of it.
|
||||
func toggleStreamMode(hwnd uintptr) {
|
||||
app.streamMode = !app.streamMode
|
||||
mode := protocol.PerfNormal
|
||||
title := "lanrdp viewer"
|
||||
if app.streamMode {
|
||||
mode = protocol.PerfStream
|
||||
title = "lanrdp viewer [STREAM MODE]"
|
||||
}
|
||||
_ = app.send(protocol.MsgSetPerfMode, protocol.EncodeSetPerfMode(mode))
|
||||
|
||||
titlePtr, err := windows.UTF16PtrFromString(title)
|
||||
if err == nil {
|
||||
procSetWindowTextW.Call(hwnd, uintptr(unsafe.Pointer(titlePtr)))
|
||||
}
|
||||
|
||||
// Repaint immediately so the border indicator appears/disappears right
|
||||
// away instead of waiting for the next frame update from the host.
|
||||
procInvalidateRect.Call(hwnd, 0, 0)
|
||||
}
|
||||
|
||||
func wndProc(hwnd, msg, wparam, lparam uintptr) uintptr {
|
||||
switch msg {
|
||||
case wmPaint:
|
||||
onPaint(hwnd)
|
||||
return 0
|
||||
case wmEraseBkgnd:
|
||||
return 1
|
||||
case wmClose:
|
||||
_ = app.send(protocol.MsgDisconnect, nil)
|
||||
procDestroyWindow.Call(hwnd)
|
||||
return 0
|
||||
case wmDestroy:
|
||||
procPostQuitMessage.Call(0)
|
||||
return 0
|
||||
case wmMouseMove:
|
||||
onMouseEvent(hwnd, protocol.MouseMove, lparam)
|
||||
return 0
|
||||
case wmLButtonDown:
|
||||
onMouseEvent(hwnd, protocol.MouseLeftDown, lparam)
|
||||
return 0
|
||||
case wmLButtonUp:
|
||||
onMouseEvent(hwnd, protocol.MouseLeftUp, lparam)
|
||||
return 0
|
||||
case wmRButtonDown:
|
||||
onMouseEvent(hwnd, protocol.MouseRightDown, lparam)
|
||||
return 0
|
||||
case wmRButtonUp:
|
||||
onMouseEvent(hwnd, protocol.MouseRightUp, lparam)
|
||||
return 0
|
||||
case wmMButtonDown:
|
||||
onMouseEvent(hwnd, protocol.MouseMiddleDown, lparam)
|
||||
return 0
|
||||
case wmMButtonUp:
|
||||
onMouseEvent(hwnd, protocol.MouseMiddleUp, lparam)
|
||||
return 0
|
||||
case wmMouseWheel:
|
||||
onWheel(wparam)
|
||||
return 0
|
||||
case wmHotkey:
|
||||
if wparam == streamModeHotkeyID {
|
||||
toggleStreamMode(hwnd)
|
||||
}
|
||||
return 0
|
||||
case wmKeyDown:
|
||||
if !app.streamMode {
|
||||
_ = app.send(protocol.MsgInputKey, protocol.EncodeInputKey(protocol.InputKey{VKCode: uint16(wparam), Down: true}))
|
||||
}
|
||||
return 0
|
||||
case wmKeyUp:
|
||||
if !app.streamMode {
|
||||
_ = app.send(protocol.MsgInputKey, protocol.EncodeInputKey(protocol.InputKey{VKCode: uint16(wparam), Down: false}))
|
||||
}
|
||||
return 0
|
||||
case wmDropFiles:
|
||||
handleDropFiles(hwnd, wparam)
|
||||
return 0
|
||||
}
|
||||
ret, _, _ := procDefWindowProcW.Call(hwnd, msg, wparam, lparam)
|
||||
return ret
|
||||
}
|
||||
|
||||
func runWindow(a *appState, remoteW, remoteH int) {
|
||||
runtime.LockOSThread()
|
||||
|
||||
className, _ := windows.UTF16PtrFromString("LanRDPViewerWindow")
|
||||
title, _ := windows.UTF16PtrFromString("lanrdp viewer")
|
||||
|
||||
hInstance, _, _ := procGetModuleHandleW.Call(0)
|
||||
|
||||
cursor, _, _ := procLoadCursorW.Call(0, uintptr(idcArrow))
|
||||
|
||||
wc := wndClassExW{
|
||||
style: 0,
|
||||
lpfnWndProc: windows.NewCallback(wndProc),
|
||||
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 {
|
||||
panic("RegisterClassExW failed")
|
||||
}
|
||||
|
||||
clientW, clientH := remoteW, remoteH
|
||||
if clientW <= 0 {
|
||||
clientW = 1024
|
||||
}
|
||||
if clientH <= 0 {
|
||||
clientH = 768
|
||||
}
|
||||
|
||||
// Clamp the initial client area to the local screen (minus a margin for
|
||||
// the title bar/taskbar) so a host with a larger resolution than this
|
||||
// machine doesn't open a window bigger than the display. This only
|
||||
// changes the window's on-screen size, not the mapping: mouse input is
|
||||
// always normalized against the window's actual (live) client rect, so
|
||||
// the picture stays correctly aligned regardless of any scaling here.
|
||||
screenW, _, _ := procGetSystemMetrics.Call(uintptr(smCXScreen))
|
||||
screenH, _, _ := procGetSystemMetrics.Call(uintptr(smCYScreen))
|
||||
maxW, maxH := int(screenW)-100, int(screenH)-100
|
||||
if maxW > 0 && maxH > 0 && (clientW > maxW || clientH > maxH) {
|
||||
scale := min(float64(maxW)/float64(clientW), float64(maxH)/float64(clientH))
|
||||
clientW = int(float64(clientW) * scale)
|
||||
clientH = int(float64(clientH) * scale)
|
||||
}
|
||||
|
||||
// AdjustWindowRect turns our desired *client area* size into the
|
||||
// outer window size CreateWindowExW expects (which includes the
|
||||
// title bar and borders), so the window's actual client rect ends up
|
||||
// exactly clientW x clientH instead of being a few pixels smaller.
|
||||
wr := rectT{0, 0, int32(clientW), int32(clientH)}
|
||||
procAdjustWindowRect.Call(uintptr(unsafe.Pointer(&wr)), uintptr(wsOverlappedWindow), 0)
|
||||
outerW := int(wr.Right - wr.Left)
|
||||
outerH := int(wr.Bottom - wr.Top)
|
||||
|
||||
hwnd, _, _ := procCreateWindowExW.Call(
|
||||
0,
|
||||
uintptr(unsafe.Pointer(className)),
|
||||
uintptr(unsafe.Pointer(title)),
|
||||
uintptr(wsOverlappedWindow|wsVisible),
|
||||
uintptr(cwUseDefault), uintptr(cwUseDefault),
|
||||
uintptr(outerW), uintptr(outerH),
|
||||
0, 0,
|
||||
hInstance,
|
||||
0,
|
||||
)
|
||||
if hwnd == 0 {
|
||||
panic("CreateWindowExW failed")
|
||||
}
|
||||
currentHwnd.Store(hwnd)
|
||||
|
||||
procDragAcceptFiles.Call(hwnd, 1) // enables WM_DROPFILES for drag-and-drop file transfer
|
||||
|
||||
if ret, _, _ := procRegisterHotKey.Call(hwnd, uintptr(streamModeHotkeyID), uintptr(modControl|modAlt), uintptr(vkS)); ret == 0 {
|
||||
// Non-fatal: some other app may already own Ctrl+Alt+S. The viewer
|
||||
// still works, just without the toggle.
|
||||
}
|
||||
defer procUnregisterHotKey.Call(hwnd, uintptr(streamModeHotkeyID))
|
||||
|
||||
procShowWindow.Call(hwnd, uintptr(swShow))
|
||||
procUpdateWindow.Call(hwnd)
|
||||
|
||||
var m msgT
|
||||
for {
|
||||
ret, _, _ := procGetMessageW.Call(uintptr(unsafe.Pointer(&m)), 0, 0, 0)
|
||||
if int32(ret) <= 0 {
|
||||
break
|
||||
}
|
||||
procTranslateMessage.Call(uintptr(unsafe.Pointer(&m)))
|
||||
procDispatchMessageW.Call(uintptr(unsafe.Pointer(&m)))
|
||||
}
|
||||
|
||||
_ = a.conn.Close()
|
||||
}
|
||||
Reference in new issue
Block a user