Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

agent add access control when remote cmd #81

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 35 additions & 2 deletions agent/agent.go
Expand Up @@ -8,6 +8,7 @@ package agent

import (
"bufio"
"bytes"
"encoding/binary"
"fmt"
"io"
Expand Down Expand Up @@ -54,6 +55,9 @@ type Options struct {
// can call Close before shutting down.
// Optional.
ShutdownCleanup bool

// Key is remote access credentials. Maxlength 64 characters
Key string
}

// Listen starts the gops agent on a host process. Once agent started, users
Expand Down Expand Up @@ -107,12 +111,18 @@ func Listen(opts Options) error {
return err
}

go listen()
key := opts.Key
if len(key) > 64 {
return fmt.Errorf("Key maxlength is 64 characters")
}

go listen(key)
return nil
}

func listen() {
func listen(key string) {
buf := make([]byte, 1)
keyBuf := make([]byte, 64)
for {
fd, err := listener.Accept()
if err != nil {
Expand All @@ -122,10 +132,28 @@ func listen() {
}
continue
}
if key != "" {
if _, err := fd.Read(keyBuf); err != nil {
fmt.Fprintf(os.Stderr, "gops: %v", err)
continue
}
if !verify(keyBuf, key) {
fmt.Fprintf(os.Stderr, "gops: access denied. client: %s\n", fd.RemoteAddr())
fd.Write([]byte{0}) // login failed
fd.Write([]byte("access denied. Please set right GOPS_KEY\n"))
fd.Close()
continue

} else {
fd.Write([]byte{1}) // login success
}
}

if _, err := fd.Read(buf); err != nil {
fmt.Fprintf(os.Stderr, "gops: %v", err)
continue
}

if err := handle(fd, buf); err != nil {
fmt.Fprintf(os.Stderr, "gops: %v", err)
continue
Expand Down Expand Up @@ -175,6 +203,11 @@ func formatBytes(val uint64) string {
return fmt.Sprintf("%d bytes", val)
}

func verify(input []byte, key string) bool {
input = bytes.TrimRight(input, string([]byte{0}))
return bytes.Compare(input, []byte(key)) == 0
}

func handle(conn io.ReadWriter, msg []byte) error {
switch msg[0] {
case signal.StackTrace:
Expand Down
25 changes: 25 additions & 0 deletions agent/agent_test.go
Expand Up @@ -6,6 +6,7 @@ package agent

import (
"os"
"strings"
"testing"
)

Expand Down Expand Up @@ -76,3 +77,27 @@ func TestFormatBytes(t *testing.T) {
}
}
}

func TestVerify(t *testing.T) {
inputFunc := func(text string) []byte {
input := make([]byte, 64)
copy(input, []byte(text))
return input
}

tests := []struct {
input []byte
key string
want bool
}{
{inputFunc("abc"), "abc", true},
{inputFunc("ab"), "abc", false},
{[]byte{0x01}, "abc", false},
{inputFunc(strings.Repeat("1", 64)), strings.Repeat("1", 64), true},
}
for _, tt := range tests {
if got := verify(tt.input, tt.key); got != tt.want {
t.Errorf("verify(%v, %v) = %v; want %v", tt.input, tt.key, got, tt.want)
}
}
}
16 changes: 16 additions & 0 deletions cmd.go
Expand Up @@ -206,6 +206,22 @@ func cmdLazy(addr net.TCPAddr, c byte, params ...byte) (io.Reader, error) {
if err != nil {
return nil, err
}
key := os.Getenv("GOPS_KEY")
if key != "" {
keyBuf := make([]byte, 64)
restBuf := make([]byte, 1)
copy(keyBuf, []byte(key))
if _, err := conn.Write(keyBuf); err != nil {
return nil, err
}
if _, err := conn.Read(restBuf); err != nil {
return nil, err
}
if restBuf[0] == 0 { // login failed.
return conn, nil
}
}

buf := []byte{c}
buf = append(buf, params...)
if _, err := conn.Write(buf); err != nil {
Expand Down
Binary file added gops
Binary file not shown.