-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
329 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
package app | ||
|
||
import ( | ||
"context" | ||
"encoding/binary" | ||
"fmt" | ||
"github.com/buptczq/WinCryptSSHAgent/utils" | ||
"golang.org/x/crypto/ssh" | ||
"io" | ||
"net" | ||
"sync" | ||
) | ||
|
||
const ( | ||
maxAgentResponseBytes = 16 << 20 | ||
agentSignRequest = 13 | ||
) | ||
|
||
type XShell struct { | ||
cookie string | ||
} | ||
|
||
func (s *XShell) Run(ctx context.Context, handler func(conn io.ReadWriteCloser)) error { | ||
s.cookie = utils.RandomString(7) | ||
win, err := utils.NewXAgent(s.cookie) | ||
if err != nil { | ||
return err | ||
} | ||
defer win.Close() | ||
|
||
wg := new(sync.WaitGroup) | ||
l := win.Listener() | ||
for { | ||
conn, err := l.Accept() | ||
if err != nil { | ||
if err != io.ErrClosedPipe { | ||
return err | ||
} | ||
return nil | ||
} | ||
err = xshellHandshake(conn, s.cookie) | ||
if err != nil { | ||
println(err.Error()) | ||
conn.Close() | ||
continue | ||
} | ||
wg.Add(1) | ||
go func(c io.ReadWriteCloser) { | ||
w := &xshellProxy{c, nil} | ||
handler(w) | ||
wg.Done() | ||
}(conn) | ||
} | ||
} | ||
|
||
func (*XShell) AppId() AppId { | ||
return APP_XSHELL | ||
} | ||
|
||
func (s *XShell) Menu(register func(id AppId, name string, handler func())) { | ||
} | ||
|
||
type initAgentMsg struct { | ||
Flag uint32 `sshtype:"99"` | ||
Length uint32 | ||
Cookie []byte `ssh:"rest"` | ||
} | ||
|
||
type initAgentRepMsg struct { | ||
Flag uint32 `sshtype:"99"` | ||
} | ||
|
||
func xshellHandshake(conn net.Conn, cookie string) error { | ||
var length [4]byte | ||
if _, err := io.ReadFull(conn, length[:]); err != nil { | ||
return err | ||
} | ||
l := binary.BigEndian.Uint32(length[:]) + 4 | ||
if l > maxAgentResponseBytes { | ||
return fmt.Errorf("xagent: request too large: %d", l) | ||
} | ||
|
||
req := make([]byte, l) | ||
if _, err := io.ReadFull(conn, req); err != nil { | ||
return err | ||
} | ||
if req[0] != 99 { | ||
return fmt.Errorf("xagent: unknown opcode: %d", req[0]) | ||
} | ||
|
||
var initMsg initAgentMsg | ||
if err := ssh.Unmarshal(req, &initMsg); err != nil { | ||
return err | ||
} | ||
if int(initMsg.Length) != len(cookie) { | ||
return fmt.Errorf("xagent: invalid cookie length") | ||
} | ||
if int(initMsg.Length) < len(initMsg.Cookie) { | ||
return fmt.Errorf("xagent: invalid message length") | ||
} | ||
|
||
cookieRemain := make([]byte, int(initMsg.Length)-len(initMsg.Cookie)) | ||
if _, err := io.ReadFull(conn, cookieRemain); err != nil { | ||
return err | ||
} | ||
cookieReq := string(initMsg.Cookie) + string(cookieRemain) | ||
if cookieReq != cookie { | ||
return fmt.Errorf("xagent: invalid cookie") | ||
} | ||
var repMsg initAgentRepMsg | ||
repMsg.Flag = initMsg.Flag | ||
rep := ssh.Marshal(&repMsg) | ||
binary.BigEndian.PutUint32(length[:], uint32(len(rep))) | ||
if _, err := conn.Write(length[:]); err != nil { | ||
return err | ||
} | ||
if _, err := conn.Write(rep); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
type xshellProxy struct { | ||
conn io.ReadWriteCloser | ||
buf []byte | ||
} | ||
|
||
type signRequestAgentMsg struct { | ||
KeyBlob []byte `sshtype:"13"` | ||
Data []byte | ||
Flags uint32 | ||
} | ||
|
||
func (s *xshellProxy) Read(p []byte) (n int, err error) { | ||
if len(s.buf) > 0 { | ||
n := copy(p, s.buf) | ||
s.buf = s.buf[n:] | ||
return n, nil | ||
} | ||
var length [4]byte | ||
if _, err := io.ReadFull(s.conn, length[:]); err != nil { | ||
return 0, err | ||
} | ||
l := binary.BigEndian.Uint32(length[:]) | ||
if l == 0 { | ||
return 0, io.ErrUnexpectedEOF | ||
} | ||
if l > maxAgentResponseBytes { | ||
return 0, fmt.Errorf("xagent: request too large: %d", l) | ||
} | ||
|
||
s.buf = make([]byte, 4+l, 8+l) | ||
if _, err := io.ReadFull(s.conn, s.buf[4:]); err != nil { | ||
return 0, err | ||
} | ||
// sign | ||
if s.buf[4] == agentSignRequest { | ||
var req signRequestAgentMsg | ||
if err := ssh.Unmarshal(s.buf[4:], &req); err != nil { | ||
l += 4 | ||
s.buf = append(s.buf, []byte{0, 0, 0, 0}...) | ||
} | ||
} | ||
binary.BigEndian.PutUint32(s.buf, l) | ||
n = copy(p, s.buf) | ||
s.buf = s.buf[n:] | ||
return n, nil | ||
} | ||
|
||
func (s *xshellProxy) Write(p []byte) (n int, err error) { | ||
return s.conn.Write(p) | ||
} | ||
|
||
func (s *xshellProxy) Close() error { | ||
return s.conn.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package utils | ||
|
||
import ( | ||
"golang.org/x/sys/windows" | ||
"net" | ||
"syscall" | ||
"unsafe" | ||
) | ||
|
||
const ( | ||
xAgentClassName = "NSSSH:AGENTWND" | ||
xAgentSingleClassName = "STATIC" | ||
xAgentSingleWindowName = "_SINGLE_INSTANCE::XAGENT" | ||
) | ||
|
||
var ( | ||
pSetWindowLong = u32.NewProc("SetWindowLongW") | ||
) | ||
|
||
type XAgent struct { | ||
socket net.Listener | ||
cookieWin *window | ||
singleInstanceWin *window | ||
} | ||
|
||
type window struct { | ||
class *wndClassEx | ||
window windows.Handle | ||
} | ||
|
||
func createDefaultWindow(class, name string) (*window, error) { | ||
classNamePtr, err := syscall.UTF16PtrFromString(class) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
windowNamePtr, err := syscall.UTF16PtrFromString(name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
win := new(window) | ||
wcex := &wndClassEx{ | ||
WndProc: pDefWindowProc.Addr(), | ||
ClassName: classNamePtr, | ||
} | ||
err = wcex.register() | ||
if err != nil { | ||
return nil, err | ||
} | ||
win.class = wcex | ||
|
||
windowHandle, _, err := pCreateWindowEx.Call( | ||
uintptr(0), | ||
uintptr(unsafe.Pointer(classNamePtr)), | ||
uintptr(unsafe.Pointer(windowNamePtr)), | ||
uintptr(0), | ||
uintptr(0), | ||
uintptr(0), | ||
uintptr(0), | ||
uintptr(0), | ||
uintptr(0), | ||
uintptr(0), | ||
uintptr(0), | ||
uintptr(0), | ||
) | ||
if windowHandle == 0 { | ||
wcex.unregister() | ||
return nil, err | ||
} | ||
win.window = windows.Handle(windowHandle) | ||
return win, nil | ||
} | ||
|
||
func (s *window) Close() { | ||
pDestroyWindow.Call(uintptr(s.window)) | ||
s.class.unregister() | ||
} | ||
|
||
func NewXAgent(cookie string) (*XAgent, error) { | ||
l, err := net.Listen("tcp", "localhost:0") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
win := new(XAgent) | ||
win.socket = l | ||
cookieWin, err := createDefaultWindow(xAgentClassName, cookie) | ||
if err != nil { | ||
return nil, err | ||
} | ||
siWin, err := createDefaultWindow(xAgentSingleClassName, xAgentSingleWindowName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
SetWindowLong(cookieWin.window, 0xFFFFFFEB, uintptr(l.Addr().(*net.TCPAddr).Port)) | ||
win.cookieWin = cookieWin | ||
win.singleInstanceWin = siWin | ||
return win, nil | ||
} | ||
|
||
func (s *XAgent) Listener() net.Listener { | ||
return s.socket | ||
} | ||
|
||
func (s *XAgent) Close() { | ||
s.cookieWin.Close() | ||
s.singleInstanceWin.Close() | ||
} | ||
|
||
func SetWindowLong(hWnd windows.Handle, index, value uintptr) int32 { | ||
ret, _, _ := pSetWindowLong.Call( | ||
uintptr(hWnd), | ||
index, | ||
value, | ||
) | ||
return int32(ret) | ||
} |