Skip to content

Commit 8b86cc3

Browse files
authored
Allow creating custom templates in user-config directory (Gallopsled#2257)
* Allow creating custom templates in user-config directory * Update changelog * add cmdline parameter for template * direct template file selection, added more help * added template path to help string * remove testing template * allow crash on invalid template file * python2 compatibility * improved documentation for template --------- Co-authored-by: Hannes Weissteiner <[email protected]>
1 parent 20f0779 commit 8b86cc3

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,14 @@ The table below shows which release corresponds to each branch, and what date th
7272
- [#2221][2221] Add shellcraft.sleep template wrapping SYS_nanosleep
7373
- [#2219][2219] Fix passing arguments on the stack in shellcraft syscall template
7474
- [#2212][2212] Add `--libc libc.so` argument to `pwn template` command
75+
- [#2257][2257] Allow creation of custom templates for `pwn template` command
7576

7677
[2202]: https://github.com/Gallopsled/pwntools/pull/2202
7778
[2117]: https://github.com/Gallopsled/pwntools/pull/2117
7879
[2221]: https://github.com/Gallopsled/pwntools/pull/2221
7980
[2219]: https://github.com/Gallopsled/pwntools/pull/2219
8081
[2212]: https://github.com/Gallopsled/pwntools/pull/2212
82+
[2257]: https://github.com/Gallopsled/pwntools/pull/2257
8183

8284
## 4.11.0 (`beta`)
8385

pwnlib/commandline/template.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55
from pwn import *
66
from pwnlib.commandline import common
77

8-
from mako.lookup import TemplateLookup
8+
from mako.lookup import TemplateLookup, Template
99

1010
parser = common.parser_commands.add_parser(
1111
'template',
1212
help = 'Generate an exploit template',
1313
description = 'Generate an exploit template'
1414
)
1515

16+
# change path to hardcoded one when building the documentation
17+
printable_data_path = "pwnlib/data" if 'sphinx' in sys.modules else pwnlib.data.path
18+
1619
parser.add_argument('exe', nargs='?', help='Target binary')
1720
parser.add_argument('--host', help='Remote host / SSH server')
1821
parser.add_argument('--port', help='Remote port / SSH port', type=int)
@@ -22,10 +25,17 @@
2225
parser.add_argument('--path', help='Remote path of file on SSH server')
2326
parser.add_argument('--quiet', help='Less verbose template comments', action='store_true')
2427
parser.add_argument('--color', help='Print the output in color', choices=['never', 'always', 'auto'], default='auto')
28+
parser.add_argument('--template', help='Path to a custom template. Tries to use \'~/.config/pwntools/templates/pwnup.mako\', if it exists. '
29+
'Check \'%s\' for the default template shipped with pwntools.' %
30+
os.path.join(printable_data_path, "templates", "pwnup.mako"))
2531

2632
def main(args):
33+
2734
lookup = TemplateLookup(
28-
directories = [os.path.join(pwnlib.data.path, 'templates')],
35+
directories = [
36+
os.path.expanduser('~/.config/pwntools/templates/'),
37+
os.path.join(pwnlib.data.path, 'templates')
38+
],
2939
module_directory = None
3040
)
3141

@@ -48,7 +58,12 @@ def main(args):
4858
if not args.exe:
4959
args.exe = os.path.basename(args.path)
5060

51-
template = lookup.get_template('pwnup.mako')
61+
62+
if args.template:
63+
template = Template(filename=args.template) # Failing on invalid file is ok
64+
else:
65+
template = lookup.get_template('pwnup.mako')
66+
5267
output = template.render(args.exe,
5368
args.host,
5469
args.port,
@@ -77,3 +92,4 @@ def main(args):
7792

7893
if __name__ == '__main__':
7994
pwnlib.commandline.common.main(__file__)
95+

0 commit comments

Comments
 (0)