Skip to content

Commit

Permalink
Add templating support (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
joohoi authored May 6, 2021
1 parent 2dc0961 commit cdb9ab0
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 11 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,22 @@ it is required to apply multiple encodings to a payload (and possibly inserting

## Installation
```
go get -u github.com/ffuf/pencode/cmd/pencode
go install github.com/ffuf/pencode/cmd/pencode
```

### Usage

```
pencode - complex payload encoder v0.1
pencode - complex payload encoder v0.2
Usage: pencode ENCODER1 ENCODER2 ENCODER3...
Usage: ./pencode ENCODER1 ENCODER2 ENCODER3...
pencode reads input from stdin, which is typically piped from another process.
./pencode reads input from stdin, which is typically piped from another process.
Available encoders:
b64decode - Base64 decoder
b64encode - Base64 encoder
filename.tmpl - Replaces string #PAYLOAD# in content of a file that has .tmpl extension.
hexdecode - Hex string decoder
hexencode - Hex string encoder
jsonescape - JSON escape
Expand All @@ -36,6 +37,7 @@ Available encoders:
utf16be - UTF-16 encoder (Big Endian)
xmlescape - XML escape
xmlunescape - XML unescape
```

To urlencode, base64encode and hex encode a string:
Expand Down Expand Up @@ -63,6 +65,11 @@ $ echo 'what%ever'|pencode urlencode b64encode hexencode
- XML escape encoder (reserved characters)
- XML escape decoder

### Templating

Any command line parameter that is a file path ending with `.tmpl` is considered as a template file by
pencode. It attempts to read the file content and to replace instances of a string `#PAYLOAD#` within the file with
the input in the current encoder chain.

### Shell completion

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module "github.com/ffuf/pencode"
module github.com/ffuf/pencode

go 1.14
2 changes: 1 addition & 1 deletion pkg/pencode/constants.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package pencode

const VERSION = "0.1"
const VERSION = "0.2"
26 changes: 21 additions & 5 deletions pkg/pencode/encoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
var availableEncoders = map[string]Encoder{
"b64encode": Base64Encoder{},
"b64decode": Base64Decoder{},
"filename.tmpl": Template{},
"hexencode": HexEncoder{},
"hexdecode": HexDecoder{},
"jsonescape": JSONEscaper{},
Expand Down Expand Up @@ -40,8 +41,19 @@ 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])
if ok, err := c.HasEncoder(a); ok {
// Templates are a bit special
if isTemplate(a) {
tenc, err := NewTemplateEncoder(a)
if err != nil {
return err
}
c.Encoders = append(c.Encoders, tenc)
} else {
c.Encoders = append(c.Encoders, availableEncoders[a])
}
} else if err != nil {
return err
} else {
return fmt.Errorf("Encoder %s requested but not found.\n", a)
}
Expand All @@ -65,11 +77,15 @@ func (c *Chain) Encode(input []byte) ([]byte, error) {
}

//HasEncoder returns true if encoder with a specified name is configured
func (c *Chain) HasEncoder(name string) bool {
func (c *Chain) HasEncoder(name string) (bool, error) {
if _, ok := availableEncoders[name]; ok {
return true
return true, nil
}
return false
// Check for template
if isTemplate(name) {
return hasTemplate(name)
}
return false, nil
}

func (c *Chain) GetEncoders() []string {
Expand Down
47 changes: 47 additions & 0 deletions pkg/pencode/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package pencode

import (
"io/ioutil"
"os"
"strings"
)

//isTemplate checks if the name ends with string ".tmpl"
func isTemplate(name string) bool {
return strings.HasSuffix(name, ".tmpl")
}

//hasTemplate checks if the name points to an existing file
func hasTemplate(name string) (bool, error) {
if _, err := os.Stat(name); err == nil {
return true, nil
} else {
return false, err
}
}

type Template struct {
Data []byte
Keyword string
}

func NewTemplateEncoder(name string) (Encoder, error) {
var ok bool
var err error
t := Template{}
// Using static keyword for now
t.Keyword = "#PAYLOAD#"

if ok, err = hasTemplate(name); ok {
t.Data, err = ioutil.ReadFile(name)
}
return t, err
}

func (t Template) Encode(input []byte) ([]byte, error) {
return []byte(strings.ReplaceAll(string(t.Data), t.Keyword, string(input))), nil
}

func (t Template) HelpText() string {
return "Replaces string #PAYLOAD# in content of a file that has .tmpl extension."
}

0 comments on commit cdb9ab0

Please sign in to comment.