Skip to content

Commit

Permalink
Misc
Browse files Browse the repository at this point in the history
  • Loading branch information
hupe1980 committed Aug 18, 2021
1 parent 45ca1c2 commit bd74c84
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 11 deletions.
14 changes: 12 additions & 2 deletions elf.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ func (e *ELF) NX() bool {
return false
}

// PIE checks whether the current binary is position-independent
func (e *ELF) PIE() bool {
return e.file.Type == elf.ET_DYN
}

func (e *ELF) Checksec() string {
nx := map[bool]string{
true: color.GreenString("NX enabled"),
Expand All @@ -61,12 +66,17 @@ func (e *ELF) Checksec() string {
true: color.GreenString("Canary found"),
false: color.RedString("No canary found"),
}
pie := map[bool]string{
true: color.GreenString("PIE enabled"),
false: color.RedString("No PIE"),
}

var builder strings.Builder
writer := tabwriter.NewWriter(&builder, 0, 0, 3, ' ', 0)

fmt.Fprintf(writer, "NX\t%s\n", nx[e.NX()])
fmt.Fprintf(writer, "Stack\t%s\n", stack[e.Canary()])
fmt.Fprintf(writer, "NX:\t%s\n", nx[e.NX()])
fmt.Fprintf(writer, "Stack:\t%s\n", stack[e.Canary()])
fmt.Fprintf(writer, "PIE:\t%s\n", pie[e.PIE()])

writer.Flush()

Expand Down
43 changes: 34 additions & 9 deletions tubes/process.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tubes

import (
"io"
"os"
"os/exec"
)

Expand All @@ -10,21 +12,22 @@ type Process struct {
}

type ProcessOptions struct {
Path string
Args []string
Env []string
Dir string
NewLine byte
}

func NewProcess(argv []string, optFns ...func(o *ProcessOptions)) (*Process, error) {
options := ProcessOptions{
Path: argv[0],
Args: argv[1:],
NewLine: '\n',
}
for _, fn := range optFns {
fn(&options)
}
cmd := exec.Command(options.Path, options.Args...)

cmd := exec.Command(argv[0], argv[1:]...)
cmd.Env = append(os.Environ(), options.Env...)
cmd.Dir = options.Dir

stdin, err := cmd.StdinPipe()
if err != nil {
Expand All @@ -42,15 +45,37 @@ func NewProcess(argv []string, optFns ...func(o *ProcessOptions)) (*Process, err
return &Process{
cmd: cmd,
tube: tube{
stdin: stdin,
stdout: stdout,
stderr: stderr,

stdin: stdin,
stdout: stdout,
stderr: stderr,
newLine: options.NewLine,
},
}, nil
}

// Start starts the specified command but does not wait for it to complete.
func (p *Process) Start() error {
return p.cmd.Start()
}

func (p Process) Interactive() error {
go io.Copy(p.tube.stdin, os.Stdin)
go io.Copy(os.Stdout, p.tube.stdout)
go io.Copy(os.Stderr, p.tube.stderr)

// Wait for the process to exit
return p.cmd.Wait()
}

// Kill causes the Process to exit immediately. Kill does not wait until
// the Process has actually exited. This only kills the Process itself,
// not any other processes it may have started.
func (p *Process) Kill() error {
return p.cmd.Process.Kill()
}

// Signal sends a signal to the Process.
// Sending Interrupt on Windows is not implemented.
func (p *Process) Signal(sig os.Signal) error {
return p.cmd.Process.Signal(sig)
}
53 changes: 53 additions & 0 deletions tubes/process_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package tubes

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestProcess(t *testing.T) {
t.Run("echo", func(t *testing.T) {
p, err := NewProcess([]string{"echo", "helloworld"})
assert.NoError(t, err)

err = p.Start()
assert.NoError(t, err)

out, err := p.RecvLine()
assert.NoError(t, err)
assert.Equal(t, []byte("helloworld\n"), out)
})

t.Run("sh", func(t *testing.T) {
p, err := NewProcess([]string{"sh"})
assert.NoError(t, err)

err = p.Start()
assert.NoError(t, err)

_, err = p.SendLine("echo helloworld")
assert.NoError(t, err)

out, err := p.RecvLine()
assert.NoError(t, err)
assert.Equal(t, []byte("helloworld\n"), out)
})

t.Run("env", func(t *testing.T) {
p, err := NewProcess([]string{"sh"}, func(o *ProcessOptions) {
o.Env = []string{"HELLO_WORLD=helloworld"}
})
assert.NoError(t, err)

err = p.Start()
assert.NoError(t, err)

_, err = p.SendLine("echo $HELLO_WORLD")
assert.NoError(t, err)

out, err := p.RecvLine()
assert.NoError(t, err)
assert.Equal(t, []byte("helloworld\n"), out)
})
}

0 comments on commit bd74c84

Please sign in to comment.