Skip to content

Writing new commands

mkishere edited this page Feb 1, 2018 · 4 revisions

Note: The command API is still under heavy development and may break on version update

Command interface

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.

Exec()

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.

sys.In()

io.Reader for the virtual STDIN

sys.Out()

io.Writer for the virtual STDOUT

sys.Fsys()

Filesystem object based on afero library. See godoc for complete reference.

sys.Width(), sys.Height()

Allocated PTY(Pseudo TTY) width and height.

sys.Getcwd()

Get current directory

sys.Chdir(string)

Change directory

sys.IOStream()

Get io.ReadWriter for accessing STDIN/OUT

sys.Environ()

Get a list of environment variables in name=value format

sys.SetEnv(key, value string)

Set environment variable

Register the command

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{})
}

Example

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"
}