-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 4616ab6
Showing
6 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"flag" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/ffuf/pencode/pkg/pencode" | ||
) | ||
|
||
func main() { | ||
chain := pencode.NewChain() | ||
flag.Usage = func() { | ||
fmt.Printf("pencode - complex payload encoder v%s\n\n", pencode.VERSION) | ||
fmt.Printf("Usage: %s ENCODER1 ENCODER2 ENCODER3...\n\n", os.Args[0]) | ||
fmt.Printf("%s reads input from stdin, which is typically piped from another process.\n\n", os.Args[0]) | ||
fmt.Printf("Available encoders:\n") | ||
chain.Usage() | ||
} | ||
flag.Parse() | ||
if len(os.Args) < 2 { | ||
flag.Usage() | ||
os.Exit(1) | ||
} | ||
err := chain.Initialize(os.Args[1:]) | ||
if err != nil { | ||
flag.Usage() | ||
fmt.Printf("\n[!] %s", err) | ||
os.Exit(1) | ||
} | ||
|
||
input := readInput() | ||
output, err := chain.Encode([]byte(input)) | ||
if err != nil { | ||
fmt.Printf(" [!] %s", err) | ||
} | ||
fmt.Print(string(output)) | ||
} | ||
|
||
func readInput() string { | ||
input := os.Stdin | ||
defer input.Close() | ||
reader := bufio.NewScanner(input) | ||
|
||
data := "" | ||
for reader.Scan() { | ||
data = data + reader.Text() | ||
} | ||
return data | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module "github.com/ffuf/pencode" | ||
|
||
go 1.14 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package pencode | ||
|
||
import ( | ||
"encoding/base64" | ||
) | ||
|
||
type Base64Encoder struct{} | ||
|
||
func (b Base64Encoder) Encode(input []byte) ([]byte, error) { | ||
output := base64.StdEncoding.EncodeToString(input) | ||
return []byte(output), nil | ||
} | ||
|
||
func (b Base64Encoder) HelpText() string { | ||
return "Base64 encoder" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package pencode | ||
|
||
const VERSION = "0.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package pencode | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
var availableEncoders = map[string]Encoder{ | ||
"b64encode": Base64Encoder{}, | ||
} | ||
|
||
type Chain struct { | ||
Encoders []Encoder | ||
initialized bool | ||
actions []string | ||
} | ||
|
||
func NewChain() *Chain { | ||
c := Chain{initialized: false} | ||
return &c | ||
} | ||
|
||
//Initialize loops through requested names for encoders and sets up the encoder chain. If an unknown encoder is | ||
//requested, error will be returned. | ||
func (c *Chain) Initialize(actions []string) error { | ||
c.actions = actions | ||
c.Encoders = make([]Encoder, 0) | ||
for _, a := range actions { | ||
if c.HasEncoder(a) { | ||
c.Encoders = append(c.Encoders, availableEncoders[a]) | ||
} else { | ||
return fmt.Errorf("Encoder %s requested but not found.\n", a) | ||
} | ||
} | ||
c.initialized = true | ||
return nil | ||
} | ||
|
||
func (c *Chain) Encode(input []byte) ([]byte, error) { | ||
var err error | ||
if !c.initialized { | ||
return []byte{}, fmt.Errorf("Encoder chain not initialized.\n") | ||
} | ||
for _, e := range c.Encoders { | ||
input, err = e.Encode(input) | ||
if err != nil { | ||
return []byte{}, err | ||
} | ||
} | ||
return input, nil | ||
} | ||
|
||
//HasEncoder returns true if encoder with a specified name is configured | ||
func (c *Chain) HasEncoder(name string) bool { | ||
if _, ok := availableEncoders[name]; ok { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
//Usage prints the help string for each configured encoder | ||
func (c *Chain) Usage() { | ||
// Calculate maximum keyword length for nice help formatting | ||
max_length := 0 | ||
for k := range availableEncoders { | ||
if len(k) > max_length { | ||
max_length = len(k) | ||
} | ||
} | ||
format := fmt.Sprintf(" %%-%ds- %%s\n", max_length+2) | ||
|
||
for k, v := range availableEncoders { | ||
fmt.Printf(format, k, v.HelpText()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package pencode | ||
|
||
type Encoder interface { | ||
Encode([]byte) ([]byte, error) | ||
HelpText() string | ||
} |