Skip to content

Commit 240e775

Browse files
committed
feature: pageant debug log
1 parent f9aad94 commit 240e775

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

app/pageant.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@ import (
44
"context"
55
"github.com/buptczq/WinCryptSSHAgent/utils"
66
"io"
7+
"os"
78
"sync"
89
)
910

1011
type Pageant struct{}
1112

1213
func (*Pageant) Run(ctx context.Context, handler func(conn io.ReadWriteCloser)) error {
13-
win, err := utils.NewPageant()
14+
debug := false
15+
if os.Getenv("WCSA_DEBUG") == "1" {
16+
debug = true
17+
}
18+
win, err := utils.NewPageant(debug)
1419
if err != nil {
1520
return err
1621
}

utils/pageant.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,19 @@ type pageantWindow struct {
5151
class *wndClassEx
5252
window windows.Handle
5353
requestCh chan request
54+
debug bool
5455
}
5556

56-
func NewPageant() (*pageantWindow, error) {
57+
func NewPageant(debug bool) (*pageantWindow, error) {
5758

5859
classNamePtr, err := syscall.UTF16PtrFromString(className)
5960
if err != nil {
6061
return nil, err
6162
}
6263

63-
win := new(pageantWindow)
64+
win := &pageantWindow{
65+
debug: debug,
66+
}
6467

6568
wcex := &wndClassEx{
6669
WndProc: windows.NewCallback(win.wndProc),
@@ -163,10 +166,22 @@ func (s *pageantWindow) wndProc(hWnd windows.Handle, message uint32, wParam, lPa
163166
}
164167
copyData := (*copyDataStruct)(unsafe.Pointer(lParam))
165168
if copyData.dwData != agentCopydataId {
169+
if s.debug {
170+
println("Pageant: invalid copy data id", copyData.dwData)
171+
}
166172
return 0
167173
}
174+
if s.debug {
175+
h := [3]uintptr{copyData.lpData, uintptr(copyData.cbData), uintptr(copyData.cbData)}
176+
mapName := *(*[]byte)(unsafe.Pointer(&h))
177+
if mapName[len(mapName)-1] == 0 {
178+
mapName = mapName[:len(mapName)-1]
179+
}
180+
println("Pageant: OpenFileMapping", copyData.lpData, copyData.cbData, string(mapName))
181+
}
168182
fileMap, err := OpenFileMapping(fileMapAllAccess, 0, copyData.lpData)
169183
if err != nil {
184+
println("Pageant: OpenFileMapping error", err.Error())
170185
return
171186
}
172187
defer func() {
@@ -175,22 +190,30 @@ func (s *pageantWindow) wndProc(hWnd windows.Handle, message uint32, wParam, lPa
175190
// check security
176191
ourself, err := GetUserSID()
177192
if err != nil {
193+
println("Pageant: GetUserSID error", err.Error())
178194
return
179195
}
180196
ourself2, err := GetDefaultSID()
181197
if err != nil {
198+
println("Pageant: GetDefaultSID error", err.Error())
182199
return
183200
}
184201
mapOwner, err := GetHandleSID(fileMap)
185202
if err != nil {
203+
println("Pageant: GetHandleSID error", err.Error())
186204
return
187205
}
206+
if s.debug {
207+
println("Pageant: ourSID:", ourself.String(), "ourSID2:", ourself2.String(), "mapOwnerSID:", mapOwner.String())
208+
}
188209
if !windows.EqualSid(mapOwner, ourself) && !windows.EqualSid(mapOwner, ourself2) {
210+
println("Pageant: wrong owning SID of file mapping")
189211
return
190212
}
191213
// get map view
192214
sharedMemory, err := windows.MapViewOfFile(fileMap, fileMapWrite, 0, 0, 0)
193215
if err != nil {
216+
println("Pageant: MapViewOfFile error", err.Error())
194217
return
195218
}
196219
defer windows.UnmapViewOfFile(sharedMemory)
@@ -199,6 +222,7 @@ func (s *pageantWindow) wndProc(hWnd windows.Handle, message uint32, wParam, lPa
199222
size := binary.BigEndian.Uint32(sharedMemoryArray[:4])
200223
size += 4
201224
if size > agentMaxMsglen {
225+
println("Pageant: invalid message length", size)
202226
return
203227
}
204228

0 commit comments

Comments
 (0)