Skip to content

Commit

Permalink
Misc
Browse files Browse the repository at this point in the history
  • Loading branch information
hupe1980 committed Aug 21, 2021
1 parent 7437d35 commit 992c3ca
Show file tree
Hide file tree
Showing 15 changed files with 101 additions and 56 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# gopwn
# gopwn ![Build Status](https://github.com/hupe1980/gopwn/workflows/build/badge.svg)
> Golang CTF framework and exploit development module
This module is strictly for educational purposes only. Usage of the methods and tools for attacking targets without prior mutual consent is illegal. It is the end user's responsibility to obey all applicable laws. Developers assume no liability and are not responsible for any misuse or damage caused by this module.
Expand All @@ -10,8 +10,8 @@ This module is strictly for educational purposes only. Usage of the methods and
package main

import (
"bytes"
"fmt"
"bytes"
"fmt"

"github.com/hupe1980/gopwn"
)
Expand Down
8 changes: 4 additions & 4 deletions asm.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ func Assemble(assembly string, arch Arch) ([]byte, error) {
return ks.Assemble(assembly)
}

func Assemble_X86_64(assembly string) ([]byte, error) {
func AssembleX86_64(assembly string) ([]byte, error) {
return Assemble(assembly, ARCH_X86_64)
}

func Assemble_I386(assembly string) ([]byte, error) {
func AssembleI386(assembly string) ([]byte, error) {
return Assemble(assembly, ARCH_I386)
}

Expand Down Expand Up @@ -126,10 +126,10 @@ func Disam(data []byte, vma uint64, arch Arch) (string, error) {
return engine.Disam(data, vma)
}

func Disam_X86_64(data []byte, vma uint64) (string, error) {
func DisamX86_64(data []byte, vma uint64) (string, error) {
return Disam(data, vma, ARCH_X86_64)
}

func Disam_I386(data []byte, vma uint64) (string, error) {
func DisamI386(data []byte, vma uint64) (string, error) {
return Disam(data, vma, ARCH_I386)
}
8 changes: 4 additions & 4 deletions asm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,27 @@ import (

func TestASM(t *testing.T) {
t.Run("x86_64", func(t *testing.T) {
insn, err := Assemble_X86_64("mov rax, 0")
insn, err := AssembleX86_64("mov rax, 0")
assert.NoError(t, err)
assert.Equal(t, []byte("\x48\xc7\xc0\x00\x00\x00\x00"), insn)
})

t.Run("i386", func(t *testing.T) {
insn, err := Assemble_I386("mov eax, 0")
insn, err := AssembleI386("mov eax, 0")
assert.NoError(t, err)
assert.Equal(t, []byte("\xb8\x00\x00\x00\x00"), insn)
})
}

func TestDISASM(t *testing.T) {
t.Run("x86_64", func(t *testing.T) {
assembly, err := Disam_X86_64([]byte("\x48\xc7\xc0\x17\x00\x00\x00"), 0)
assembly, err := DisamX86_64([]byte("\x48\xc7\xc0\x17\x00\x00\x00"), 0)
assert.NoError(t, err)
assert.Equal(t, "0x0:\tmov\t\trax, 0x17\n", assembly)
})

t.Run("i386", func(t *testing.T) {
assembly, err := Disam_I386([]byte("\xb8\x5d\x00\x00\x00"), 0)
assembly, err := DisamI386([]byte("\xb8\x5d\x00\x00\x00"), 0)
assert.NoError(t, err)
assert.Equal(t, "0x0:\tmov\t\teax, 0x5d\n", assembly)
})
Expand Down
25 changes: 8 additions & 17 deletions gopwn.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package gopwn

import (
"github.com/hupe1980/gopwn/tubes"
)
import "github.com/hupe1980/gopwn/tube"

type Arch int

Expand All @@ -18,7 +16,7 @@ func (a Arch) String() string {
0: "x86_64",
1: "i386",
2: "arm",
3: "arm_64",
3: "aarch64",
}
return archString[a]
}
Expand All @@ -38,21 +36,14 @@ func (a Endian) String() string {
return endianString[a]
}

func NewProcess(argv []string, optFns ...func(o *tubes.ProcessOptions)) (*tubes.Process, error) {
p, err := tubes.NewProcess(argv, optFns...)
if err != nil {
return nil, err
}
if err := p.Start(); err != nil {
return nil, err
}
return p, nil
func NewProcess(argv []string, optFns ...func(o *tube.ProcessOptions)) (*tube.Process, error) {
return tube.NewProcess(argv, optFns...)
}

func NewRemote(network, addr string) (*tubes.Remote, error) {
return tubes.NewRemote(network, addr)
func NewRemote(network, addr string) (*tube.Remote, error) {
return tube.NewRemote(network, addr)
}

func NewListener(addr string) (*tubes.Listener, error) {
return tubes.NewListener(addr)
func NewListener(addr string) (*tube.Listener, error) {
return tube.NewListener(addr)
}
23 changes: 22 additions & 1 deletion misc.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package gopwn

import (
"encoding/base64"
"encoding/hex"
"strings"
)

//Hex encodes the bytes hexadecimal.
// Hex encodes the bytes hexadecimal.
func Hex(src []byte) []byte {
dst := make([]byte, hex.EncodedLen(len(src)))
hex.Encode(dst, src)
Expand Down Expand Up @@ -34,3 +36,22 @@ func UnHexString(src string) ([]byte, error) {
}
return decoded, nil
}

func Base64E(b []byte) string {
return base64.StdEncoding.EncodeToString(b)
}

func Base64D(s string) ([]byte, error) {
return base64.StdEncoding.DecodeString(s)
}

func ROT13(s string) string {
return strings.Map(func(c rune) rune {
if c >= 'a' && c <= 'm' || c >= 'A' && c <= 'M' {
return c + 13
} else if c >= 'n' && c <= 'z' || c >= 'N' && c <= 'Z' {
return c - 13
}
return c
}, s)
}
17 changes: 17 additions & 0 deletions misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,20 @@ func TestUnhexString(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, []byte("ABCD"), b)
}

func TestBase64E(t *testing.T) {
s := Base64E([]byte("ABCD"))
assert.Equal(t, "QUJDRA==", s)
}

func TestBase64D(t *testing.T) {
b, err := Base64D("QUJDRA==")
assert.NoError(t, err)
assert.Equal(t, []byte("ABCD"), b)
}

func TestROT13(t *testing.T) {
s := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
r := ROT13(s)
assert.Equal(t, ROT13(s), r)
}
2 changes: 1 addition & 1 deletion tubes/listener.go → tube/listener.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tubes
package tube

import "net"

Expand Down
10 changes: 4 additions & 6 deletions tubes/process.go → tube/process.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tubes
package tube

import (
"os"
Expand Down Expand Up @@ -40,6 +40,9 @@ func NewProcess(argv []string, optFns ...func(o *ProcessOptions)) (*Process, err
if err != nil {
return nil, err
}
if err := cmd.Start(); err != nil {
return nil, err
}

return &Process{
cmd: cmd,
Expand All @@ -52,11 +55,6 @@ func NewProcess(argv []string, optFns ...func(o *ProcessOptions)) (*Process, err
}, nil
}

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

// PID returns the pid of the process.
func (p *Process) PID() int {
return p.cmd.Process.Pid
Expand Down
2 changes: 1 addition & 1 deletion tubes/process_darwin.go → tube/process_darwin.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// +build darwin
// +build cgo

package tubes
package tube

/*
#include <libproc.h>
Expand Down
2 changes: 1 addition & 1 deletion tubes/process_linux.go → tube/process_linux.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// +build linux

package tubes
package tube

import (
"github.com/shirou/gopsutil/v3/process"
Expand Down
14 changes: 1 addition & 13 deletions tubes/process_posix_test.go → tube/process_posix_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// +build linux freebsd openbsd darwin solaris

package tubes
package tube

import (
"path/filepath"
Expand All @@ -14,9 +14,6 @@ func TestProcess(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"), out)
Expand All @@ -26,9 +23,6 @@ func TestProcess(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)

Expand All @@ -43,9 +37,6 @@ func TestProcess(t *testing.T) {
})
assert.NoError(t, err)

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

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

Expand All @@ -61,9 +52,6 @@ func TestProcess(t *testing.T) {
})
assert.NoError(t, err)

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

_, err = p.SendLine("cd .. && pwd")
assert.NoError(t, err)

Expand Down
2 changes: 1 addition & 1 deletion tubes/remote.go → tube/remote.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tubes
package tube

import "net"

Expand Down
2 changes: 1 addition & 1 deletion tubes/remote_test.go → tube/remote_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tubes
package tube

import (
"net"
Expand Down
2 changes: 1 addition & 1 deletion tubes/tube.go → tube/tube.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tubes
package tube

import (
"bufio"
Expand Down
34 changes: 32 additions & 2 deletions web.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ type HTTPClientOptions struct {
Timeout time.Duration
ProxyURL string
TLSClientConfig *tls.Config
Cookie *http.Cookie
Headers map[string]string
UserAgent string
}

func HTTPGet(url string, optFns ...func(o *HTTPClientOptions)) ([]byte, error) {
Expand All @@ -24,8 +27,12 @@ func HTTPGet(url string, optFns ...func(o *HTTPClientOptions)) ([]byte, error) {
fn(&options)
}
client := newHTTPClient(options)
req, err := newHTTPRequest("GET", url, options)
if err != nil {
return nil, err
}

res, err := client.Get(url)
res, err := client.Do(req)
if err != nil {
return nil, err
}
Expand All @@ -46,8 +53,12 @@ func Download(url, filename string, optFns ...func(o *HTTPClientOptions)) error
fn(&options)
}
client := newHTTPClient(options)
req, err := newHTTPRequest("GET", url, options)
if err != nil {
return err
}

res, err := client.Get(url)
res, err := client.Do(req)
if err != nil {
return err
}
Expand Down Expand Up @@ -81,3 +92,22 @@ func newHTTPClient(options HTTPClientOptions) *http.Client {
Transport: transport,
}
}

func newHTTPRequest(method, url string, options HTTPClientOptions) (*http.Request, error) {
r, err := http.NewRequest(method, url, nil)
if err != nil {
return nil, err
}
if options.Cookie != nil {
r.AddCookie(options.Cookie)
}
if options.UserAgent != "" {
r.Header.Set("User-Agent", options.UserAgent)
}
if len(options.Headers) > 0 {
for k, v := range options.Headers {
r.Header.Set(k, v)
}
}
return r, nil
}

0 comments on commit 992c3ca

Please sign in to comment.