Skip to content

Commit cdb9ab0

Browse files
authored
Add templating support (#23)
1 parent 2dc0961 commit cdb9ab0

File tree

5 files changed

+81
-11
lines changed

5 files changed

+81
-11
lines changed

README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,22 @@ it is required to apply multiple encodings to a payload (and possibly inserting
88

99
## Installation
1010
```
11-
go get -u github.com/ffuf/pencode/cmd/pencode
11+
go install github.com/ffuf/pencode/cmd/pencode
1212
```
1313

1414
### Usage
1515

1616
```
17-
pencode - complex payload encoder v0.1
17+
pencode - complex payload encoder v0.2
1818
19-
Usage: pencode ENCODER1 ENCODER2 ENCODER3...
19+
Usage: ./pencode ENCODER1 ENCODER2 ENCODER3...
2020
21-
pencode reads input from stdin, which is typically piped from another process.
21+
./pencode reads input from stdin, which is typically piped from another process.
2222
2323
Available encoders:
2424
b64decode - Base64 decoder
2525
b64encode - Base64 encoder
26+
filename.tmpl - Replaces string #PAYLOAD# in content of a file that has .tmpl extension.
2627
hexdecode - Hex string decoder
2728
hexencode - Hex string encoder
2829
jsonescape - JSON escape
@@ -36,6 +37,7 @@ Available encoders:
3637
utf16be - UTF-16 encoder (Big Endian)
3738
xmlescape - XML escape
3839
xmlunescape - XML unescape
40+
3941
```
4042

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

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

6774
### Shell completion
6875

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
module "github.com/ffuf/pencode"
1+
module github.com/ffuf/pencode
22

33
go 1.14

pkg/pencode/constants.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
package pencode
22

3-
const VERSION = "0.1"
3+
const VERSION = "0.2"

pkg/pencode/encoders.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
var availableEncoders = map[string]Encoder{
99
"b64encode": Base64Encoder{},
1010
"b64decode": Base64Decoder{},
11+
"filename.tmpl": Template{},
1112
"hexencode": HexEncoder{},
1213
"hexdecode": HexDecoder{},
1314
"jsonescape": JSONEscaper{},
@@ -40,8 +41,19 @@ func (c *Chain) Initialize(actions []string) error {
4041
c.actions = actions
4142
c.Encoders = make([]Encoder, 0)
4243
for _, a := range actions {
43-
if c.HasEncoder(a) {
44-
c.Encoders = append(c.Encoders, availableEncoders[a])
44+
if ok, err := c.HasEncoder(a); ok {
45+
// Templates are a bit special
46+
if isTemplate(a) {
47+
tenc, err := NewTemplateEncoder(a)
48+
if err != nil {
49+
return err
50+
}
51+
c.Encoders = append(c.Encoders, tenc)
52+
} else {
53+
c.Encoders = append(c.Encoders, availableEncoders[a])
54+
}
55+
} else if err != nil {
56+
return err
4557
} else {
4658
return fmt.Errorf("Encoder %s requested but not found.\n", a)
4759
}
@@ -65,11 +77,15 @@ func (c *Chain) Encode(input []byte) ([]byte, error) {
6577
}
6678

6779
//HasEncoder returns true if encoder with a specified name is configured
68-
func (c *Chain) HasEncoder(name string) bool {
80+
func (c *Chain) HasEncoder(name string) (bool, error) {
6981
if _, ok := availableEncoders[name]; ok {
70-
return true
82+
return true, nil
7183
}
72-
return false
84+
// Check for template
85+
if isTemplate(name) {
86+
return hasTemplate(name)
87+
}
88+
return false, nil
7389
}
7490

7591
func (c *Chain) GetEncoders() []string {

pkg/pencode/template.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package pencode
2+
3+
import (
4+
"io/ioutil"
5+
"os"
6+
"strings"
7+
)
8+
9+
//isTemplate checks if the name ends with string ".tmpl"
10+
func isTemplate(name string) bool {
11+
return strings.HasSuffix(name, ".tmpl")
12+
}
13+
14+
//hasTemplate checks if the name points to an existing file
15+
func hasTemplate(name string) (bool, error) {
16+
if _, err := os.Stat(name); err == nil {
17+
return true, nil
18+
} else {
19+
return false, err
20+
}
21+
}
22+
23+
type Template struct {
24+
Data []byte
25+
Keyword string
26+
}
27+
28+
func NewTemplateEncoder(name string) (Encoder, error) {
29+
var ok bool
30+
var err error
31+
t := Template{}
32+
// Using static keyword for now
33+
t.Keyword = "#PAYLOAD#"
34+
35+
if ok, err = hasTemplate(name); ok {
36+
t.Data, err = ioutil.ReadFile(name)
37+
}
38+
return t, err
39+
}
40+
41+
func (t Template) Encode(input []byte) ([]byte, error) {
42+
return []byte(strings.ReplaceAll(string(t.Data), t.Keyword, string(input))), nil
43+
}
44+
45+
func (t Template) HelpText() string {
46+
return "Replaces string #PAYLOAD# in content of a file that has .tmpl extension."
47+
}

0 commit comments

Comments
 (0)