-
Notifications
You must be signed in to change notification settings - Fork 8
Writing new commands
Note: The command API is still under heavy development and may break on version update
A command will need to implement the os.Command
interface:
type Command interface {
GetHelp() string
Exec(args []string, sys Sys) int
Where() string
}
The GetHelp()
and Where()
are for returning the values corresponding to UNIX command man
and which
.
Exec()
is executed when the command was typed in the terminal by the user or being passed into ssh exec request i.e. ssh xxx@honeypot cmd
.
Let's focus on the Exec()
function.
When being triggered, the "shell" will pass in 2 arguments: args
is equivalent to the args
variable used in ordinary golang program, is an array of string that contains the command arguments passed in. sys
is the "all-in-one" interface that allow the program to access the honeypot virtual system variables, filesystem and etc.
io.Reader
for the virtual STDIN
io.Writer
for the virtual STDOUT
Filesystem object based on afero library. See godoc for complete reference.
Allocated PTY(Pseudo TTY) width and height.
Get current directory
Change directory
Get io.ReadWriter
for accessing STDIN/OUT
Get a list of environment variables in name=value
format
Set environment variable
A command has to be registered with the Syrup table before being able to executed in shell. Every command program should have the following line in the init() function:
func init() {
os.RegisterCommand("text_for_invoking_cmd", commandStruct{})
}
package command
import (
"fmt"
"github.com/mkishere/sshsyrup/os"
)
type pwd struct{}
func init() {
os.RegisterCommand("pwd", pwd{})
}
func (p pwd) GetHelp() string {
return ""
}
func (p pwd) Exec(args []string, sys os.Sys) int {
fmt.Fprintln(sys.Out(), sys.Getcwd())
return 0
}
func (p pwd) Where() string {
return "/bin/pwd"
}