Version 0.2.0

master
Rob Glew 9 years ago
parent 312b985229
commit 26376eaaec
  1. 1
      Makefile
  2. 232
      README.md
  3. 6
      docs/source/conf.py
  4. 1
      docs/source/index.rst
  5. 371
      docs/source/overview.rst
  6. 450
      docs/source/pappyplugins.rst
  7. 82
      docs/source/pappyproxy.rst
  8. 16
      docs/source/pappyproxy.schema.rst
  9. 91
      docs/source/tutorial.rst
  10. 19
      pappyproxy/comm.py
  11. 76
      pappyproxy/config.py
  12. 1394
      pappyproxy/console.py
  13. 839
      pappyproxy/context.py
  14. 1154
      pappyproxy/http.py
  15. 2
      pappyproxy/iter.py
  16. 113
      pappyproxy/macros.py
  17. 85
      pappyproxy/pappy.py
  18. 150
      pappyproxy/plugin.py
  19. 0
      pappyproxy/plugins/__init__.py
  20. 192
      pappyproxy/plugins/filter.py
  21. 215
      pappyproxy/plugins/macrocmds.py
  22. 243
      pappyproxy/plugins/manglecmds.py
  23. 85
      pappyproxy/plugins/misc.py
  24. 102
      pappyproxy/plugins/tagcmds.py
  25. 328
      pappyproxy/plugins/view.py
  26. 0
      pappyproxy/plugins/vim_repeater/__init__.py
  27. 135
      pappyproxy/plugins/vim_repeater/repeater.py
  28. 17
      pappyproxy/plugins/vim_repeater/repeater.vim
  29. 86
      pappyproxy/proxy.py
  30. 36
      pappyproxy/schema/schema_2.py
  31. 50
      pappyproxy/schema/schema_4.py
  32. 29
      pappyproxy/schema/schema_5.py
  33. 45
      pappyproxy/schema/update.py
  34. 6
      pappyproxy/templates/intmacro.py
  35. 2
      pappyproxy/templates/macro.py
  36. 211
      pappyproxy/tests/old_test_mangle.py
  37. 98
      pappyproxy/tests/test_context.py
  38. 153
      pappyproxy/tests/test_http.py
  39. 13
      pappyproxy/tests/test_proxy.py
  40. 6
      pappyproxy/tests/test_session.py
  41. 21
      pappyproxy/util.py
  42. 4
      setup.py

@ -2,5 +2,6 @@
docs: docs:
pandoc --from=markdown --to=rst --output=docs/source/overview.rst README.md pandoc --from=markdown --to=rst --output=docs/source/overview.rst README.md
cd docs; make html cd docs; make html
cd docs/build/html; touch .nojekyll
.PHONY: docs .PHONY: docs

@ -4,7 +4,7 @@ The Pappy Proxy
Introduction Introduction
------------ ------------
The Pappy (**P**roxy **A**ttack **P**roxy **P**rox**Y**) Proxy is an intercepting proxy for performing web application security testing. Its features are often similar, or straight up rippoffs from [Burp Suite](https://portswigger.net/burp/). However, Burp Suite is neither open source nor a command line tool, thus making a proxy like Pappy inevitable. The project is still in its early stages, so there are bugs and only the bare minimum features, but it should be able to do some cool stuff soon (I'm already using it for real work). The Pappy (**P**roxy **A**ttack **P**roxy **P**rox**Y**) Proxy is an intercepting proxy for performing web application security testing. Its features are often similar, or straight up rippoffs from [Burp Suite](https://portswigger.net/burp/). However, Burp Suite is neither open source nor a command line tool, thus making a proxy like Pappy inevitable. The project is still in its early stages, so there are bugs and only the bare minimum features, but it can already do some cool stuff.
Contributing Contributing
------------ ------------
@ -12,6 +12,8 @@ Contributing
If you're brave and want to try and contribute code, please let me know. Right now the codebase is kind of rough and I have refactored it a few times already, but I would be more than happy to find a stable part of the codebase that you can contribute to. If you're brave and want to try and contribute code, please let me know. Right now the codebase is kind of rough and I have refactored it a few times already, but I would be more than happy to find a stable part of the codebase that you can contribute to.
Another option is to try writing a plugin. It might be a bit easier than contributing code and plugins are extremely easy to integrate as a core feature. So you can also contribute by writing a plugin and letting me know about it. You can find out more by looking at [the official plugin docs](https://roglew.github.io/pappy-proxy/pappyplugins.html).
How to Use It How to Use It
============= =============
@ -26,7 +28,7 @@ $ pip install .
Quickstart Quickstart
---------- ----------
Pappy projects take up an entire directory. Any generated scripts, exported responses, etc. will be placed in the current directory so it's good to give your project a directory of its own. To start a project, do something like: Pappy projects take up an entire directory. Any generated scripts, exported responses, plugin data, etc. will be placed in the current directory so it's good to give your project a directory of its own. To start a project, do something like:
``` ```
$ mkdir test_project $ mkdir test_project
@ -34,7 +36,7 @@ $ cd test_project
$ pappy $ pappy
Copying default config to directory Copying default config to directory
Proxy is listening on port 8000 Proxy is listening on port 8000
itsPappyTime> exit pappy> exit
$ ls $ ls
data.db project_config.json data.db project_config.json
$ $
@ -51,7 +53,7 @@ Example:
$ pappy -l $ pappy -l
Temporary datafile is /tmp/tmpw4mGv2 Temporary datafile is /tmp/tmpw4mGv2
Proxy is listening on port 8000 Proxy is listening on port 8000
itsPappyTime> quit pappy> quit
Deleting temporary datafile Deleting temporary datafile
$ $
``` ```
@ -173,6 +175,7 @@ The context is a set of filters that define which requests are considered "activ
|:--------|:------------|:---| |:--------|:------------|:---|
| `f <filter string>` | filter, fl, f |Add a filter that limits which requests are included in the current context. See the Filter String section for how to create a filter string | | `f <filter string>` | filter, fl, f |Add a filter that limits which requests are included in the current context. See the Filter String section for how to create a filter string |
| `fc` | filter_clear, fc | Clears the filters and resets the context to contain all requests and responses. Ignores scope | | `fc` | filter_clear, fc | Clears the filters and resets the context to contain all requests and responses. Ignores scope |
| `fu` | filter_up, fu | Removes the most recently applied filter |
| `fls` | filter_list, fls | Print the filters that make up the current context | | `fls` | filter_list, fls | Print the filters that make up the current context |
| `filter_prune` | filter_prune | Delete all the requests that aren't in the current context from the data file | | `filter_prune` | filter_prune | Delete all the requests that aren't in the current context from the data file |
@ -259,6 +262,14 @@ Matches both A and B but not C
| gt | gt | A gt B if A > B (A and B must be a number) | | gt | gt | A gt B if A > B (A and B must be a number) |
| lt | lt | A lt B if A < B (A and B must be a number) | | lt | lt | A lt B if A < B (A and B must be a number) |
### Special form filters
A few filters don't conform to the field, comparer, value format. You can still negate these.
| Format | Aliases | Description |
|:--|:--|:--|
| before <reqid> | before, bf, b4 | Filters out any request that is not before the given request. Filters out any request without a time. |
| after <reqid> | after, af | Filters out any request that is not before the given request. Filters out any request without a time. |
Scope Scope
----- -----
Scope is a set of rules to define whether Pappy should mess with a request. You define the scope by setting the context to what you want the scope to be and running `scope_save`. The scope is saved in the data file and is automatically restored when using the same project directory. Scope is a set of rules to define whether Pappy should mess with a request. You define the scope by setting the context to what you want the scope to be and running `scope_save`. The scope is saved in the data file and is automatically restored when using the same project directory.
@ -346,7 +357,7 @@ $ ls -l
In this case we have a `blank`, `hackthensa`, `testgen`, and `test` macro. A macro script is any python script that defines a `run_macro(args)` function and a `MACRO_NAME` variable. For example, a simple macro would be: In this case we have a `blank`, `hackthensa`, `testgen`, and `test` macro. A macro script is any python script that defines a `run_macro(args)` function and a `MACRO_NAME` variable. For example, a simple macro would be:
``` ```
--- macro_print.py ### macro_print.py
MACRO_NAME = 'Print Macro' MACRO_NAME = 'Print Macro'
@ -362,19 +373,19 @@ You can place this macro in your project directory then load and run it from Pap
``` ```
$ pappy $ pappy
Proxy is listening on port 8000 Proxy is listening on port 8000
itsPappyTime> lma pappy> lma
Loaded "<Macro Test Macro (tm/test)>" Loaded "<Macro Test Macro (tm/test)>"
Loaded "<Macro Macro 6494496 (testgen)>" Loaded "<Macro Macro 6494496 (testgen)>"
Loaded "<Macro Print Macro (print)>" Loaded "<Macro Print Macro (print)>"
Loaded "<Macro Hack the NSA (htnsa/hackthensa)>" Loaded "<Macro Hack the NSA (htnsa/hackthensa)>"
Loaded "<Macro Macro 62449408 (blank)>" Loaded "<Macro Macro 62449408 (blank)>"
itsPappyTime> rma print pappy> rma print
Hello, Pappy! Hello, Pappy!
itsPappyTime> rma print NSA pappy> rma print NSA
Hello, NSA! Hello, NSA!
itsPappyTime> rma print Idiot Slayer pappy> rma print Idiot Slayer
Hello, Idiot! Hello, Idiot!
itsPappyTime> rma print "Idiot Slayer" pappy> rma print "Idiot Slayer"
Hello, Idiot Slayer! Hello, Idiot Slayer!
``` ```
@ -387,16 +398,16 @@ You can also generate macros that have Pappy `Request` objects created with the
``` ```
$ pappy $ pappy
Proxy is listening on port 8000 Proxy is listening on port 8000
itsPappyTime> ls pappy> ls
ID Verb Host Path S-Code Req Len Rsp Len Time Mngl ID Verb Host Path S-Code Req Len Rsp Len Time Mngl
5 GET vitaly.sexy /esr1.jpg 200 OK 0 17653 -- -- 5 GET vitaly.sexy /esr1.jpg 200 OK 0 17653 -- --
4 GET vitaly.sexy /netscape.gif 200 OK 0 1135 -- -- 4 GET vitaly.sexy /netscape.gif 200 OK 0 1135 -- --
3 GET vitaly.sexy /construction.gif 200 OK 0 28366 -- -- 3 GET vitaly.sexy /construction.gif 200 OK 0 28366 -- --
2 GET vitaly.sexy /vitaly2.jpg 200 OK 0 2034003 -- -- 2 GET vitaly.sexy /vitaly2.jpg 200 OK 0 2034003 -- --
1 GET vitaly.sexy / 200 OK 0 1201 -- -- 1 GET vitaly.sexy / 200 OK 0 1201 -- --
itsPappyTime> gma sexy 1 pappy> gma sexy 1
Wrote script to macro_sexy.py Wrote script to macro_sexy.py
itsPappyTime> quit pappy> quit
$ cat macro_sexy.py $ cat macro_sexy.py
from pappyproxy.http import Request, get_request, post_request from pappyproxy.http import Request, get_request, post_request
@ -430,7 +441,7 @@ def run_macro(args):
pass pass
``` ```
If you enter in a value for `SHORT_NAME`, you can use it as a shortcut to run that macro. So if in a macro you set `SHORT_NAME='tm'` you can run it by running `itsPappyTime> rma tm`. If you enter in a value for `SHORT_NAME`, you can use it as a shortcut to run that macro. So if in a macro you set `SHORT_NAME='tm'` you can run it by running `pappy> rma tm`.
| Command | Aliases | Description | | Command | Aliases | Description |
|:--------|:--------|:------------| |:--------|:--------|:------------|
@ -441,9 +452,9 @@ If you enter in a value for `SHORT_NAME`, you can use it as a shortcut to run th
### Request Objects ### Request Objects
The main method of interacting with the proxy is through `Request` objects. You can submit a request with `req.sumbit()` and save it to the data file with `req.save()`. The objects also have attributes which can be used to modify the request in a high-level way. Unfortunately, I haven't gotten around to writing full docs on the API and it's still changing every once in a while so I apologize if I pull the carpet out from underneath you. The main method of interacting with the proxy is through `Request` objects. You can submit a request with `req.sumbit()` and save it to the data file with `req.save()`. The objects also have attributes which can be used to modify the request in a high-level way. You can see the [full documentation](https://roglew.github.io/pappy-proxy/pappyproxy.html#module-pappyproxy.http) for more details on using these objects.
Dict-like objects are represented with a custom class called a `RepeatableDict`. I haven't gotten around to writing docs on it yet, so just interact with it like a dict and don't be surprised if it's missing some methods you would expect a dict to have. Dict-like objects are represented with a custom class called a `RepeatableDict`. Again, look at the docs for details. For the most part, you can interact with it like a normal dictionary, but don't be surprised if it's missing some methods you would expect.
Here is a quick list of attributes that you can use with `Request` objects: Here is a quick list of attributes that you can use with `Request` objects:
@ -500,12 +511,30 @@ Like I said, these interfaces are prone to change and will probably crash when y
### Useful Functions ### Useful Functions
There are also a few functions which could be useful for creating requests in macros. There are also a few functions which could be useful for creating requests in macros. It's worth pointing out that `request_by_id` is useful for passing request objects as arguments. For example, here is a macro that lets you resubmit a request with the Google Bot user agent:
```
## macro_googlebot.py
from pappyproxy.http import Request, get_request, post_request, request_by_id
from pappyproxy.context import set_tag
from pappyproxy.iter import *
MACRO_NAME = 'Submit as Google'
SHORT_NAME = ''
def run_macro(args):
req = request_by_id(args[0])
req.headers['User-Agent'] = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
req.submit()
req.save()
```
| Function | Description | | Function | Description |
|:--|:--| |:--|:--|
| get_request(url, url_params={}) | Returns a Request object that contains a GET request to the given url with the given url params | | get_request(url, url_params={}) | Returns a Request object that contains a GET request to the given url with the given url params |
| post_request(url, post_params={}, url_params={}) | Returns a Request object that contains a POST request to the given url with the given url and post params | | post_request(url, post_params={}, url_params={}) | Returns a Request object that contains a POST request to the given url with the given url and post params |
| request_by_id(reqid) | Get a request object from its id. |
Intercepting Macros Intercepting Macros
------------------- -------------------
@ -556,6 +585,46 @@ def mangle_request(request):
return r return r
``` ```
In addition, you can use an `init(args)` function to get arguments from the command line. If no arguments are passed, args will be an empty list. Here is an example macro that does a search and replace:
```
## int_replace.py
MACRO_NAME = 'Find and Replace'
SHORT_NAME = ''
runargs = []
def init(args):
global runargs
runargs = args
def mangle_request(request):
global runargs
if len(runargs) < 2:
return request
request.body = request.body.replace(runargs[0], runargs[1])
return request
def mangle_response(request):
global runargs
if len(runargs) < 2:
return request.response
request.response.body = request.response.body.replace(runargs[0], runargs[1])
return request.response
```
You can use this macro to do any search and replace that you want. For example, if you wanted to replace "Google" with "Skynet", you can run the macro like this:
```
pappy> lma
Loaded "<InterceptingMacro Find and Replace (replace)>"
pappy> rim replace Google Skynet
"Find and Replace" started
pappy>
```
Now every site that you visit will be a little bit more accurate.
### Enabling/Disabling Intercepting Macros ### Enabling/Disabling Intercepting Macros
You can use the following commands to start/stop intercepting macros You can use the following commands to start/stop intercepting macros
@ -563,8 +632,8 @@ You can use the following commands to start/stop intercepting macros
|:--------|:--------|:------------| |:--------|:--------|:------------|
| `lma [dir]` | `load_macros`, `lma` | Load macros from a directory. If `dir` is not given, use the current directory (the project directory) | | `lma [dir]` | `load_macros`, `lma` | Load macros from a directory. If `dir` is not given, use the current directory (the project directory) |
| `rim <macro name>` | `run_int_macro`, `rim` | Run an intercepting macro. Similarly to normal macros you can use the name, short name, or file name of the macro. | | `rim <macro name>` | `run_int_macro`, `rim` | Run an intercepting macro. Similarly to normal macros you can use the name, short name, or file name of the macro. |
| `sim <macro name>` | `stop_int_macro`, `sim` | Stop an intercepting macro. | | `sim <macro name> [args]` | `stop_int_macro`, `sim` | Stop an intercepting macro. If arguments are given, they will be passed to the macro's `init(args)` function if it exists. |
| `lim` | `list_int_macros`, `lim` | List all enabled/disabled intercepting macros | | `lim` | `list_int_macros`, `lsim` | List all enabled/disabled intercepting macros |
| `gima <name>` | `generate_int_macro`, `gima` | Generate an intercepting macro with the given name. | | `gima <name>` | `generate_int_macro`, `gima` | Generate an intercepting macro with the given name. |
Logging Logging
@ -575,8 +644,8 @@ You can watch in real-time what requests are going through the proxy. Verbosisty
|:--------|:------------| |:--------|:------------|
| `log [verbosity]` | View the log at the given verbosity. Default verbosity is 1 which just shows connections being made/lost and some other info, verbosity 3 shows full requests/responses as they pass through and are processed by the proxy | | `log [verbosity]` | View the log at the given verbosity. Default verbosity is 1 which just shows connections being made/lost and some other info, verbosity 3 shows full requests/responses as they pass through and are processed by the proxy |
Additional Commands Additional Commands and Features
------------------- --------------------------------
This is a list of other random stuff you can do that isn't categorized under anything else. These are mostly commands that I found that I needed while doing a test and just added. They likely don't do a ton of error checking. This is a list of other random stuff you can do that isn't categorized under anything else. These are mostly commands that I found that I needed while doing a test and just added. They likely don't do a ton of error checking.
| Command | Aliases | Description | | Command | Aliases | Description |
@ -584,3 +653,124 @@ This is a list of other random stuff you can do that isn't categorized under any
| `dump_response <reqid> [filename]` | `dump_response` | Dumps the data from the response to the given filename (useful for images, .swf, etc). If no filename is given, it uses the name given in the path. | | `dump_response <reqid> [filename]` | `dump_response` | Dumps the data from the response to the given filename (useful for images, .swf, etc). If no filename is given, it uses the name given in the path. |
| `export <req|rsp> <reqid>` | `export` | Writes either the full request or response to a file in the current directory. | | `export <req|rsp> <reqid>` | `export` | Writes either the full request or response to a file in the current directory. |
### Response streaming
If you don't have any intercepting macros running, Pappy will forward data to the browser as it gets it. However, if you're trying to mangle messages/responses, Pappy will need to download the entire message first.
Plugins
-------
Note that this section is a very quick overview of plugins. For a full description of how to write them, please see [the official docs](https://roglew.github.io/pappy-proxy/pappyplugins.html).
It is also possible to write plugins which are reusable across projects. Plugins are simply Python scripts located in `~/.pappy/plugins`. Plugins are able to create new console commands and maintain state throughout a Pappy session. They can access the same API as macros, but the plugin system is designed to allow you to create general purpose commands as compared to macros which are meant to be project-specific scripts. Still, it may not be a bad idea to try building a macro to do something in a quick and dirty way before writing a plugin since plugins are more complicated to write.
A simple hello world plugin could be something like:
```
## hello.py
import shlex
def hello_world(line):
if line:
args = shlex.split(line)
print 'Hello, %s!' % (', '.join(args))
else:
print "Hello, world!"
###############
## Plugin hooks
def load_cmds(cmd):
cmd.set_cmds({
'hello': (hello_world, None),
})
cmd.add_aliases([
('hello', 'hlo'),
('hello', 'ho'),
])
```
You can also create commands which support autocomplete:
```
import shlex
_AUTOCOMPLETE_NAMES = ['alice', 'allie', 'sarah', 'mallory', 'slagathor']
def hello_world(line):
if line:
args = shlex.split(line)
print 'Hello, %s!' % (', '.join(args))
else:
print "Hello, world!"
def complete_hello_world(text, line, begidx, endidx):
return [n for n in _AUTOCOMPLETE_NAMES if n.startswith(text)]
###############
## Plugin hooks
def load_cmds(cmd):
cmd.set_cmds({
'hello': (hello_world, complete_hello_world),
})
cmd.add_aliases([
('hello', 'hlo'),
])
```
Then when you run Pappy you can use the ``hello`` command:
```
$ pappy -l
Temporary datafile is /tmp/tmpBOXyJ3
Proxy is listening on port 8000
pappy> ho
Hello, world!
pappy> ho foo bar baz
Hello, foo, bar, baz!
pappy> ho foo bar "baz lihtyur"
Hello, foo, bar, baz lihtyur!
pappy>
```
### Should I Write a Plugin or a Macro?
A lot of the time, you can get away with writing a macro. However, you may consider writing a plugin if:
* You find yourself copying one macro to multiple projects
* You want to write a general tool that can be applied to any website
* You need to maintain state during the Pappy session
My guess is that if you need one quick thing for a project, you're better off writing a macro first and seeing if you end up using it in future projects. Then if you find yourself needing it a lot, write a plugin for it. You may also consider keeping a `mine.py` plugin where you can write out commands that you use regularly but may not be worth creating a dedicated plugin for.
FAQ
---
### I still like Burp, but Pappy looks interesting, can I use both?
Yes! If you don't want to go completely over to Pappy yet, you can configure Burp to use Pappy as an upstream proxy server. That way, traffic will go through both Burp and Pappy and you can use whichever you want to do your testing.
How to have Burp forward traffic through Pappy:
1. Open Burp
2. Go to `Options -> Connections -> Upstream Proxy Servers`
3. Click `Add`
4. Leave `Destination Host` blank, but put `127.0.0.1` in `Proxy Host` and `8000` into `Port` (assuming you're using the default listener)
5. Configure your browser to use Burp as a proxy
### Why does my request have an id of `--`?!?!
You can't do anything with a request/response until it is decoded and saved to disk. In between the time when a request is decoded and when it's saved to disk, it will have an ID of `--`. So just wait a little bit and it will get an ID you can use.
Changelog
---------
The boring part of the readme
* 0.2.0
* Lots of refactoring
* Plugins
* Bugfixes probably
* Change prompt to make Pappy look more professional (but it will always be pappy time in your heart, I promise)
* Create changelog
* Add response streaming if no intercepting macros are active
* 0.1.1
* Start using sane versioning system
* Did proxy things

@ -59,9 +59,9 @@ author = u'Rob Glew'
# built documents. # built documents.
# #
# The short X.Y version. # The short X.Y version.
version = u'0.0.1' version = u'0.2.0'
# The full version, including alpha/beta/rc tags. # The full version, including alpha/beta/rc tags.
release = u'0.0.1' release = u'0.2.0'
# The language for content autogenerated by Sphinx. Refer to documentation # The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages. # for a list of supported languages.
@ -307,6 +307,8 @@ intersphinx_mapping = {'https://docs.python.org/': None}
def maybe_skip_member(app, what, name, obj, skip, options): def maybe_skip_member(app, what, name, obj, skip, options):
skip_vals = ('__doc__', '__module__', '__weakref__') skip_vals = ('__doc__', '__module__', '__weakref__')
if name[0] == '_':
return True
if name in skip_vals: if name in skip_vals:
return True return True
if obj.__doc__ is None: if obj.__doc__ is None:

@ -13,6 +13,7 @@ Contents:
overview overview
tutorial tutorial
pappyplugins
Indices and tables Indices and tables

@ -11,7 +11,7 @@ testing. Its features are often similar, or straight up rippoffs from
neither open source nor a command line tool, thus making a proxy like neither open source nor a command line tool, thus making a proxy like
Pappy inevitable. The project is still in its early stages, so there are Pappy inevitable. The project is still in its early stages, so there are
bugs and only the bare minimum features, but it should be able to do bugs and only the bare minimum features, but it should be able to do
some cool stuff soon (I'm already using it for realtm work). some cool stuff soon (I'm already using it for real work).
Contributing Contributing
------------ ------------
@ -24,9 +24,9 @@ know so that I can [STRIKEOUT:use it to stomp them into the dust]
improve my project. improve my project.
If you're brave and want to try and contribute code, please let me know. If you're brave and want to try and contribute code, please let me know.
Right now the codebase is a giant clusterfun which I have refactored a Right now the codebase is kind of rough and I have refactored it a few
few times already, but I would be more than happy to find a stable part times already, but I would be more than happy to find a stable part of
of the codebase that you can contribute to. the codebase that you can contribute to.
How to Use It How to Use It
============= =============
@ -48,11 +48,10 @@ installed correctly by running ``pappy -l`` to start the proxy.
Quickstart Quickstart
---------- ----------
Pappy projects take up an entire directory. While a full directory may Pappy projects take up an entire directory. Any generated scripts,
seem like a dumb idea compared to storing everything in a zip file, but exported responses, etc. will be placed in the current directory so it's
when it comes to generating attack strips and things, it's easier to good to give your project a directory of its own. To start a project, do
just keep everything in a directory so you can view/edit files with something like:
other programs. To start a project, do something like:
:: ::
@ -61,7 +60,7 @@ other programs. To start a project, do something like:
$ pappy $ pappy
Copying default config to directory Copying default config to directory
Proxy is listening on port 8000 Proxy is listening on port 8000
itsPappyTime> exit pappy> exit
$ ls $ ls
data.db project_config.json data.db project_config.json
$ $
@ -70,16 +69,18 @@ And that's it! The proxy will by default be running on port 8000 and
bound to localhost (to keep the hackers out). You can modify the bound to localhost (to keep the hackers out). You can modify the
port/interface in ``config.json``. You can list all your intercepted port/interface in ``config.json``. You can list all your intercepted
requests with ``ls``, view a full request with ``vfq <reqid>`` or view a requests with ``ls``, view a full request with ``vfq <reqid>`` or view a
full response with ``vfs <reqid>``. No you can't delete them yet. I'm full response with ``vfs <reqid>``. Right now, the only command to
working on it. delete requests is ``filter_prune`` which deletes all the requests that
aren't in the current context (look at the sections on the
context/filter strings for more information on that).
Lite Mode Lite Mode
--------- ---------
If you don't want to dirty up a directory, you can run Pappy in "lite" If you don't want to dirty up a directory, you can run Pappy in "lite"
mode. Pappy will use the default configuration settings and will create mode. Pappy will use the default configuration settings and will create
a temporary datafile in ``/tmp`` to use. When you quit, the file will be a temporary data file in ``/tmp`` to use. When you quit, the file will
deleted. If you want to run Pappy in line mode, run Pappy with either be deleted. If you want to run Pappy in lite mode, run Pappy with either
``-l`` or ``--lite``. ``-l`` or ``--lite``.
Example: Example:
@ -89,7 +90,7 @@ Example:
$ pappy -l $ pappy -l
Temporary datafile is /tmp/tmpw4mGv2 Temporary datafile is /tmp/tmpw4mGv2
Proxy is listening on port 8000 Proxy is listening on port 8000
itsPappyTime> quit pappy> quit
Deleting temporary datafile Deleting temporary datafile
$ $
@ -99,11 +100,10 @@ Adding The CA Cert to Your Browser
In order for Pappy to view data sent using HTTPS, you need to add a In order for Pappy to view data sent using HTTPS, you need to add a
generated CA cert (``certificate.crt``) to your browser. Certificates generated CA cert (``certificate.crt``) to your browser. Certificates
are generated using the ``gencerts`` command and are by default stored are generated using the ``gencerts`` command and are by default stored
in the same directory as ``pappy.py``. This allows Pappy to act as a CA in ``~/.pappy/certs``. This allows Pappy to act as a CA and sign any
and MITM HTTPS connections. I believe that Firefox and Chrome ignore HTTPS certificate it wants without the browser complaining. This allows
keychain/system certs, so you will have to install the CA cert to the Pappy to decrypt and modify HTTPS requests. The certificate installation
browsers instead of (or in addition to) adding the cert to your instructions are different for each browser.
keychain.
Firefox Firefox
~~~~~~~ ~~~~~~~
@ -156,11 +156,13 @@ proxy. The following fields can be used to configure the proxy:
The following tokens will also be replaced with values: The following tokens will also be replaced with values:
+------------------+------------------------------------------------+ +-----------------+-----------------------------------------------------+
| Token | Replaced with | | Token | Replaced with |
+==================+================================================+ +=================+=====================================================+
| ``{PAPPYDIR}`` | The directory where Pappy's files are stored | | ``{DATADIR}`` | The directory where Pappy's data files are stored |
+------------------+------------------------------------------------+ +-----------------+-----------------------------------------------------+
See the default ``config.json`` for examples.
Generating Pappy's CA Cert Generating Pappy's CA Cert
-------------------------- --------------------------
@ -168,16 +170,16 @@ Generating Pappy's CA Cert
In order to intercept and modify requests to sites that use HTTPS, you In order to intercept and modify requests to sites that use HTTPS, you
have to generate and install CA certs to your browser. You can do this have to generate and install CA certs to your browser. You can do this
by running the ``gencerts`` command in Pappy. By default, certs are by running the ``gencerts`` command in Pappy. By default, certs are
stored in the same directory as Pappy's script files. However, you can stored ``~/.pappy/certs``. This is also the default location that Pappy
change where Pappy will look for the private key file in the config will look for certificates (unless you specify otherwise in
file. In addition, you can give the ``gencerts`` command an argument to ``config.json``.) In addition, you can give the ``gencerts`` command an
have it put the generated certs in a different directory. argument to have it put the generated certs in a different directory.
+----------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------+ +----------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Command | Description | | Command | Description |
+========================================+================================================================================================================================================================+ +========================================+==================================================================================================================================================================================================================================================================================+
| ``gencerts [/path/to/put/certs/in]`` | Generate a CA cert that can be added to your browser to let Pappy decrypt HTTPS traffic. Also generates the private key for that cert in the same directory. | | ``gencerts [/path/to/put/certs/in]`` | Generate a CA cert that can be added to your browser to let Pappy decrypt HTTPS traffic. Also generates the private key for that cert in the same directory. If no path is given, the certs will be placed in the default certificate location. Overwrites any existing certs. |
+----------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------+ +----------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
Browsing Recorded Requests/Responses Browsing Recorded Requests/Responses
------------------------------------ ------------------------------------
@ -202,7 +204,7 @@ The following commands can be used to view requests and responses
| ``vhs <id(s)>`` | view\_response\_headers, vhs | [V]iew [H]eaders of a Re[S]ponse. Prints just the headers of a response associated with a request. | | ``vhs <id(s)>`` | view\_response\_headers, vhs | [V]iew [H]eaders of a Re[S]ponse. Prints just the headers of a response associated with a request. |
+--------------------+--------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +--------------------+--------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
The table shown will have the following columns: The table shown by ``ls`` will have the following columns:
+-----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +-----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Label | Description | | Label | Description |
@ -230,7 +232,7 @@ Tags
---- ----
You can apply tags to a request and use filters to view specific tags. You can apply tags to a request and use filters to view specific tags.
The following commands can be used to apply tags to requests: The following commands can be used to apply and remove tags to requests:
+---------------------------+-----------+---------------------------------------------------------------------------------------------------------------+ +---------------------------+-----------+---------------------------------------------------------------------------------------------------------------+
| Command | Aliases | Description | | Command | Aliases | Description |
@ -245,10 +247,13 @@ The following commands can be used to apply tags to requests:
Request IDs Request IDs
----------- -----------
Request IDs are how you identify a request. You can see it when you run Request IDs are how you identify a request and every command that
``ls``. In addition, you can prepend an ID with prefixes to get requests involves specifying a request will take one or more request IDs. You can
or responses associated with the request (for example its unmangled see it when you run ``ls``. In addition, you can prepend an ID with
request or response) Here are the valid prefixes: prefixes to get requests or responses associated with the request (for
example if you modified the request or its response with the
interceptor, you can get the unmangled versions.) Here are the valid
prefixes:
+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Prefix | Description | | Prefix | Description |
@ -258,9 +263,9 @@ request or response) Here are the valid prefixes:
| ``s`` | If the response was mangled, prefixing the request ID ``s`` will result in the same request but its associated response will be the unmangled version. | | ``s`` | If the response was mangled, prefixing the request ID ``s`` will result in the same request but its associated response will be the unmangled version. |
+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
I know it sounds kind of weird, but here are some example commands that I know it sounds kind of unintuitive. Here are some example commands
will hopefully make things clearer. Suppose request 1 had its request that will hopefully make things clearer. Suppose request 1 had its
mangled, and request 2 had its response mangled. request mangled, and request 2 had its response mangled.
- ``vfq 1`` Prints the mangled version of request 1 - ``vfq 1`` Prints the mangled version of request 1
- ``vfq u1`` Prints the unmangled version of request 1 - ``vfq u1`` Prints the unmangled version of request 1
@ -280,7 +285,7 @@ Passing Multiple Request IDs to a Command
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Some arguments can take multiple IDs for an argument. To pass multiple Some arguments can take multiple IDs for an argument. To pass multiple
IDs to a command, separate the IDs with commas (no spaces!). A few IDs to a command, separate the IDs with commas **(no spaces!)**. A few
examples: examples:
- ``viq 1,2,u3`` View information about requests 1, 2, and the - ``viq 1,2,u3`` View information about requests 1, 2, and the
@ -293,13 +298,12 @@ Context
The context is a set of filters that define which requests are The context is a set of filters that define which requests are
considered "active". Only requests in the current context are displayed considered "active". Only requests in the current context are displayed
with ``ls``, and eventually contexts will be how Pappy will manage with ``ls``. By default, the context includes every single request that
requests for group operations. By default, the context includes every passes through the proxy. You can limit down the current context by
single request that passes through the proxy. You can limit down the applying filters. Filters apply rules such as "the response code must
current context by applying filters. Filters apply rules such as "the equal 500" or "the host must contain google.com". Once you apply one or
response code must equal 500" or "the host must contain google.com". more filters, only requests/responses which pass every active filter
Once you apply one or more filters, only requests/responses which pass will be a part of the current context.
every active filter will be a part of the current context.
+-------------------------+---------------------+------------------------------------------------------------------------------------------------------------------------------------------------+ +-------------------------+---------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
| Command | Aliases | Description | | Command | Aliases | Description |
@ -308,24 +312,28 @@ every active filter will be a part of the current context.
+-------------------------+---------------------+------------------------------------------------------------------------------------------------------------------------------------------------+ +-------------------------+---------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
| ``fc`` | filter\_clear, fc | Clears the filters and resets the context to contain all requests and responses. Ignores scope | | ``fc`` | filter\_clear, fc | Clears the filters and resets the context to contain all requests and responses. Ignores scope |
+-------------------------+---------------------+------------------------------------------------------------------------------------------------------------------------------------------------+ +-------------------------+---------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
| ``fu`` | filter\_up, fu | Removes the most recently applied filter |
+-------------------------+---------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
| ``fls`` | filter\_list, fls | Print the filters that make up the current context | | ``fls`` | filter\_list, fls | Print the filters that make up the current context |
+-------------------------+---------------------+------------------------------------------------------------------------------------------------------------------------------------------------+ +-------------------------+---------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
| ``filter_prune`` | filter\_prune | Delete all the requests that aren't in the current context from the data file |
+-------------------------+---------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
Filter Strings Filter Strings
-------------- --------------
Filter strings define a condition that a request/response pair must pass Filter strings define a condition that a request/response pair must pass
to be part of a context. Most filter strings have the following format: to be part of the context. Most filter strings have the following
format:
:: ::
<field> <comparer> <value> <field> <comparer> <value>
Where ``<field>`` is some part of the request/response, ``<comparer>`` Where ``<field>`` is some part of the request/response, ``<comparer>``
is some comparison to ``<value>``. Also **if you prefix a comparer with is some comparison to ``<value>``. For example, if you wanted a filter
'n' it turns it into a negation.** For example, if you wanted a filter that only matches requests to ``target.org``, you could use the
that only matches requests to target.org, you could use the following following filter string:
filter string:
:: ::
@ -335,6 +343,18 @@ filter string:
comparer = "is" comparer = "is"
value = "target.org" value = "target.org"
Also **if you prefix a comparer with 'n' it turns it into a negation.**
Using the previous example, the following will match any request except
for ones where the host contains ``target.org``:
::
host nis target.org
field = "host"
comparer = "nis"
value = "target.org"
For fields that are a list of key/value pairs (headers, get params, post For fields that are a list of key/value pairs (headers, get params, post
params, and cookies) you can use the following format: params, and cookies) you can use the following format:
@ -423,21 +443,34 @@ List of comparers
| lt | lt | A lt B if A < B (A and B must be a number) | | lt | lt | A lt B if A < B (A and B must be a number) |
+--------------+------------------+-----------------------------------------------------------------+ +--------------+------------------+-----------------------------------------------------------------+
Special form filters
~~~~~~~~~~~~~~~~~~~~
A few filters don't conform to the field, comparer, value format. You
can still negate these.
+-----------+------------------+---------------------------------------------------------------------------------------------------------+
| Format | Aliases | Description |
+===========+==================+=========================================================================================================+
| before | before, bf, b4 | Filters out any request that is not before the given request. Filters out any request without a time. |
+-----------+------------------+---------------------------------------------------------------------------------------------------------+
| after | after, af | Filters out any request that is not before the given request. Filters out any request without a time. |
+-----------+------------------+---------------------------------------------------------------------------------------------------------+
Scope Scope
----- -----
Scope is a set of rules to define whether Pappy should mess with a Scope is a set of rules to define whether Pappy should mess with a
request. You define the scope by setting the context to what you want request. You define the scope by setting the context to what you want
the scope to be and running ``scope_save``. The scope is saved in the scope to be and running ``scope_save``. The scope is saved in the
data.db and is automatically restored when using the same project data file and is automatically restored when using the same project
directory. directory.
Any requests which don't match all the filters in the scope will be Any requests which don't match all the filters in the scope will be
passed straight to the browser and will not be caught by the interceptor passed straight to the browser and will not be caught by the interceptor
or recorded in the database. This is useful to make sure you don't or recorded in the data file. This is useful to make sure you don't
accidentally do something like log in to your email through the proxy accidentally do something like log in to your email through the proxy
and have your plaintext username/password stored and accidentally shown and have your plaintext username/password stored.
to your coworkers.
+--------------------+---------------------------+------------------------------------------------------+ +--------------------+---------------------------+------------------------------------------------------+
| Command | Aliases | Description | | Command | Aliases | Description |
@ -458,11 +491,13 @@ Pappy also includes some built in filters that you can apply. These are
things that you may want to filter by but may be too tedius to type out. things that you may want to filter by but may be too tedius to type out.
The ``fbi`` command also supports tab completion. The ``fbi`` command also supports tab completion.
+-----------------+-----------------------------------------+ +-----------------+--------------------------------------------------+
| Filter | Description | | Filter | Description |
+=================+=========================================+ +=================+==================================================+
| ``not_image`` | Matches anything that isn't an image. | | ``not_image`` | Matches anything that isn't an image. |
+-----------------+-----------------------------------------+ +-----------------+--------------------------------------------------+
| ``not_jscss`` | Matches anything that isn't JavaScript or CSS. |
+-----------------+--------------------------------------------------+
+--------------------+-------------------------------+--------------------------------------------------+ +--------------------+-------------------------------+--------------------------------------------------+
| Command | Aliases | Description | | Command | Aliases | Description |
@ -476,12 +511,12 @@ Interceptor
This feature is like Burp's proxy with "Intercept Mode" turned on, This feature is like Burp's proxy with "Intercept Mode" turned on,
except it's not turned on unless you explicitly turn it on. When the except it's not turned on unless you explicitly turn it on. When the
proxy gets a request while in intercept mode, it lets you edit it before proxy gets a request while in intercept mode, it lets you edit it before
it forwards it to the server. In addition, it can stop responses from forwarding it to the server. In addition, it can stop responses from the
the server and let you edit them before they get forwarded to the server and let you edit them before they get forwarded to the browser.
browser. When you run the command, you can pass ``request`` and/or When you run the command, you can pass ``req`` and/or ``rsp`` as
``response`` as arguments to say whether you would like to intercept arguments to say whether you would like to intercept requests and/or
requests and/or responses. Only in-scope requests/responses will be responses. Only in-scope requests/responses will be intercepted (see
intercepted (see Scope section). Scope section).
The interceptor will use your EDITOR variable to decide which editor to The interceptor will use your EDITOR variable to decide which editor to
edit the request/response with. If no editor variable is set, it will edit the request/response with. If no editor variable is set, it will
@ -512,6 +547,8 @@ To forward a request, edit it, save the file, then quit.
Be totally useless: Be totally useless:
> ic > ic
To drop a request, delete everything, save and quit.
Repeater Repeater
-------- --------
@ -520,12 +557,11 @@ and Pappy will open vim in a split window with your request on the left
and the original response on the right. You can make changes to the and the original response on the right. You can make changes to the
request and then run ":RepeaterSubmitBuffer" to submit the modified request and then run ":RepeaterSubmitBuffer" to submit the modified
request. The response will be displayed on the right. This command is request. The response will be displayed on the right. This command is
bound to ``<leader>f`` by default, but you can rebind it in your vimrc bound to ``<leader>f`` by default, but you can bind it to something else
(I think, dunno if vim will complain if it's undefined). This command too in your vimrc (I think, dunno if vim will complain if the function
will submit whatever buffer your cursor is in, so make sure it's in the undefined which it will be for regular files). This command will submit
request buffer. whatever buffer your cursor is in, so make sure it's in the request
buffer.
To drop a request, delete everything, save and quit (``ggdG:wq``).
When you're done with repeater, run ":qa!" to avoid having to save When you're done with repeater, run ":qa!" to avoid having to save
changes to nonexistent files. changes to nonexistent files.
@ -588,19 +624,19 @@ quotes around it.
$ pappy $ pappy
Proxy is listening on port 8000 Proxy is listening on port 8000
itsPappyTime> lma pappy> lma
Loaded "<Macro Test Macro (tm/test)>" Loaded "<Macro Test Macro (tm/test)>"
Loaded "<Macro Macro 6494496 (testgen)>" Loaded "<Macro Macro 6494496 (testgen)>"
Loaded "<Macro Print Macro (print)>" Loaded "<Macro Print Macro (print)>"
Loaded "<Macro Hack the NSA (htnsa/hackthensa)>" Loaded "<Macro Hack the NSA (htnsa/hackthensa)>"
Loaded "<Macro Macro 62449408 (blank)>" Loaded "<Macro Macro 62449408 (blank)>"
itsPappyTime> rma print pappy> rma print
Hello, Pappy! Hello, Pappy!
itsPappyTime> rma print NSA pappy> rma print NSA
Hello, NSA! Hello, NSA!
itsPappyTime> rma print Idiot Slayer pappy> rma print Idiot Slayer
Hello, Idiot! Hello, Idiot!
itsPappyTime> rma print "Idiot Slayer" pappy> rma print "Idiot Slayer"
Hello, Idiot Slayer! Hello, Idiot Slayer!
You'll need to run ``lma`` every time you make a change to the macro in You'll need to run ``lma`` every time you make a change to the macro in
@ -617,16 +653,16 @@ with the same information as requests you've already made. For example:
$ pappy $ pappy
Proxy is listening on port 8000 Proxy is listening on port 8000
itsPappyTime> ls pappy> ls
ID Verb Host Path S-Code Req Len Rsp Len Time Mngl ID Verb Host Path S-Code Req Len Rsp Len Time Mngl
5 GET vitaly.sexy /esr1.jpg 200 OK 0 17653 -- -- 5 GET vitaly.sexy /esr1.jpg 200 OK 0 17653 -- --
4 GET vitaly.sexy /netscape.gif 200 OK 0 1135 -- -- 4 GET vitaly.sexy /netscape.gif 200 OK 0 1135 -- --
3 GET vitaly.sexy /construction.gif 200 OK 0 28366 -- -- 3 GET vitaly.sexy /construction.gif 200 OK 0 28366 -- --
2 GET vitaly.sexy /vitaly2.jpg 200 OK 0 2034003 -- -- 2 GET vitaly.sexy /vitaly2.jpg 200 OK 0 2034003 -- --
1 GET vitaly.sexy / 200 OK 0 1201 -- -- 1 GET vitaly.sexy / 200 OK 0 1201 -- --
itsPappyTime> gma sexy 1 pappy> gma sexy 1
Wrote script to macro_sexy.py Wrote script to macro_sexy.py
itsPappyTime> quit pappy> quit
$ cat macro_sexy.py $ cat macro_sexy.py
from pappyproxy.http import Request, get_request, post_request from pappyproxy.http import Request, get_request, post_request
@ -658,11 +694,10 @@ with the same information as requests you've already made. For example:
# req.save() # save the request to the data file # req.save() # save the request to the data file
# or copy req0 into a loop and use string substitution to automate requests # or copy req0 into a loop and use string substitution to automate requests
pass pass
$
If you enter in a value for ``SHORT_NAME``, you can use it as a shortcut If you enter in a value for ``SHORT_NAME``, you can use it as a shortcut
to run that macro. So if in a macro you set ``SHORT_NAME='tm'`` you can to run that macro. So if in a macro you set ``SHORT_NAME='tm'`` you can
run it by running ``itsPappyTime> rma tm``. run it by running ``pappy> rma tm``.
+--------------------------+-------------------------------+-------------------------------------------------------------------------------------------------------------------------------------+ +--------------------------+-------------------------------+-------------------------------------------------------------------------------------------------------------------------------------+
| Command | Aliases | Description | | Command | Aliases | Description |
@ -692,8 +727,8 @@ Dict-like objects are represented with a custom class called a
so just interact with it like a dict and don't be surprised if it's so just interact with it like a dict and don't be surprised if it's
missing some methods you would expect a dict to have. missing some methods you would expect a dict to have.
Here is a quick (non-comprehensive) list of attributes that you can use Here is a quick list of attributes that you can use with ``Request``
with ``Request`` objects: objects:
+-----------------+-------------+------------------+-----------------------------------------------------------------------------------------------------------------+ +-----------------+-------------+------------------+-----------------------------------------------------------------------------------------------------------------+
| Attribute | Settable? | Data Type | Description | | Attribute | Settable? | Data Type | Description |
@ -755,8 +790,8 @@ Request methods:
| save() | Save the request, its unmangled version, its associated response, and the unmangled version of the response to the database | | save() | Save the request, its unmangled version, its associated response, and the unmangled version of the response to the database |
+------------+-------------------------------------------------------------------------------------------------------------------------------+ +------------+-------------------------------------------------------------------------------------------------------------------------------+
And here is a quick (non-comprehensive) list of attributes that you can And here is a quick list of attributes that you can use with
use with ``Response`` objects: ``Response`` objects:
+------------------+-------------+------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +------------------+-------------+------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Attribute | Settable? | Data Type | Description | | Attribute | Settable? | Data Type | Description |
@ -791,8 +826,27 @@ can fix it.
Useful Functions Useful Functions
~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~
There are also a few functions which could be useful for making There are also a few functions which could be useful for creating
requests. requests in macros. It's worth pointing out that ``request_by_id`` is
useful for passing request objects as arguments. For example, here is a
macro that lets you resubmit a request with the Google Bot user agent:
::
## macro_googlebot.py
from pappyproxy.http import Request, get_request, post_request, request_by_id
from pappyproxy.context import set_tag
from pappyproxy.iter import *
MACRO_NAME = 'Submit as Google'
SHORT_NAME = ''
def run_macro(args):
req = request_by_id(args[0])
req.headers['User-Agent'] = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
req.submit()
req.save()
+-------------------------------------------------------+-------------------------------------------------------------------------------------------------------------+ +-------------------------------------------------------+-------------------------------------------------------------------------------------------------------------+
| Function | Description | | Function | Description |
@ -801,6 +855,8 @@ requests.
+-------------------------------------------------------+-------------------------------------------------------------------------------------------------------------+ +-------------------------------------------------------+-------------------------------------------------------------------------------------------------------------+
| post\_request(url, post\_params={}, url\_params={}) | Returns a Request object that contains a POST request to the given url with the given url and post params | | post\_request(url, post\_params={}, url\_params={}) | Returns a Request object that contains a POST request to the given url with the given url and post params |
+-------------------------------------------------------+-------------------------------------------------------------------------------------------------------------+ +-------------------------------------------------------+-------------------------------------------------------------------------------------------------------------+
| request\_by\_id(reqid) | Get a request object from its id. |
+-------------------------------------------------------+-------------------------------------------------------------------------------------------------------------+
Intercepting Macros Intercepting Macros
------------------- -------------------
@ -824,9 +880,10 @@ Note, that due to twisted funkyness, *you cannot save requests from
intercepting macros*. Technically you **can**, but to do that you'll intercepting macros*. Technically you **can**, but to do that you'll
have to define ``async_mangle_request`` (or response) instead of have to define ``async_mangle_request`` (or response) instead of
``mangle_request`` (or response) then use ``Request.async_deep_save`` ``mangle_request`` (or response) then use ``Request.async_deep_save``
which returns a deferred, then return a deferred from which generates a deferred, then generate a deferred from
``async_mangle_requests`` (inline callbacks work too). If you've never ``async_mangle_requests`` (inline callbacks work too). If you've never
used twisted before, please don't try. Twisted is hard. used twisted before, please don't try. Twisted is hard. Plus the mangled
request will be saved before it is submitted anyways.
Confusing? Here are some example intercepting macros: Confusing? Here are some example intercepting macros:
@ -862,9 +919,7 @@ Confusing? Here are some example intercepting macros:
## int_adminplz.py ## int_adminplz.py
from pappyproxy.http import ResponseCookie
from base64 import base64encode as b64e from base64 import base64encode as b64e
import string
MACRO_NAME = 'Admin Session' MACRO_NAME = 'Admin Session'
@ -873,40 +928,68 @@ Confusing? Here are some example intercepting macros:
r.headers['Authorization'] = 'Basic %s' % b64e('Admin:Password123') r.headers['Authorization'] = 'Basic %s' % b64e('Admin:Password123')
return r return r
In addition, you can use an ``init(args)`` function to get arguments
from the command line. If no arguments are passed, args will be an empty
list. Here is an example macro that does a search and replace:
::
## int_replace.py
MACRO_NAME = 'Find and Replace'
SHORT_NAME = ''
runargs = []
def init(args):
global runargs
runargs = args
def mangle_request(request):
global runargs
if len(runargs) < 2:
return request
request.body = request.body.replace(runargs[0], runargs[1])
return request
def mangle_response(request):
global runargs
if len(runargs) < 2:
return request.response
request.response.body = request.response.body.replace(runargs[0], runargs[1])
return request.response
You can use this macro to do any search and replace that you want. For
example, if you wanted to replace "Google" with "Skynet", you can run
the macro like this:
::
pappy> lma
Loaded "<InterceptingMacro Find and Replace (replace)>"
pappy> rim replace Google Skynet
"Find and Replace" started
pappy>
Now every site that you visit will be a little bit more accurate.
Enabling/Disabling Intercepting Macros Enabling/Disabling Intercepting Macros
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You can use the following commands to start/stop intercepting macros You can use the following commands to start/stop intercepting macros
+------------------------+------------------------------------+----------------------------------------------------------------------------------------------------------------------+ +-------------------------------+------------------------------------+--------------------------------------------------------------------------------------------------------------------------------+
| Command | Aliases | Description | | Command | Aliases | Description |
+========================+====================================+======================================================================================================================+ +===============================+====================================+================================================================================================================================+
| ``lma [dir]`` | ``load_macros``, ``lma`` | Load macros from a directory. If ``dir`` is not given, use the current directory (the project directory) | | ``lma [dir]`` | ``load_macros``, ``lma`` | Load macros from a directory. If ``dir`` is not given, use the current directory (the project directory) |
+------------------------+------------------------------------+----------------------------------------------------------------------------------------------------------------------+ +-------------------------------+------------------------------------+--------------------------------------------------------------------------------------------------------------------------------+
| ``rim <macro name>`` | ``run_int_macro``, ``rim`` | Run an intercepting macro. Similarly to normal macros you can use the name, short name, or file name of the macro. | | ``rim <macro name>`` | ``run_int_macro``, ``rim`` | Run an intercepting macro. Similarly to normal macros you can use the name, short name, or file name of the macro. |
+------------------------+------------------------------------+----------------------------------------------------------------------------------------------------------------------+ +-------------------------------+------------------------------------+--------------------------------------------------------------------------------------------------------------------------------+
| ``sim <macro name>`` | ``stop_int_macro``, ``sim`` | Stop an intercepting macro. | | ``sim <macro name> [args]`` | ``stop_int_macro``, ``sim`` | Stop an intercepting macro. If arguments are given, they will be passed to the macro's ``init(args)`` function if it exists. |
+------------------------+------------------------------------+----------------------------------------------------------------------------------------------------------------------+ +-------------------------------+------------------------------------+--------------------------------------------------------------------------------------------------------------------------------+
| ``lim`` | ``list_int_macros``, ``lim`` | List all enabled/disabled intercepting macros | | ``lim`` | ``list_int_macros``, ``lsim`` | List all enabled/disabled intercepting macros |
+------------------------+------------------------------------+----------------------------------------------------------------------------------------------------------------------+ +-------------------------------+------------------------------------+--------------------------------------------------------------------------------------------------------------------------------+
| ``gima <name>`` | ``generate_int_macro``, ``gima`` | Generate an intercepting macro with the given name. | | ``gima <name>`` | ``generate_int_macro``, ``gima`` | Generate an intercepting macro with the given name. |
+------------------------+------------------------------------+----------------------------------------------------------------------------------------------------------------------+ +-------------------------------+------------------------------------+--------------------------------------------------------------------------------------------------------------------------------+
Additional Commands
-------------------
This is a list of other random stuff you can do that isn't categorized
under anything else. These are mostly commands that I found that I
needed while doing a test and just added. They likely don't do a ton of
error checking and are likely not super full-featured.
+----------------------------------------+---------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------+
| Command | Aliases | Description |
+========================================+=====================+=======================================================================================================================================================+
| ``dump_response <reqid> [filename]`` | ``dump_response`` | Dumps the data from the response to the given filename (useful for images, .swf, etc). If no filename is given, it uses the name given in the path. |
+----------------------------------------+---------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------+
| ``export <req|rsp> <reqid>`` | ``export`` | Writes either the full request or response to a file in the current directory. |
+----------------------------------------+---------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------+
Logging Logging
------- -------
@ -925,3 +1008,53 @@ every start though!)
+=======================+===============================================================================================================================================================================================================================+ +=======================+===============================================================================================================================================================================================================================+
| ``log [verbosity]`` | View the log at the given verbosity. Default verbosity is 1 which just shows connections being made/lost and some other info, verbosity 3 shows full requests/responses as they pass through and are processed by the proxy | | ``log [verbosity]`` | View the log at the given verbosity. Default verbosity is 1 which just shows connections being made/lost and some other info, verbosity 3 shows full requests/responses as they pass through and are processed by the proxy |
+-----------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +-----------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
Additional Commands and Features
--------------------------------
This is a list of other random stuff you can do that isn't categorized
under anything else. These are mostly commands that I found that I
needed while doing a test and just added. They likely don't do a ton of
error checking.
+----------------------------------------+---------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------+
| Command | Aliases | Description |
+========================================+=====================+=======================================================================================================================================================+
| ``dump_response <reqid> [filename]`` | ``dump_response`` | Dumps the data from the response to the given filename (useful for images, .swf, etc). If no filename is given, it uses the name given in the path. |
+----------------------------------------+---------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------+
| ``export <req|rsp> <reqid>`` | ``export`` | Writes either the full request or response to a file in the current directory. |
+----------------------------------------+---------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------+
Response streaming
~~~~~~~~~~~~~~~~~~
If you don't have any intercepting macros running, Pappy will forward
data to the browser as it gets it. However, if you're trying to mangle
messages/responses, Pappy will need to download the entire message
first.
FAQ
---
Why does my request have an id of ``--``?!?!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You can't do anything with a request/response until it is decoded and
saved to disk. In between the time when a request is decoded and when
it's saved to disk, it will have an ID of ``--``. So just wait a little
bit and it will get an ID you can use.
Changelog
---------
The boring part of the readme
- 0.1.2
- Refactor almost every part of proxy
- Basic framework for plugins
- Bugfixes probably
- Create changelog
- 0.1.1
- Start using sane versioning system
- No idea what I added

@ -0,0 +1,450 @@
Writing Plugins for the Pappy Proxy
***********************************
.. contents:: Table of Contents
:local:
Introduction
============
Are macros not powerful enough? Want to make something reusable? Want to add console commands?! Then you might want to write yourself a plugin. Some quick highlights about plugins:
* Python scripts stored in ``~/.pappy/plugins``
* Can add console commands
* For actions which aren't specific to one project
* Harder to write than macros
Since macros can also use the plugin API, plugins aren't any more powerful than macros (besides adding console commands). However, if you find yourself copying a useful macro to more than one project, it may be worth it to just bind it to some commands, put the script in one place, and stop worrying about copying it around. Plus then you can put it on GitHub for some sweet sweet nerd cred.
Should I Write a Plugin or a Macro?
-----------------------------------
A lot of the time, you can get away with writing a macro. However, you may consider writing a plugin if:
* You find yourself copying one macro to multiple projects
* You want to write a general tool that can be applied to any website
* You need to maintain state during the Pappy session
My guess is that if you need one quick thing for a project, you're better off writing a macro first and seeing if you end up using it in future projects. Then if you find yourself needing it a lot, write a plugin for it. You may also consider keeping a ``mine.py`` plugin where you can write out commands that you use regularly but may not be worth creating a dedicated plugin for.
Plugins Get Merged
------------------
If you write a useful plugin, as long as it isn't uber niche, I'll try and merge it into the core project.
Creating a Plugin
=================
Whenever you make a macro, you'll have to bind some functions to some console commands. To do this, you'll have to define a ``load_cmds`` function in your plugin. This function should take one argument. When the plugin is loaded, this function will be called and the console object will be passed to this function. You can then use ``set_cmds`` and ``add_aliases`` to bind functions to console commands.
Writing a Hello World Plugin
----------------------------
It's probably easiest to explain how to write a plugin by writing one. Here is a simple plugin that defines a ``hello`` command and gives an alias ``hlo`` (we'll go over all the parts in a second)::
## hello.py
def hello_world(line):
print "Hello, world!"
###############
## Plugin hooks
def load_cmds(cmd):
cmd.set_cmds({
'hello': (hello_world, None),
})
cmd.add_aliases([
('hello', 'hlo'),
])
Save this as ``~/.pappy/plugins/hello.py`` and run Pappy. You should have a new ``hello`` command that prints your message::
$ cp hello.py ~/.pappy/plugins/
$ pappy -l
Temporary datafile is /tmp/tmp1Myw6q
Proxy is listening on port 8000
pappy> hello
Hello, world!
pappy> hlo
Hello, world!
pappy>
Awesome! So let's go over the code. Here are the important parts of the code:
* We define a function that we want to call
* We define ``load_cmds(cmd)`` to be called when our plugin is loaded to bind our function to a command
* We use ``cmd.set_cmds`` to set all our commands
* We use ``cmd.add_aliases`` to add aliases for commands
Now let's go over it in detail
Passing Arguments to Your Function
----------------------------------
Each command gets bound to one function which takes one argument. That argument is all the text that was entered after the name of the command in the console. For example if we run ``hello foo bar``, in our function line would be "foo bar". **I suggest using shlex.split(line) to parse multiple arguments**. So let's update our script to take some arguments::
## hello.py
import shlex
def hello_world(line):
if line:
args = shlex.split(line)
print 'Hello, %s!' % (', '.join(args))
else:
print "Hello, world!"
###############
## Plugin hooks
def load_cmds(cmd):
cmd.set_cmds({
'hello': (hello_world, None),
})
cmd.add_aliases([
('hello', 'hlo'),
])
Save your changes and restart pappy to reload the plugin::
$ pappy -l
Temporary datafile is /tmp/tmpBOXyJ3
Proxy is listening on port 8000
pappy> hello
Hello, world!
pappy> hello foo bar baz
Hello, foo, bar, baz!
pappy> hello foo bar "baz lihtyur"
Hello, foo, bar, baz lihtyur!
pappy>
Adding More Aliases
-------------------
So now let's add some more aliases to our command. If we want to add a new alias, we just add another tuple to the list passed into ``cmd.add_aliases``. The first element is the real name of the command (what you set with ``set_cmds``) and the second value is the alias you want to type. So let's make it so we can just type ``ho`` to say hello::
## hello.py
import shlex
def hello_world(line):
if line:
args = shlex.split(line)
print 'Hello, %s!' % (', '.join(args))
else:
print "Hello, world!"
###############
## Plugin hooks
def load_cmds(cmd):
cmd.set_cmds({
'hello': (hello_world, None),
})
cmd.add_aliases([
('hello', 'hlo'),
('hello', 'ho'),
])
.. note::
You must use the actual name of the command that you used in ``set_cmds``. You can't "chain" alieases. As a result, in our example we couldn't add the alias ``('hlo', 'ho')`` to add ``ho`` as our alias.
Then reload the plugin::
$ pappy -l
Temporary datafile is /tmp/tmpBOXyJ3
Proxy is listening on port 8000
pappy> ho
Hello, world!
pappy> ho foo bar baz
Hello, foo, bar, baz!
pappy> ho foo bar "baz lihtyur"
Hello, foo, bar, baz lihtyur!
pappy>
Adding Another Command
----------------------
So now let's add a ``robe_and_wizard_hat`` command. To do this, we will define another function, then add another entry in the dict that is passed to ``set_cmds``. The second value in the tuple is the autocomplete function, but we'll get to that later. For now, just put in ``None`` to say we don't have one. We will also add a ``wh`` alias to it::
$ pappy -l
Temporary datafile is /tmp/tmpyl2cEZ
Proxy is listening on port 8000
pappy> wh
I put on my robe and wizard hat
pappy>
Adding Autocompletion
---------------------
You can also define a function to handle autocompletion for your command. In order to do this, you define a function that takes 4 arguments: ``text``, ``line``, ``begidx``, and ``endidx``. From the `Cmd docs <https://docs.python.org/2/library/cmd.html>`_, this is what the arguments mean:
``text`` is the string prefix we are attempting to match: all returned matches must begin with it. ``line`` is the current input line with leading whitespace removed, ``begidx`` and ``endidx`` are the beginning and ending indexes of the prefix text, which could be used to provide different completion depending upon which position the argument is in.
Let's let the user to autocomplete some names in our plugin::
import shlex
_AUTOCOMPLETE_NAMES = ['alice', 'allie', 'sarah', 'mallory', 'slagathor']
def hello_world(line):
if line:
args = shlex.split(line)
print 'Hello, %s!' % (', '.join(args))
else:
print "Hello, world!"
def put_on_rope_and_wizard_hat(line):
if line:
print '%s puts on their robe and wizard hat' % line
else:
print 'I put on my robe and wizard hat'
def complete_hello_world(text, line, begidx, endidx):
return [n for n in _AUTOCOMPLETE_NAMES if n.startswith(text)]
###############
## Plugin hooks
def load_cmds(cmd):
cmd.set_cmds({
'hello': (hello_world, complete_hello_world),
'wizard_hat': (put_on_rope_and_wizard_hat, None),
})
cmd.add_aliases([
('hello', 'hlo'),
('wizard_hat', 'wh'),
])
Then restart and run::
$ pappy -l
Temporary datafile is /tmp/tmp3J97rE
Proxy is listening on port 8000
pappy> hello
alice allie mallory sarah slagathor
pappy> hello allie
Hello, allie!
pappy>
You can't see it, but I hit tab twice after typing hello to get the completions to appear.
Adding Help
-----------
Now let's say we want to add some help to the command so that when the user runs ``help hello`` they get something useful. To do that, just add a docstring to your function::
import shlex
_AUTOCOMPLETE_NAMES = ['alice', 'allie', 'sarah', 'mallory', 'slagathor']
def hello_world(line):
"""
Say hello to the world. Usage: hello [name]
"""
if line:
args = shlex.split(line)
print 'Hello, %s!' % (', '.join(args))
else:
print "Hello, world!"
def put_on_rope_and_wizard_hat(line):
if line:
print '%s puts on their robe and wizard hat' % line
else:
print 'I put on my robe and wizard hat'
def complete_hello_world(text, line, begidx, endidx):
return [n for n in _AUTOCOMPLETE_NAMES if n.startswith(text)]
###############
## Plugin hooks
def load_cmds(cmd):
cmd.set_cmds({
'hello': (hello_world, complete_hello_world),
'wizard_hat': (put_on_rope_and_wizard_hat, None),
})
cmd.add_aliases([
('hello', 'hlo'),
('wizard_hat', 'wh'),
])
Using defer.inlineCallbacks With a Command
------------------------------------------
.. note::
If you are using inlineCallbacks, you can't use any functions which are blocking versions of async functions. For example, you cannot use :func:`pappyproxy.http.Request.save` and must instead use :func:`pappyproxy.http.Request.async_deep_save`.
.. note::
This tutorial won't tell you how to use inlineCallbacks in general. Type "twisted inline callbacks" into google to figure out what they are. This is mainly just a reminder to use the ``crochet`` wrapper for console commands and warning you that some functions may return deferreds that you may have to deal with.
Since you're writing a plugin, you'll probably be using functions which return a deferred. And to keep things readable, you'll want to use the ``defer.inlineCallbacks`` function wrapper. Unfortunately, you can't bind async functions to commands. Luckily, there's a library called `crochet <https://pypi.python.org/pypi/crochet>`_ which lets you add another wrapper to the function that lets it be used like a blocking function. Rather than talking about it, let's write a plugin to call :func:`pappyproxy.console.load_reqlist` to print out some requests' hosts. Let's start by pretending it's a normal function::
import shlex
from pappyproxy.console import load_reqlist
def print_hosts(line):
args = shlex.split(line)
reqs = load_reqlist(args[0]) # It's supposed to return a list of requests, right?
for r in reqs:
print 'The host for request %s is: %s' % (r.reqid, r.host)
###############
## Plugin hooks
def load_cmds(cmd):
cmd.set_cmds({
'print_hosts': (print_hosts, None),
})
cmd.add_aliases([
])
And we run it::
pappy> print_hosts 1
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/cmd2.py", line 788, in onecmd_plus_hooks
stop = self.onecmd(statement)
File "/usr/local/lib/python2.7/dist-packages/cmd2.py", line 871, in onecmd
stop = func(statement)
File "/home/supahacker/pappy/pappyproxy/console.py", line 15, in catch
func(*args, **kwargs)
File "/home/supahacker/.pappy/plugins/hosts.py", line 7, in print_hosts
for r in reqs:
TypeError: iteration over non-sequence
iteration over non-sequence
pappy>
Iteration over a non-sequence? what? Well, :func:`pappyproxy.console.load_reqlist` doesn't actually return a list of requests. It returns a deferred which returns a list of requests. I'm not going into the details (look up some stuff on using inline callbacks with Twisted if you want more info), but the way to fix it is to slap an ``inlineCallbacks`` wrapper on the function and ``yield`` the result of the function. Now it looks like this::
import shlex
from pappyproxy.console import load_reqlist
from twisted.internet import defer
@defer.inlineCallbacks
def print_hosts(line):
args = shlex.split(line)
reqs = yield load_reqlist(args[0])
for r in reqs:
print 'The host for request %s is: %s' % (r.reqid, r.host)
###############
## Plugin hooks
def load_cmds(cmd):
cmd.set_cmds({
'print_hosts': (print_hosts, None),
})
cmd.add_aliases([
])
However, the console assumes that any functions it calls will be blocking. As a result, we need to add the ``crochet.wait_for`` wrapper::
import shlex
import crochet
from pappyproxy.console import load_reqlist
from twisted.internet import defer
@crochet.wait_for(timeout=None)
@defer.inlineCallbacks
def print_hosts(line):
args = shlex.split(line)
reqs = yield load_reqlist(args[0])
for r in reqs:
print 'The host for request %s is: %s' % (r.reqid, r.host)
###############
## Plugin hooks
def load_cmds(cmd):
cmd.set_cmds({
'print_hosts': (print_hosts, None),
})
cmd.add_aliases([
])
And now we're good! If you run it without the crochet wrapper, it may still work. However, since the console assumes any functions it calls will be blocking, not having the wrapper could lead to weird errors.
Plugin API
==========
There are also some useful functions that you can use to interact with the request history and the context. It's somewhat limited for now, but for now you can at least look through history and create/send new requests. Hopefully the API will expand as people find themselves wanting to do new things. That means **if you're writing a plugin, let me know and I'll add any APIs you need**. For now at least, plugins will let you maintain state over the course of the session and let you define commands.
The best way to learn what you can do is to go through the :ref:`pappyproxy-package` and look at all the available functions.
API Functions
-------------
See :mod:`pappyproxy.plugin` for docs on all the functions you can use. You can also use any of the functions provided for writing macros (and vice-versa).
Storing Data on Disk
--------------------
Unfortunately, you're on your own if you want to store plugin specific stuff on disk. It's also important that you store any data that is specific to a project in the same directory as the data file. This is to make sure that if you encrypt your project folder, you can be sure that no sensitive data about the test can be found anywhere else. The only time you should store anything outside of the current directory is to store global plugin settings, and even then it would probably be better to parse options from ``config.config_dict``. Pappy doesn't even store data outside of the project directory except for its CA certificates.
However, if your plugin is a special snowflake that needs to store unencrypted, global settings, you should create a directory for your plugin in ``{config.DATA_DIR}/plugindata`` and put your files there. But again, avoid this if you can.
.. note::
Any project-specific data (ie anything that contains info about requests) should be stored in the project directory unless you have a really really good reason. This is because it must be possible to secure any sensitive data by encrypting the project folder and storing data outside of the directory will add complications.
.. warning::
Do not modify the data file schema. There is a good chance the schema will break in future versions of Pappy.
Storing Custom Request Metadata
-------------------------------
:class:`pappyproxy.http.Request` objects have a ``plugin_data`` attribute. It is a dictionary that is intended to be used by plugins to give the request custom metadata. If you want to store metadata about a request, it is suggested that you add a key to this dictionary and store any metadata you want under that key. You can use :func:`pappyproxy.http.Request.get_plugin_dict` to get a dictionary for a specific name. It will create an entry for that name if it doesn't exist. I also suggest defining a function plugin-wide for getting the plugin's data dict from a specific request. Since dictionaries are always passed by reference, any modifications you make to the returned dict will be applied to the request as well.
.. note::
You will need to save the request using something like :func:`pappyproxy.http.Request.save` or :func:`pappyproxy.http.Request.async_deep_save` in order to store the changes in the data file.
Here is an example plugin for storing the user-agent (if it exists) in the ``plugin_data`` dict of a request under the key ``agent``::
import crochet
import shlex
from twisted.internet import defer
from pappyproxy.console import load_reqlist
from pappyproxy.plugin import main_context
from pappyproxy.util import PappyException
DATA_KEY = 'agent'
def get_data(r):
return r.get_plugin_dict(DATA_KEY)
@crochet.wait_for(timeout=None)
@defer.inlineCallbacks
def update_agent_metadata(line):
for r in main_context().active_requests:
if 'user-agent' in r.headers:
get_data(r)['agent'] = r.headers['user-agent']
yield r.async_deep_save()
@crochet.wait_for(timeout=None)
@defer.inlineCallbacks
def view_agent(line):
args = shlex.split(line)
reqs = yield load_reqlist(args[0])
for r in reqs:
if 'agent' in get_data(r):
print 'The user agent for %s is "%s"' % (r.reqid, get_data(r)['agent'])
else:
print 'Request %s has no user agent data' % r.reqid
###############
## Plugin hooks
def load_cmds(cmd):
cmd.set_cmds({
'agent_update': (update_agent_metadata, None),
'view_agent': (view_agent, None),
})
cmd.add_aliases([
])
Useful Functions
----------------
* Load a request by id: :func:`pappyproxy.http.Request.load_request`
* Create a filter from a filter string: :func:`pappyproxy.context.Filter.from_filter_string`
Built In Plugins As Examples
============================
Built In Plugins
----------------
All the commands in Pappy are implemented as plugins. I have done what I could to avoid using internal functions as much as I could, but there are still some instances where I had to implement an internal function in order to get the functions I needed. However, you can still look them over to see how things are structured and see some examples of semi-complicated plugins.
Interceptor and Repeater
------------------------
Pappy's interceptor and repeater are fully implemented as a plugin. It defines an intercepting macro that handles saving then editing messages and commands that read those files and edit them. It relies on Twisted to switch between the macro handling the request and the command modifying it, so if you want to make something similar, you'll have to learn how to use deferreds.

@ -1,42 +1,40 @@
pappyproxy package pappyproxy package
================== ==================
.. Subpackages
Subpackages -----------
-----------
.. toctree:: .. toctree::
pappyproxy.plugins
pappyproxy.schema pappyproxy.schema
pappyproxy.templates pappyproxy.templates
pappyproxy.tests pappyproxy.tests
pappyproxy.vim_repeater pappyproxy.vim_repeater
Submodules Submodules
---------- ----------
.. pappyproxy.comm module
pappyproxy.comm module ----------------------
----------------------
.. automodule:: pappyproxy.comm .. automodule:: pappyproxy.comm
:members: :members:
:noindex:
:undoc-members: :undoc-members:
:show-inheritance: :show-inheritance:
pappyproxy.config module pappyproxy.config module
------------------------ ------------------------
.. automodule:: pappyproxy.config .. automodule:: pappyproxy.config
:members: :members:
:undoc-members: :undoc-members:
:show-inheritance: :show-inheritance:
pappyproxy.console module pappyproxy.console module
------------------------- -------------------------
.. automodule:: pappyproxy.console .. automodule:: pappyproxy.console
:members: :members:
:undoc-members: :undoc-members:
:show-inheritance: :show-inheritance:
@ -65,69 +63,67 @@ pappyproxy.iter module
:undoc-members: :undoc-members:
:show-inheritance: :show-inheritance:
.. pappyproxy.macros module
pappyproxy.macros module ------------------------
------------------------
.. automodule:: pappyproxy.macros .. automodule:: pappyproxy.macros
:members: :members:
:undoc-members: :undoc-members:
:show-inheritance: :show-inheritance:
pappyproxy.mangle module pappyproxy.pappy module
------------------------ -----------------------
.. automodule:: pappyproxy.mangle .. automodule:: pappyproxy.pappy
:members: :members:
:undoc-members: :undoc-members:
:show-inheritance: :show-inheritance:
pappyproxy.pappy module pappyproxy.plugin module
----------------------- ------------------------
.. automodule:: pappyproxy.pappy .. automodule:: pappyproxy.plugin
:members: :members:
:undoc-members: :undoc-members:
:show-inheritance: :show-inheritance:
pappyproxy.proxy module pappyproxy.proxy module
----------------------- -----------------------
.. automodule:: pappyproxy.proxy .. automodule:: pappyproxy.proxy
:members: :members:
:undoc-members: :undoc-members:
:show-inheritance: :show-inheritance:
pappyproxy.repeater module pappyproxy.repeater module
-------------------------- --------------------------
.. automodule:: pappyproxy.repeater .. automodule:: pappyproxy.repeater
:members: :members:
:undoc-members: :undoc-members:
:show-inheritance: :show-inheritance:
pappyproxy.session module pappyproxy.session module
------------------------- -------------------------
.. automodule:: pappyproxy.session .. automodule:: pappyproxy.session
:members: :members:
:undoc-members: :undoc-members:
:show-inheritance: :show-inheritance:
pappyproxy.util module pappyproxy.util module
---------------------- ----------------------
.. automodule:: pappyproxy.util .. automodule:: pappyproxy.util
:members: :members:
:undoc-members: :undoc-members:
:show-inheritance: :show-inheritance:
.. Module contents
Module contents ---------------
---------------
.. automodule:: pappyproxy .. automodule:: pappyproxy
:members: :members:
:undoc-members: :undoc-members:
:show-inheritance: :show-inheritance:

@ -28,6 +28,22 @@ pappyproxy.schema.schema_3 module
:undoc-members: :undoc-members:
:show-inheritance: :show-inheritance:
pappyproxy.schema.schema_4 module
---------------------------------
.. automodule:: pappyproxy.schema.schema_4
:members:
:undoc-members:
:show-inheritance:
pappyproxy.schema.schema_5 module
---------------------------------
.. automodule:: pappyproxy.schema.schema_5
:members:
:undoc-members:
:show-inheritance:
pappyproxy.schema.update module pappyproxy.schema.update module
------------------------------- -------------------------------

@ -1,12 +1,8 @@
The Pappy Proxy Tutorial The Pappy Proxy Tutorial
************************ ************************
Table of Contents .. contents:: Table of Contents
================= :local:
.. toctree::
tutorial
Getting Set Up Getting Set Up
============== ==============
@ -40,9 +36,9 @@ That was easy! Make a project directory anywhere for Natas and fire up Pappy.::
$ cd natas $ cd natas
Copying default config to ./config.json Copying default config to ./config.json
Proxy is listening on port 8000 Proxy is listening on port 8000
itsPappyTime> pappy>
If you look at what's in the file, you'll notice that there's a ``data.db`` file and a ``config.json`` file. If you look at what's in the directory, you'll notice that there's a ``data.db`` file and a ``config.json`` file.
* ``data.db`` is a SQLite file that stores all the (in-scope) requests that pass through the proxy * ``data.db`` is a SQLite file that stores all the (in-scope) requests that pass through the proxy
* ``config.json`` stores settings for the proxy * ``config.json`` stores settings for the proxy
@ -55,13 +51,13 @@ In order to intercept HTTPS requests, you'll need to add a CA cert to your brows
To generate certificates, you'll use the ``gencerts`` command. This will generate certificates in Pappy's directory. By default, all projects will use the certs in this directory, so you should only have to generate/install the certificates once.:: To generate certificates, you'll use the ``gencerts`` command. This will generate certificates in Pappy's directory. By default, all projects will use the certs in this directory, so you should only have to generate/install the certificates once.::
itsPappyTime> gencerts pappy> gencerts
This will overwrite any existing certs in /home/anonymouse/pappy/pappyproxy/certs. Are you sure? This will overwrite any existing certs in /home/anonymouse/pappy/pappyproxy/certs. Are you sure?
(y/N) y (y/N) y
Generating certs to /home/anonymouse/pappy/pappyproxy/certs Generating certs to /home/anonymouse/pappy/pappyproxy/certs
Generating private key... Done! Generating private key... Done!
Generating client cert... Done! Generating client cert... Done!
itsPappyTime> pappy>
The directory that the certs get put in may be different for you. Next, you'll need to add the generated ``certificate.crt`` file to your browser. This is different for each browser. The directory that the certs get put in may be different for you. Next, you'll need to add the generated ``certificate.crt`` file to your browser. This is different for each browser.
@ -107,7 +103,7 @@ Start up Pappy in Lite mode by running ``pappy -l``, enable the proxy in your br
/pappynatas/ $ pappy -l /pappynatas/ $ pappy -l
Temporary datafile is /tmp/tmp5AQBrH Temporary datafile is /tmp/tmp5AQBrH
Proxy is listening on port 8000 Proxy is listening on port 8000
itsPappyTime> ls pappy> ls
ID Verb Host Path S-Code Req Len Rsp Len Time Mngl ID Verb Host Path S-Code Req Len Rsp Len Time Mngl
8 GET vitaly.sexy /favicon.ico 404 Not Found 0 114 0.21 -- 8 GET vitaly.sexy /favicon.ico 404 Not Found 0 114 0.21 --
7 GET vitaly.sexy /favicon.ico 404 Not Found 0 114 0.22 -- 7 GET vitaly.sexy /favicon.ico 404 Not Found 0 114 0.22 --
@ -117,7 +113,7 @@ Start up Pappy in Lite mode by running ``pappy -l``, enable the proxy in your br
3 GET vitaly.sexy /vitaly2.jpg 200 OK 0 2034003 1.34 -- 3 GET vitaly.sexy /vitaly2.jpg 200 OK 0 2034003 1.34 --
2 GET vitaly.sexy / 200 OK 0 1201 0.21 -- 2 GET vitaly.sexy / 200 OK 0 1201 0.21 --
1 GET vitaly.sexy / 301 Moved Permanently 0 178 0.27 -- 1 GET vitaly.sexy / 301 Moved Permanently 0 178 0.27 --
itsPappyTime> quit pappy> quit
Deleting temporary datafile Deleting temporary datafile
Make sure that the request you made appears on the list. When you quit, the temporary data file will be deleted, so no cleanup will be required! Make sure that the request you made appears on the list. When you quit, the temporary data file will be deleted, so no cleanup will be required!
@ -129,8 +125,8 @@ Setting the Scope
----------------- -----------------
The first thing we'll do is set up Pappy so that it only intercepts requests going to ``*.natas.labs.overthewire.org``:: The first thing we'll do is set up Pappy so that it only intercepts requests going to ``*.natas.labs.overthewire.org``::
itsPappyTime> filter host containsr "natas\.labs\.overthewire\.org$" pappy> filter host containsr "natas\.labs\.overthewire\.org$"
itsPappyTime> scope_save pappy> scope_save
What these commands do: What these commands do:
@ -151,7 +147,7 @@ First, go to `<http://natas0.natas.labs.overthewire.org>`_ and log in with the d
Natas 1 Natas 1
------- -------
Haha! This is the same as natas0, but they got tricky and shut off right-clicking. There's still ways to view the source in the browser, but we'll use Pappy here. The two commands we'll learn here are ``ls``, ``vfq``, and ``vfs``. Haha! This is the same as natas0, but they got tricky and shut off right-clicking. There's still ways to view the source in the browser, but we'll use Pappy here. The commands we'll learn here are ``ls``, ``vfq``, and ``vfs``.
* ``ls`` lists the most current requests that are in the current context. You'll be using this a lot to get the IDs of requests you want to do things with. * ``ls`` lists the most current requests that are in the current context. You'll be using this a lot to get the IDs of requests you want to do things with.
* ``vfq <reqid>`` prints the full request of a request you specify * ``vfq <reqid>`` prints the full request of a request you specify
@ -159,7 +155,7 @@ Haha! This is the same as natas0, but they got tricky and shut off right-clickin
So to solve natas1, we'll want to view the full response to our request to the page:: So to solve natas1, we'll want to view the full response to our request to the page::
itsPappyTime> ls pappy> ls
ID Verb Host Path S-Code Req Len Rsp Len Time Mngl ID Verb Host Path S-Code Req Len Rsp Len Time Mngl
16 GET natas1.natas.labs.overthewire.org /favicon.ico 404 Not Found 0 307 0.27 -- 16 GET natas1.natas.labs.overthewire.org /favicon.ico 404 Not Found 0 307 0.27 --
15 GET natas1.natas.labs.overthewire.org /favicon.ico 404 Not Found 0 307 0.27 -- 15 GET natas1.natas.labs.overthewire.org /favicon.ico 404 Not Found 0 307 0.27 --
@ -177,7 +173,7 @@ So to solve natas1, we'll want to view the full response to our request to the p
3 GET natas.labs.overthewire.org /css/level.css 200 OK 0 1332 0.48 -- 3 GET natas.labs.overthewire.org /css/level.css 200 OK 0 1332 0.48 --
2 GET natas0.natas.labs.overthewire.org / 200 OK 0 918 0.26 -- 2 GET natas0.natas.labs.overthewire.org / 200 OK 0 918 0.26 --
1 GET natas0.natas.labs.overthewire.org / 401 Unauthorized 0 479 0.26 -- 1 GET natas0.natas.labs.overthewire.org / 401 Unauthorized 0 479 0.26 --
itsPappyTime> vfs 14 pappy> vfs 14
HTTP/1.1 200 OK HTTP/1.1 200 OK
Date: Fri, 18 Dec 2015 19:47:21 GMT Date: Fri, 18 Dec 2015 19:47:21 GMT
@ -197,7 +193,7 @@ So to solve natas1, we'll want to view the full response to our request to the p
... snip ... ... snip ...
itsPappyTime> pappy>
Yay! Yay!
@ -205,7 +201,7 @@ Natas 2
------- -------
When you visit this page, you get a message saying "There is nothing on this page". That is probably a blatant lie. Let's see what was in that response.:: When you visit this page, you get a message saying "There is nothing on this page". That is probably a blatant lie. Let's see what was in that response.::
itsPappyTime> ls pappy> ls
ID Verb Host Path S-Code Req Len Rsp Len Time Mngl ID Verb Host Path S-Code Req Len Rsp Len Time Mngl
30 GET natas2.natas.labs.overthewire.org /favicon.ico 404 Not Found 0 307 0.27 -- 30 GET natas2.natas.labs.overthewire.org /favicon.ico 404 Not Found 0 307 0.27 --
29 GET natas2.natas.labs.overthewire.org /favicon.ico 404 Not Found 0 307 0.27 -- 29 GET natas2.natas.labs.overthewire.org /favicon.ico 404 Not Found 0 307 0.27 --
@ -213,7 +209,7 @@ When you visit this page, you get a message saying "There is nothing on this pag
27 GET natas2.natas.labs.overthewire.org / 200 OK 0 872 0.27 -- 27 GET natas2.natas.labs.overthewire.org / 200 OK 0 872 0.27 --
26 GET natas2.natas.labs.overthewire.org / 401 Unauthorized 0 479 0.27 -- 26 GET natas2.natas.labs.overthewire.org / 401 Unauthorized 0 479 0.27 --
... snip ... ... snip ...
itsPappyTime> vfs 27 pappy> vfs 27
HTTP/1.1 200 OK HTTP/1.1 200 OK
... snip ... ... snip ...
@ -225,7 +221,7 @@ When you visit this page, you get a message saying "There is nothing on this pag
</div> </div>
</body></html> </body></html>
itsPappyTime> pappy>
So the only suspicious thing is ``<img src="files/pixel.png">``. I'll let you figure out the rest ;) So the only suspicious thing is ``<img src="files/pixel.png">``. I'll let you figure out the rest ;)
@ -282,7 +278,7 @@ While we can't find all the passwords with one filter, if we remember how we got
For natas0 and natas1, the responses had a phrase like "the password is abc123". So we can filter out anything that doesn't have the word "password" in it.:: For natas0 and natas1, the responses had a phrase like "the password is abc123". So we can filter out anything that doesn't have the word "password" in it.::
itsPappyTime> ls pappy> ls
ID Verb Host Path S-Code Req Len Rsp Len Time Mngl ID Verb Host Path S-Code Req Len Rsp Len Time Mngl
52 GET natas4.natas.labs.overthewire.org /favicon.ico 404 Not Found 0 307 0.26 -- 52 GET natas4.natas.labs.overthewire.org /favicon.ico 404 Not Found 0 307 0.26 --
51 GET natas4.natas.labs.overthewire.org /favicon.ico 404 Not Found 0 307 0.27 -- 51 GET natas4.natas.labs.overthewire.org /favicon.ico 404 Not Found 0 307 0.27 --
@ -309,8 +305,8 @@ For natas0 and natas1, the responses had a phrase like "the password is abc123".
30 GET natas2.natas.labs.overthewire.org /favicon.ico 404 Not Found 0 307 0.27 -- 30 GET natas2.natas.labs.overthewire.org /favicon.ico 404 Not Found 0 307 0.27 --
29 GET natas2.natas.labs.overthewire.org /favicon.ico 404 Not Found 0 307 0.27 -- 29 GET natas2.natas.labs.overthewire.org /favicon.ico 404 Not Found 0 307 0.27 --
28 GET natas2.natas.labs.overthewire.org /files/pixel.png 200 OK 0 303 0.27 -- 28 GET natas2.natas.labs.overthewire.org /files/pixel.png 200 OK 0 303 0.27 --
itsPappyTime> f body ct password pappy> f body ct password
itsPappyTime> ls pappy> ls
ID Verb Host Path S-Code Req Len Rsp Len Time Mngl ID Verb Host Path S-Code Req Len Rsp Len Time Mngl
49 GET natas4.natas.labs.overthewire.org / 401 Unauthorized 0 479 0.26 -- 49 GET natas4.natas.labs.overthewire.org / 401 Unauthorized 0 479 0.26 --
38 GET natas3.natas.labs.overthewire.org / 401 Unauthorized 0 479 0.28 -- 38 GET natas3.natas.labs.overthewire.org / 401 Unauthorized 0 479 0.28 --
@ -325,7 +321,7 @@ For natas0 and natas1, the responses had a phrase like "the password is abc123".
6 GET natas.labs.overthewire.org /js/jquery-1.9.1.js 200 OK 0 268381 1.20 -- 6 GET natas.labs.overthewire.org /js/jquery-1.9.1.js 200 OK 0 268381 1.20 --
2 GET natas0.natas.labs.overthewire.org / 200 OK 0 918 0.26 -- 2 GET natas0.natas.labs.overthewire.org / 200 OK 0 918 0.26 --
1 GET natas0.natas.labs.overthewire.org / 401 Unauthorized 0 479 0.26 -- 1 GET natas0.natas.labs.overthewire.org / 401 Unauthorized 0 479 0.26 --
itsPappyTime> pappy>
It looks like requests 2 and 14 are the ones we're looking for (we know the password is on the page and those are the requests to / that have a 200 OK response). Use ``vfs`` to look at the response and you'll get the passwords again! It looks like we also found the password from natas2 (the request to /s3cr3t/users.txt). It looks like requests 2 and 14 are the ones we're looking for (we know the password is on the page and those are the requests to / that have a 200 OK response). Use ``vfs`` to look at the response and you'll get the passwords again! It looks like we also found the password from natas2 (the request to /s3cr3t/users.txt).
@ -341,7 +337,7 @@ To do this, we'll be using Pappy's interceptor. The interceptor lets you stop a
In this case, we only want to intercept requests, so we'll run ``ic req``:: In this case, we only want to intercept requests, so we'll run ``ic req``::
itsPappyTime> ic req pappy> ic req
And we'll get a screen that says something like:: And we'll get a screen that says something like::
@ -405,14 +401,15 @@ For this one, when you view the source you'll notice they're taking value you en
Use ``:wq!`` to quit the repeater without having to save buffers Use ``:wq!`` to quit the repeater without having to save buffers
.. note:: .. note::
You must know the basics of how to use vim for the repeater and have a key bound to the leader. You can find more information on the leader key ``here <https://stackoverflow.com/questions/1764263/what-is-the-leader-in-a-vimrc-file>``. By default <leader> is bound to ``\``. You must know the basics of how to use vim for the repeater and have a key bound to the leader. You can find more information on the leader key `here <https://stackoverflow.com/questions/1764263/what-is-the-leader-in-a-vimrc-file>`_. By default <leader> is bound to ``\``.
Submit a request then open that request in the repeater::
Submit a request then open that request in the repeater.:: pappy> ls
itsPappyTime> ls
196 GET natas9.natas.labs.overthewire.org /index.php?needle=ball&submit=Search 200 OK 0 1686 0.27 -- 196 GET natas9.natas.labs.overthewire.org /index.php?needle=ball&submit=Search 200 OK 0 1686 0.27 --
195 GET natas9.natas.labs.overthewire.org /index-source.html 200 OK 0 1952 0.27 -- 195 GET natas9.natas.labs.overthewire.org /index-source.html 200 OK 0 1952 0.27 --
... snip ... ... snip ...
itsPappyTime> rp 196 pappy> rp 196
Vim will open up in a vertical split with the request on the left and the response on the right. Vim will open up in a vertical split with the request on the left and the response on the right.
@ -434,7 +431,7 @@ Commands we'll learn:
So the first thing we'll do is submit a request to have a base request that we can modify. Submit a request with any username. You should get a response back saying the user doesn't exist. Now we'll generate a macro and use that request as a base for our script:: So the first thing we'll do is submit a request to have a base request that we can modify. Submit a request with any username. You should get a response back saying the user doesn't exist. Now we'll generate a macro and use that request as a base for our script::
itsPappyTime> ls pappy> ls
ID Verb Host Path S-Code Req Len Rsp Len Time Mngl ID Verb Host Path S-Code Req Len Rsp Len Time Mngl
224 POST natas15.natas.labs.overthewire.org /index.php 200 OK 14 937 0.27 -- 224 POST natas15.natas.labs.overthewire.org /index.php 200 OK 14 937 0.27 --
223 POST natas15.natas.labs.overthewire.org /index.php 200 OK 12 937 0.27 -- 223 POST natas15.natas.labs.overthewire.org /index.php 200 OK 12 937 0.27 --
@ -445,9 +442,9 @@ So the first thing we'll do is submit a request to have a base request that we c
218 GET natas15.natas.labs.overthewire.org / 401 Unauthorized 0 480 0.27 -- 218 GET natas15.natas.labs.overthewire.org / 401 Unauthorized 0 480 0.27 --
... snip ... ... snip ...
itsPappyTime> gma brute 224 pappy> gma brute 224
Wrote script to macro_brute.py Wrote script to macro_brute.py
itsPappyTime> pappy>
Now open up ``macro_brute.py`` in your favorite text editor. You should have a script that looks like this:: Now open up ``macro_brute.py`` in your favorite text editor. You should have a script that looks like this::
@ -513,15 +510,15 @@ To start out simple, we'll write a macro that lets us check a username from the
Then to run it:: Then to run it::
itsPappyTime> lma pappy> lma
Loaded "<Macro Macro 41855887 (brute)>" Loaded "<Macro Macro 41855887 (brute)>"
itsPappyTime> rma brute admin pappy> rma brute admin
admin is not a user admin is not a user
itsPappyTime> rma brute fooooo pappy> rma brute fooooo
fooooo is not a user fooooo is not a user
itsPappyTime> rma brute natas16 pappy> rma brute natas16
natas16 is a user! natas16 is a user!
itsPappyTime> pappy>
Awesome! Notice how we didn't have to deal with authentication either. This is because the authentication is handled by the ``Authorization`` header which was included in the generated request. Awesome! Notice how we didn't have to deal with authentication either. This is because the authentication is handled by the ``Authorization`` header which was included in the generated request.
@ -535,11 +532,11 @@ So to escape it, we use a payload like::
In this case, any username that ends in ``" OR 1=1; #`` will be considered a valid username. Let's try this out:: In this case, any username that ends in ``" OR 1=1; #`` will be considered a valid username. Let's try this out::
itsPappyTime> rma brute "foo\" OR 1=1;" pappy> rma brute "foo\" OR 1=1;"
foo" OR 1=1; is a user! foo" OR 1=1; is a user!
itsPappyTime> rma brute "fooooooo\" OR 1=1;" pappy> rma brute "fooooooo\" OR 1=1;"
fooooooo" OR 1=1; is a user! fooooooo" OR 1=1; is a user!
itsPappyTime> pappy>
Great! Now we can check any true/false condition we want. In this case, we want to check if a certain character is at a certain position in the ``password`` column. We do this with the ``ASCII`` and ``SUBSTRING`` functions. So something like this will check if the first character is an ``A``.:: Great! Now we can check any true/false condition we want. In this case, we want to check if a certain character is at a certain position in the ``password`` column. We do this with the ``ASCII`` and ``SUBSTRING`` functions. So something like this will check if the first character is an ``A``.::
@ -598,9 +595,9 @@ Alright, let's update our macro to find the first character of the password.::
And when we run it...:: And when we run it...::
itsPappyTime> lma pappy> lma
Loaded "<Macro Macro 41855887 (brute)>" Loaded "<Macro Macro 41855887 (brute)>"
itsPappyTime> rma brute pappy> rma brute
Trying a... Trying a...
Trying b... Trying b...
Trying c... Trying c...
@ -610,7 +607,7 @@ And when we run it...::
Trying V... Trying V...
Trying W... Trying W...
W is the first char! W is the first char!
itsPappyTime> pappy>
We find the first character! Woo! Next we just have to do this for each position. Even through we don't know the length of the password, we will know that the password is over when none of the characters are valid. So let's update our macro:: We find the first character! Woo! Next we just have to do this for each position. Even through we don't know the length of the password, we will know that the password is over when none of the characters are valid. So let's update our macro::
@ -685,9 +682,9 @@ We find the first character! Woo! Next we just have to do this for each position
Then we run it:: Then we run it::
itsPappyTime> lma pappy> lma
Loaded "<Macro Macro 41855887 (brute)>" Loaded "<Macro Macro 41855887 (brute)>"
itsPappyTime> rma brute pappy> rma brute
a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W
W is char 1! W is char 1!
The password so far is W The password so far is W
@ -713,7 +710,7 @@ Then we run it::
The password so far is WaIHEacj63wnNIBROHeqi3p9t0m5nhmh The password so far is WaIHEacj63wnNIBROHeqi3p9t0m5nhmh
a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 0 a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 0
Done! The password is "WaIHEacj63wnNIBROHeqi3p9t0m5nhmh" Done! The password is "WaIHEacj63wnNIBROHeqi3p9t0m5nhmh"
itsPappyTime> pappy>
Boom! There it is! Boom! There it is!

@ -1,10 +1,10 @@
import base64 import base64
import json import json
import pappyproxy
from twisted.protocols.basic import LineReceiver from twisted.protocols.basic import LineReceiver
from twisted.internet import defer from twisted.internet import defer
from util import PappyException from util import PappyException
from .http import Request, Response
""" """
comm.py comm.py
@ -31,6 +31,8 @@ class CommServer(LineReceiver):
} }
def lineReceived(self, line): def lineReceived(self, line):
from .http import Request, Response
if line == '': if line == '':
return return
try: try:
@ -71,7 +73,7 @@ class CommServer(LineReceiver):
def action_get_request(self, data): def action_get_request(self, data):
try: try:
reqid = data['reqid'] reqid = data['reqid']
req = yield pappyproxy.http.Request.load_request(reqid) req = yield Request.load_request(reqid)
except KeyError: except KeyError:
raise PappyException("Request with given ID does not exist") raise PappyException("Request with given ID does not exist")
@ -82,12 +84,12 @@ class CommServer(LineReceiver):
def action_get_response(self, data): def action_get_response(self, data):
try: try:
reqid = data['reqid'] reqid = data['reqid']
req = yield pappyproxy.http.Request.load_request(reqid) req = yield Request.load_request(reqid)
except KeyError: except KeyError:
raise PappyException("Request with given ID does not exist, cannot fetch associated response.") raise PappyException("Request with given ID does not exist, cannot fetch associated response.")
if req.response: if req.response:
rsp = yield pappyproxy.http.Response.load_response(req.response.rspid) rsp = yield Response.load_response(req.response.rspid)
dat = json.loads(rsp.to_json()) dat = json.loads(rsp.to_json())
else: else:
dat = {} dat = {}
@ -95,13 +97,8 @@ class CommServer(LineReceiver):
@defer.inlineCallbacks @defer.inlineCallbacks
def action_submit_request(self, data): def action_submit_request(self, data):
try: message = base64.b64decode(data['full_message'])
req = pappyproxy.http.Request(base64.b64decode(data['full_request'])) req = yield Request.submit_new(data['host'], data['port'], data['is_ssl'], message)
req.port = data['port']
req.is_ssl = data['is_ssl']
except:
raise PappyException("Error parsing request")
yield req.async_submit()
yield req.async_deep_save() yield req.async_deep_save()
retdata = {} retdata = {}

@ -1,20 +1,89 @@
import imp """
The configuration settings for the proxy.
.. data:: CERT_DIR
The location of the CA certs that Pappy will use. This can be configured in the
``config.json`` file for a project.
:Default: ``{DATADIR}/certs``
.. data:: PAPPY_DIR
The file where pappy's scripts are located. Don't write anything here, and you
probably don't need to write anything here. Use DATA_DIR instead.
:Default: Wherever the scripts are installed
.. data:: DATA_DIR
The data directory. This is where files that have to be read by Pappy every time
it's run are put. For example, plugins are stored in ``{DATADIR}/plugins`` and
certs are by default stored in ``{DATADIR}/certs``. This defaults to ``~/.pappy``
and isn't configurable right now.
:Default: ``~/.pappy``
.. data:: DATAFILE
The location of the CA certs that Pappy will use. This can be configured in the
``config.json`` file for a project.
:Default: ``data.db``
.. data:: DEBUG_DIR
The directory to write debug output to. Don't put this outside the project folder
since it writes all the request data to this directory. You probably won't need
to use this. Configured in the ``config.json`` file for the project.
:Default: None
.. data: LISTENERS
The list of active listeners. It is a list of tuples of the format (port, interface)
Not modifiable after startup. Configured in the ``config.json`` file for the project.
:Default: ``[(8000, '127.0.0.1')]``
.. data: PLUGIN_DIRS
List of directories that plugins are loaded from. Not modifiable.
:Default: ``['{DATA_DIR}/plugins', '{PAPPY_DIR}/plugins']``
.. data: CONFIG_DICT
The dictionary read from config.json. When writing plugins, use this to load
configuration options for your plugin.
"""
import json import json
import os import os
import shutil import shutil
PAPPY_DIR = os.path.dirname(os.path.realpath(__file__)) PAPPY_DIR = os.path.dirname(os.path.realpath(__file__))
DATA_DIR = os.path.join(os.path.expanduser('~'), '.pappy') DATA_DIR = os.path.join(os.path.expanduser('~'), '.pappy')
DATA_DIR
CERT_DIR = os.path.join(DATA_DIR, 'certs') CERT_DIR = os.path.join(DATA_DIR, 'certs')
DATAFILE = 'data.db' DATAFILE = 'data.db'
DEBUG_DIR = None DEBUG_DIR = None
DEBUG_TO_FILE = False DEBUG_TO_FILE = False
DEBUG_VERBOSITY = 0 DEBUG_VERBOSITY = 0
LISTENERS = [(8000, '127.0.0.1')] LISTENERS = [(8000, '127.0.0.1')]
SSL_CA_FILE = 'certificate.crt' SSL_CA_FILE = 'certificate.crt'
SSL_PKEY_FILE = 'private.key' SSL_PKEY_FILE = 'private.key'
PLUGIN_DIRS = [os.path.join(DATA_DIR, 'plugins'), os.path.join(PAPPY_DIR, 'plugins')]
CONFIG_DICT = {}
def get_default_config(): def get_default_config():
default_config_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), default_config_file = os.path.join(os.path.dirname(os.path.realpath(__file__)),
'default_user_config.json') 'default_user_config.json')
@ -61,6 +130,7 @@ def load_settings(proj_config):
def load_from_file(fname): def load_from_file(fname):
global CONFIG_DICT
# Make sure we have a config file # Make sure we have a config file
if not os.path.isfile(fname): if not os.path.isfile(fname):
print "Copying default config to %s" % fname print "Copying default config to %s" % fname
@ -70,5 +140,5 @@ def load_from_file(fname):
# Load local project config # Load local project config
with open(fname, 'r') as f: with open(fname, 'r') as f:
proj_config = json.load(f) CONFIG_DICT = json.load(f)
load_settings(proj_config) load_settings(CONFIG_DICT)

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

@ -1,5 +1,5 @@
import os import os
import itertools
from .config import PAPPY_DIR from .config import PAPPY_DIR
def from_file(fname, intro=False): def from_file(fname, intro=False):

@ -3,11 +3,12 @@ import imp
import os import os
import random import random
import re import re
import stat
from pappyproxy import http from jinja2 import Environment, FileSystemLoader
from pappyproxy import config from pappyproxy import config
from pappyproxy.util import PappyException
from twisted.internet import defer from twisted.internet import defer
from jinja2 import Environment, FileSystemLoader
class Macro(object): class Macro(object):
""" """
@ -39,6 +40,9 @@ class Macro(object):
if self.filename: if self.filename:
match = re.findall('.*macro_(.*).py$', self.filename) match = re.findall('.*macro_(.*).py$', self.filename)
self.file_name = match[0] self.file_name = match[0]
st = os.stat(self.filename)
if (st.st_mode & stat.S_IWOTH):
raise PappyException("Refusing to load world-writable macro: %s" % self.filename)
module_name = os.path.basename(os.path.splitext(self.filename)[0]) module_name = os.path.basename(os.path.splitext(self.filename)[0])
self.source = imp.load_source('%s'%module_name, self.filename) self.source = imp.load_source('%s'%module_name, self.filename)
if not hasattr(self.source, 'MACRO_NAME'): if not hasattr(self.source, 'MACRO_NAME'):
@ -63,11 +67,43 @@ class InterceptMacro(object):
A class representing a macro that modifies requests as they pass through the A class representing a macro that modifies requests as they pass through the
proxy proxy
""" """
def __init__(self, filename=''): def __init__(self):
self.name = '' self.name = ''
self.short_name = None self.short_name = None
self.intercept_requests = True self.intercept_requests = False
self.intercept_responses = True self.intercept_responses = False
self.do_req = False
self.do_rsp = False
self.do_async_req = False
self.do_async_rsp = False
def __repr__(self):
return "<InterceptingMacro (%s)>" % self.name
def init(self, args):
pass
def mangle_request(self, request):
return request
def mangle_response(self, request):
return request.response
@defer.inlineCallbacks
def async_mangle_request(self, request):
defer.returnValue(request)
@defer.inlineCallbacks
def async_mangle_response(self, request):
defer.returnValue(request.response)
class FileInterceptMacro(InterceptMacro):
"""
An intercepting macro that loads a macro from a file.
"""
def __init__(self, filename=''):
InterceptMacro.__init__(self)
self.file_name = '' # name from the file self.file_name = '' # name from the file
self.filename = filename or '' # filename we load from self.filename = filename or '' # filename we load from
self.source = None self.source = None
@ -85,36 +121,6 @@ class InterceptMacro(object):
s += ' (%s)' % ('/'.join(names)) s += ' (%s)' % ('/'.join(names))
return "<InterceptingMacro %s>" % s return "<InterceptingMacro %s>" % s
@property
def do_req(self):
if (self.source and hasattr(self.source, 'async_mangle_request') or \
self.source and hasattr(self.source, 'mangle_request')) and \
self.intercept_requests:
return True
return False
@property
def do_rsp(self):
if (self.source and hasattr(self.source, 'async_mangle_response') or \
self.source and hasattr(self.source, 'mangle_response')) and \
self.intercept_responses:
return True
return False
@property
def async_req(self):
if self.source and hasattr(self.source, 'async_mangle_request'):
return True
else:
return False
@property
def async_rsp(self):
if self.source and hasattr(self.source, 'async_mangle_response'):
return True
else:
return False
def load(self): def load(self):
if self.filename: if self.filename:
match = re.findall('.*int_(.*).py$', self.filename) match = re.findall('.*int_(.*).py$', self.filename)
@ -122,6 +128,9 @@ class InterceptMacro(object):
self.file_name = match[0] self.file_name = match[0]
else: else:
self.file_name = self.filename self.file_name = self.filename
st = os.stat(self.filename)
if (st.st_mode & stat.S_IWOTH):
raise PappyException("Refusing to load world-writable macro: %s" % self.filename)
module_name = os.path.basename(os.path.splitext(self.filename)[0]) module_name = os.path.basename(os.path.splitext(self.filename)[0])
self.source = imp.load_source('%s'%module_name, self.filename) self.source = imp.load_source('%s'%module_name, self.filename)
self.name = self.source.MACRO_NAME self.name = self.source.MACRO_NAME
@ -141,9 +150,28 @@ class InterceptMacro(object):
else: else:
self.source = None self.source = None
def init(self, line): # Update what we can do
if self.source and hasattr(self.source, 'mangle_request'):
self.intercept_requests = True
self.async_req = False
elif self.source and hasattr(self.source, 'async_mangle_request'):
self.intercept_requests = True
self.async_req = True
else:
self.intercept_requests = True
if self.source and hasattr(self.source, 'mangle_response'):
self.intercept_responses = True
self.async_rsp = False
elif self.source and hasattr(self.source, 'async_mangle_response'):
self.intercept_responses = True
self.async_rsp = True
else:
self.intercept_responses = False
def init(self, args):
if hasattr(self.source, 'init'): if hasattr(self.source, 'init'):
self.source.init(line) self.source.init(args)
def mangle_request(self, request): def mangle_request(self, request):
if hasattr(self.source, 'mangle_request'): if hasattr(self.source, 'mangle_request'):
@ -178,12 +206,18 @@ def load_macros(loc):
macro_files = glob.glob(loc + "/macro_*.py") macro_files = glob.glob(loc + "/macro_*.py")
macro_objs = [] macro_objs = []
for f in macro_files: for f in macro_files:
try:
macro_objs.append(Macro(f)) macro_objs.append(Macro(f))
except PappyException as e:
print str(e)
int_macro_files = glob.glob(loc + "/int_*.py") int_macro_files = glob.glob(loc + "/int_*.py")
int_macro_objs = [] int_macro_objs = []
for f in int_macro_files: for f in int_macro_files:
int_macro_objs.append(InterceptMacro(f)) try:
int_macro_objs.append(FileInterceptMacro(f))
except PappyException as e:
print str(e)
return (macro_objs, int_macro_objs) return (macro_objs, int_macro_objs)
def req_obj_def(req): def req_obj_def(req):
@ -198,6 +232,8 @@ def req_obj_def(req):
else: else:
if req.port != 80: if req.port != 80:
params.append('port=%d'%req.port) params.append('port=%d'%req.port)
if 'host' in req.headers and req.host != req.headers['host']:
params.append('host=%d'%req.host)
if params: if params:
req_params = ', '+', '.join(params) req_params = ', '+', '.join(params)
else: else:
@ -223,7 +259,6 @@ def macro_from_requests(reqs, short_name='', long_name=''):
subs['short_name'] = short_name subs['short_name'] = short_name
n = 0
req_lines = [] req_lines = []
req_params = [] req_params = []
for req in reqs: for req in reqs:

@ -1,30 +1,53 @@
#!/usr/bin/env python2 #!/usr/bin/env python2
import argparse import argparse
import cmd2
import crochet import crochet
import datetime import datetime
import imp
import os import os
import schema.update import schema.update
import shutil import shutil
import sys import sys
import sqlite3
import tempfile import tempfile
from pappyproxy import console
from pappyproxy import config from . import comm
from pappyproxy import comm from . import config
from pappyproxy import http from . import context
from pappyproxy import context from . import http
from pappyproxy import proxy from . import plugin
from . import proxy
from .console import ProxyCmd
from twisted.enterprise import adbapi from twisted.enterprise import adbapi
from twisted.internet import reactor, defer from twisted.internet import reactor, defer
from twisted.internet.threads import deferToThread
from twisted.internet.protocol import ServerFactory
from twisted.internet.error import CannotListenError from twisted.internet.error import CannotListenError
from twisted.internet.protocol import ServerFactory
from twisted.internet.threads import deferToThread
crochet.no_setup() crochet.no_setup()
server_factory = None
main_context = context.Context()
all_contexts = [main_context]
plugin_loader = None
cons = None
@defer.inlineCallbacks
def wait_for_saves(ignored):
reset = True
printed = False
lastprint = 0
while reset:
reset = False
togo = 0
for c in all_contexts:
for r in c.all_reqs:
if r.reqid == '--':
reset = True
togo += 1
d = defer.Deferred()
d.callback(None)
yield d
if togo % 10 == 0 and lastprint != togo:
lastprint = togo
print '%d requests left to be saved (probably won\'t work)' % togo
def parse_args(): def parse_args():
# parses sys.argv and returns a settings dictionary # parses sys.argv and returns a settings dictionary
@ -51,6 +74,9 @@ def delete_datafile():
@defer.inlineCallbacks @defer.inlineCallbacks
def main(): def main():
global server_factory
global plugin_loader
global cons
settings = parse_args() settings = parse_args()
load_start = datetime.datetime.now() load_start = datetime.datetime.now()
@ -77,7 +103,12 @@ def main():
check_same_thread=False, check_same_thread=False,
cp_openfun=set_text_factory, cp_openfun=set_text_factory,
cp_max=1) cp_max=1)
yield schema.update.update_schema(dbpool) try:
yield schema.update.update_schema(dbpool, config.DATAFILE)
except Exception as e:
print 'Error updating schema: %s' % e
print 'Exiting...'
reactor.stop()
http.init(dbpool) http.init(dbpool)
yield context.init() yield context.init()
@ -85,17 +116,17 @@ def main():
if config.DEBUG_DIR and os.path.exists(config.DEBUG_DIR): if config.DEBUG_DIR and os.path.exists(config.DEBUG_DIR):
shutil.rmtree(config.DEBUG_DIR) shutil.rmtree(config.DEBUG_DIR)
print 'Removing old debugging output' print 'Removing old debugging output'
serv_factory = proxy.ProxyServerFactory(save_all=True) server_factory = proxy.ProxyServerFactory(save_all=True)
listen_strs = [] listen_strs = []
listening = False ports = []
for listener in config.LISTENERS: for listener in config.LISTENERS:
try: try:
reactor.listenTCP(listener[0], serv_factory, interface=listener[1]) port = reactor.listenTCP(listener[0], server_factory, interface=listener[1])
listening = True
listener_str = 'port %d' % listener[0] listener_str = 'port %d' % listener[0]
if listener[1] not in ('127.0.0.1', 'localhost'): if listener[1] not in ('127.0.0.1', 'localhost'):
listener_str += ' (bound to %s)' % listener[1] listener_str += ' (bound to %s)' % listener[1]
listen_strs.append(listener_str) listen_strs.append(listener_str)
ports.append(port)
except CannotListenError as e: except CannotListenError as e:
print repr(e) print repr(e)
if listen_strs: if listen_strs:
@ -112,19 +143,31 @@ def main():
# Load the scope # Load the scope
yield context.load_scope(http.dbpool) yield context.load_scope(http.dbpool)
context.reset_to_scope() context.reset_to_scope(main_context)
# Apologize for slow start times # Apologize for slow start times
load_end = datetime.datetime.now() load_end = datetime.datetime.now()
load_time = (load_end - load_start) load_time = (load_end - load_start)
if load_time.total_seconds() > 20: if load_time.total_seconds() > 20:
print 'Startup was slow (%s)! Sorry!' % load_time print 'Startup was slow (%s)! Sorry!' % load_time
print 'Database has {0} requests (~{1:.2f}ms per request)'.format(len(context.active_requests), ((load_time.total_seconds()/len(context.active_requests))*1000)) print 'Database has {0} requests (~{1:.2f}ms per request)'.format(len(main_context.active_requests), ((load_time.total_seconds()/len(main_context.active_requests))*1000))
sys.argv = [sys.argv[0]] # cmd2 tries to parse args sys.argv = [sys.argv[0]] # cmd2 tries to parse args
cons = console.ProxyCmd() cons = ProxyCmd()
console.set_proxy_server_factory(serv_factory) plugin_loader = plugin.PluginLoader(cons)
for d in config.PLUGIN_DIRS:
if not os.path.exists(d):
os.makedirs(d)
plugin_loader.load_directory(d)
@defer.inlineCallbacks
def close_listeners(ignored):
for port in ports:
yield port.stopListening()
d = deferToThread(cons.cmdloop) d = deferToThread(cons.cmdloop)
d.addCallback(close_listeners)
d.addCallback(wait_for_saves)
d.addCallback(lambda ignored: reactor.stop()) d.addCallback(lambda ignored: reactor.stop())
if delete_data_on_quit: if delete_data_on_quit:
d.addCallback(lambda ignored: delete_datafile()) d.addCallback(lambda ignored: delete_datafile())

@ -0,0 +1,150 @@
"""
This module contains all the api calls written for use in plugins. If you want
to do anything that is't allowed through these function calls or through the
functions provided for macros, contact me and I'll see what I can do to add some
more functionality into the next version.
"""
import glob
import imp
import os
import pappyproxy
import stat
from .proxy import add_intercepting_macro as proxy_add_intercepting_macro
from .proxy import remove_intercepting_macro as proxy_remove_intercepting_macro
from .util import PappyException
class Plugin(object):
def __init__(self, cmd, fname=None):
self.cmd = cmd
self.filename = ''
self.source = None
self.module_name = ''
if fname:
self.filename = fname
self.load_file(fname)
def load_file(self, fname):
module_name = os.path.basename(os.path.splitext(fname)[0])
if os.path.basename(fname) == '__init__.py':
return
st = os.stat(fname)
if (st.st_mode & stat.S_IWOTH):
raise PappyException("Refusing to load world-writable plugin: %s" % fname)
self.source = imp.load_source('%s'%module_name, fname)
if hasattr(self.source, 'load_cmds'):
self.source.load_cmds(self.cmd)
else:
print ('WARNING: %s does not define load_cmds. It will not be '
'possible to interact with the plugin through the console.' % fname)
self.module_name = module_name
class PluginLoader(object):
def __init__(self, cmd):
self.cmd = cmd
self.loaded_plugins = []
self.plugins_by_name = {}
def load_plugin(self, fname):
p = Plugin(self.cmd, fname)
self.loaded_plugins.append(p)
self.plugins_by_name[p.module_name] = p
def load_directory(self, directory):
fnames = glob.glob(os.path.join(directory, '*.py'))
for fname in fnames:
try:
self.load_plugin(fname)
except PappyException as e:
print str(e)
##########################
## Plugin helper functions
def plugin_by_name(name):
"""
Returns an interface to access the methods of a plugin from its name.
For example, to call the ``foo`` function from the ``bar`` plugin
you would call ``plugin_by_name('bar').foo()``.
"""
import pappyproxy.pappy
if name in pappyproxy.pappy.plugin_loader.plugins_by_name:
return pappyproxy.pappy.plugin_loader.plugins_by_name[name].source
else:
raise PappyException('No plugin with name %s is loaded' % name)
def add_intercepting_macro(name, macro):
"""
Adds an intercepting macro to the proxy. You can either use a
:class:`pappyproxy.macros.FileInterceptMacro` to load an intercepting macro
from the disk, or you can create your own using an :class:`pappyproxy.macros.InterceptMacro`
for a base class. You must give a unique name that will be used in
:func:`pappyproxy.plugin.remove_intercepting_macro` to deactivate it. Remember
that activating an intercepting macro will disable request streaming and will
affect performance. So please try and only use this if you may need to modify
messages before they are passed along.
"""
proxy_add_intercepting_macro(name, macro, pappyproxy.pappy.server_factory.intercepting_macros)
def remove_intercepting_macro(name):
"""
Stops an active intercepting macro. You must pass in the name that you used
when calling :func:`pappyproxy.plugin.add_intercepting_macro` to identify
which macro you would like to stop.
"""
proxy_remove_intercepting_macro(name, pappyproxy.pappy.server_factory.intercepting_macros)
def active_intercepting_macros():
"""
Returns a list of the active intercepting macro objects. Modifying this list
will not affect which macros are active.
"""
return pappyproxy.pappy.server_factory.intercepting_macros[:]
def in_memory_reqs():
"""
Returns a list containing all out of the requests which exist in memory only
(requests with an m## style id).
You can call either :func:`pappyproxy.http.Request.save` or
:func:`pappyproxy.http.Request.async_save` to save the request to the data file.
"""
return list(pappyproxy.context.Context.in_memory_requests)
def all_reqs():
"""
Returns a list containing all the requests in history (including requests
that only exist in memory). Modifying this list will not modify requests
included in the history. However, you can edit the requests
in this list then call either :func:`pappyproxy.http.Request.save` or
:func:`pappyproxy.http.Request.async_save` to modify the actual request.
"""
return list(pappyproxy.context.Context.all_reqs)
def main_context():
"""
Returns the context object representing the main context. Use this to interact
with the context. The returned object can be modified
at will. Avoid modifying any class values (ie all_reqs, in_memory_requests)
and use the class methods to add/remove requests. See the documentation on
:class:`pappyproxy.context.Context` for more information.
"""
return pappyproxy.pappy.main_context
def add_req(req):
"""
Adds a request to the history. Will not do anything to requests which are
already in history. If the request is not saved, it will be given an m## id.
"""
pappyproxy.pappy.main_context.add_request(req)
def run_cmd(cmd):
"""
Run a command as if you typed it into the console. Try and use existing APIs
to do what you want before using this.
"""
pappyproxy.pappy.cons.onecmd(cmd)

@ -0,0 +1,192 @@
import crochet
import pappyproxy
from pappyproxy.console import confirm
from pappyproxy.util import PappyException
from twisted.internet import defer
class BuiltinFilters(object):
_filters = {
'not_image': (
['path nctr "(\.png$|\.jpg$|\.gif$)"'],
'Filter out image requests',
),
'not_jscss': (
['path nctr "(\.js$|\.css$)"'],
'Filter out javascript and css files',
),
}
@staticmethod
@defer.inlineCallbacks
def get(name):
if name not in BuiltinFilters._filters:
raise PappyException('%s not a bult in filter' % name)
if name in BuiltinFilters._filters:
filters = [pappyproxy.context.Filter(f) for f in BuiltinFilters._filters[name][0]]
for f in filters:
yield f.generate()
defer.returnValue(filters)
raise PappyException('"%s" is not a built-in filter' % name)
@staticmethod
def list():
return [k for k, v in BuiltinFilters._filters.iteritems()]
@staticmethod
def help(name):
if name not in BuiltinFilters._filters:
raise PappyException('"%s" is not a built-in filter' % name)
return pappyproxy.context.Filter(BuiltinFilters._filters[name][1])
@crochet.wait_for(timeout=None)
@defer.inlineCallbacks
def filtercmd(line):
"""
Apply a filter to the current context
Usage: filter <filter string>
See README.md for information on filter strings
"""
if not line:
raise PappyException("Filter string required")
filter_to_add = pappyproxy.context.Filter(line)
yield filter_to_add.generate()
pappyproxy.pappy.main_context.add_filter(filter_to_add)
def complete_builtin_filter(text, line, begidx, endidx):
all_names = BuiltinFilters.list()
if not text:
ret = all_names[:]
else:
ret = [n for n in all_names if n.startswith(text)]
return ret
@crochet.wait_for(timeout=None)
@defer.inlineCallbacks
def builtin_filter(line):
if not line:
raise PappyException("Filter name required")
filters_to_add = yield BuiltinFilters.get(line)
for f in filters_to_add:
print f.filter_string
pappyproxy.pappy.main_context.add_filter(f)
defer.returnValue(None)
def filter_up(line):
"""
Remove the last applied filter
Usage: filter_up
"""
pappyproxy.pappy.main_context.filter_up()
@crochet.wait_for(timeout=None)
@defer.inlineCallbacks
def filter_clear(line):
"""
Reset the context so that it contains no filters (ignores scope)
Usage: filter_clear
"""
pappyproxy.pappy.main_context.active_filters = []
yield pappyproxy.context.reload_from_storage()
def filter_list(line):
"""
Print the filters that make up the current context
Usage: filter_list
"""
for f in pappyproxy.pappy.main_context.active_filters:
print f.filter_string
@crochet.wait_for(timeout=None)
@defer.inlineCallbacks
def scope_save(line):
"""
Set the scope to be the current context. Saved between launches
Usage: scope_save
"""
pappyproxy.context.save_scope(pappyproxy.pappy.main_context)
yield pappyproxy.context.store_scope(pappyproxy.http.dbpool)
def scope_reset(line):
"""
Set the context to be the scope (view in-scope items)
Usage: scope_reset
"""
pappyproxy.context.reset_to_scope(pappyproxy.pappy.main_context)
@crochet.wait_for(timeout=None)
@defer.inlineCallbacks
def scope_delete(line):
"""
Delete the scope so that it contains all request/response pairs
Usage: scope_delete
"""
pappyproxy.context.set_scope([])
yield pappyproxy.context.store_scope(pappyproxy.http.dbpool)
def scope_list(line):
"""
Print the filters that make up the scope
Usage: scope_list
"""
pappyproxy.context.print_scope()
@crochet.wait_for(timeout=None)
@defer.inlineCallbacks
def filter_prune(line):
"""
Delete all out of context requests from the data file.
CANNOT BE UNDONE!! Be careful!
Usage: filter_prune
"""
# Delete filtered items from datafile
print ''
print 'Currently active filters:'
for f in pappyproxy.pappy.main_context.active_filters:
print '> %s' % f.filter_string
# We copy so that we're not removing items from a set we're iterating over
reqs = list(pappyproxy.pappy.main_context.inactive_requests)
act_reqs = list(pappyproxy.pappy.main_context.active_requests)
message = 'This will delete %d/%d requests. You can NOT undo this!! Continue?' % (len(reqs), (len(reqs) + len(act_reqs)))
if not confirm(message, 'n'):
defer.returnValue(None)
for r in reqs:
yield r.deep_delete()
print 'Deleted %d requests' % len(reqs)
defer.returnValue(None)
###############
## Plugin hooks
def load_cmds(cmd):
cmd.set_cmds({
'filter_prune': (filter_prune, None),
'scope_list': (scope_list, None),
'scope_delete': (scope_delete, None),
'scope_reset': (scope_reset, None),
'scope_save': (scope_save, None),
'filter_list': (filter_list, None),
'filter_clear': (filter_clear, None),
'filter_up': (filter_up, None),
'builtin_filter': (builtin_filter, complete_builtin_filter),
'filter': (filtercmd, None),
})
cmd.add_aliases([
#('filter_prune', ''),
('scope_list', 'sls'),
#('scope_delete', ''),
('scope_reset', 'sr'),
#('scope_save', ''),
('filter_list', 'fls'),
('filter_clear', 'fc'),
('filter_up', 'fu'),
('builtin_filter', 'fbi'),
('filter', 'f'),
('filter', 'fl'),
])

@ -0,0 +1,215 @@
import crochet
import pappyproxy
import shlex
from pappyproxy.plugin import active_intercepting_macros, add_intercepting_macro, remove_intercepting_macro
from pappyproxy.console import load_reqlist
from pappyproxy.macros import load_macros, macro_from_requests, gen_imacro
from pappyproxy.util import PappyException
from twisted.internet import defer
loaded_macros = []
loaded_int_macros = []
macro_dict = {}
int_macro_dict = {}
def load_macros_cmd(line):
"""
Load macros from a directory. By default loads macros in the current directory.
Usage: load_macros [dir]
"""
global macro_dict
global int_macro_dict
global loaded_macros
global loaded_int_macros
if line:
load_dir = line
else:
load_dir = '.'
(to_load, int_to_load) = load_macros(load_dir)
if not to_load and not int_to_load:
raise PappyException('No macros to load.')
macro_dict = {}
loaded_macros = []
int_macro_dict = {}
loaded_int_macros = []
for macro in to_load:
if macro.name in macro_dict:
print 'Name conflict in %s! "%s" already in use, not loading.' % (macro.filename, macro.name)
elif macro.short_name and macro.short_name in macro_dict:
print 'Name conflict in %s! "%s" already in use, not loading.' % (macro.filename, macro.short_name)
elif macro.file_name in macro_dict:
print 'Name conflict in %s! "%s" already in use, not loading.' % (macro.filename, macro.file_name)
else:
macro_dict[macro.name] = macro
macro_dict[macro.file_name] = macro
if macro.short_name:
macro_dict[macro.short_name] = macro
loaded_macros.append(macro)
print 'Loaded "%s"' % macro
for macro in int_to_load:
if macro.name in int_macro_dict:
print 'Name conflict in %s! "%s" already in use, not loading.' % (macro.filename, macro.name)
elif macro.short_name and macro.short_name in int_macro_dict:
print 'Name conflict in %s! "%s" already in use, not loading.' % (macro.filename, macro.short_name)
elif macro.file_name in int_macro_dict:
print 'Name conflict in %s! "%s" already in use, not loading.' % (macro.filename, macro.file_name)
else:
int_macro_dict[macro.name] = macro
int_macro_dict[macro.file_name] = macro
if macro.short_name:
int_macro_dict[macro.short_name] = macro
loaded_int_macros.append(macro)
print 'Loaded "%s"' % macro
def run_macro(line):
"""
Run a macro
Usage: run_macro <macro name or macro short name>
"""
global macro_dict
global loaded_macros
args = shlex.split(line)
if not args:
raise PappyException('You must give a macro to run. You can give its short name, or the name in the filename.')
mname = args[0]
if mname not in macro_dict:
raise PappyException('%s not a loaded macro' % mname)
macro = macro_dict[mname]
macro.execute(args[1:])
def run_int_macro(line):
"""
Activate an intercepting macro
Usage: run_int_macro <macro name or macro short name>
Macro can be stopped with stop_int_macro
"""
global int_macro_dict
global loaded_int_macros
args = shlex.split(line)
if len(args) == 0:
raise PappyException('You must give an intercepting macro to run. You can give its short name, or the name in the filename.')
if args[0] not in int_macro_dict:
raise PappyException('%s not a loaded intercepting macro' % line)
macro = int_macro_dict[args[0]]
macro.init(args[1:])
add_intercepting_macro(macro.name, macro)
print '"%s" started' % macro.name
def stop_int_macro(line):
"""
Stop a running intercepting macro
Usage: stop_int_macro <macro name or macro short name>
"""
global int_macro_dict
global loaded_int_macros
if not line:
raise PappyException('You must give an intercepting macro to run. You can give its short name, or the name in the filename.')
if line not in int_macro_dict:
raise PappyException('%s not a loaded intercepting macro' % line)
macro = int_macro_dict[line]
remove_intercepting_macro(macro.name)
print '"%s" stopped' % macro.name
def list_int_macros(line):
"""
List all active/inactive intercepting macros
"""
global int_macro_dict
global loaded_int_macros
running = []
not_running = []
for macro in loaded_int_macros:
if macro.name in active_intercepting_macros():
running.append(macro)
else:
not_running.append(macro)
if not running and not not_running:
print 'No loaded intercepting macros'
if running:
print 'Active intercepting macros:'
for m in running:
print ' %s' % m
if not_running:
print 'Inactive intercepting macros:'
for m in not_running:
print ' %s' % m
@crochet.wait_for(timeout=None)
@defer.inlineCallbacks
def generate_macro(line):
"""
Generate a macro script with request objects
Usage: generate_macro <name> [reqs]
"""
if line == '':
raise PappyException('Macro name is required')
args = shlex.split(line)
name = args[0]
if len(args) > 1:
reqs = yield load_reqlist(args[1])
else:
reqs = []
script_str = macro_from_requests(reqs)
fname = 'macro_%s.py' % name
with open(fname, 'wc') as f:
f.write(script_str)
print 'Wrote script to %s' % fname
def generate_int_macro(line):
"""
Generate an intercepting macro script
Usage: generate_int_macro <name>
"""
if line == '':
raise PappyException('Macro name is required')
args = shlex.split(line)
name = args[0]
script_str = gen_imacro()
fname = 'int_%s.py' % name
with open(fname, 'wc') as f:
f.write(script_str)
print 'Wrote script to %s' % fname
@crochet.wait_for(timeout=None)
@defer.inlineCallbacks
def rpy(line):
"""
Copy python object definitions of requests.
Usage: rpy <reqs>
"""
reqs = yield load_reqlist(line)
for req in reqs:
print pappyproxy.macros.req_obj_def(req)
###############
## Plugin hooks
def load_cmds(cmd):
cmd.set_cmds({
'rpy': (rpy, None),
'generate_int_macro': (generate_int_macro, None),
'generate_macro': (generate_macro, None),
'list_int_macros': (list_int_macros, None),
'stop_int_macro': (stop_int_macro, None),
'run_int_macro': (run_int_macro, None),
'run_macro': (run_macro, None),
'load_macros': (load_macros_cmd, None),
})
cmd.add_aliases([
#('rpy', ''),
('generate_int_macro', 'gima'),
('generate_macro', 'gma'),
('list_int_macros', 'lsim'),
('stop_int_macro', 'sim'),
('run_int_macro', 'rim'),
('run_macro', 'rma'),
('load_macros', 'lma'),
])

@ -0,0 +1,243 @@
import crochet
import curses
import os
import pappyproxy
import shlex
import subprocess
import tempfile
from pappyproxy.util import PappyException
from pappyproxy.macros import InterceptMacro
from pappyproxy.http import Request, Response
from pappyproxy.plugin import add_intercepting_macro, remove_intercepting_macro
from pappyproxy import comm, config
from twisted.internet import defer
PLUGIN_ID="manglecmds"
edit_queue = []
class MangleInterceptMacro(InterceptMacro):
"""
A class representing a macro that modifies requests as they pass through the
proxy
"""
def __init__(self):
InterceptMacro.__init__(self)
self.name = 'Pappy Interceptor Macro'
self.intercept_requests = False
self.intercept_responses = False
self.async_req = True
self.async_rsp = True
def __repr__(self):
return "<MangleInterceptingMacro>" % self.name
@defer.inlineCallbacks
def async_mangle_request(self, request):
# This function gets called to mangle/edit requests passed through the proxy
retreq = request
# Write original request to the temp file
with tempfile.NamedTemporaryFile(delete=False) as tf:
tfName = tf.name
tf.write(request.full_request)
# Have the console edit the file
yield edit_file(tfName)
# Create new mangled request from edited file
with open(tfName, 'r') as f:
text = f.read()
os.remove(tfName)
# Check if dropped
if text == '':
pappyproxy.proxy.log('Request dropped!')
defer.returnValue(None)
mangled_req = Request(text, update_content_length=True)
mangled_req.port = request.port
mangled_req.is_ssl = request.is_ssl
# Check if it changed
if mangled_req.full_request != request.full_request:
retreq = mangled_req
defer.returnValue(retreq)
@defer.inlineCallbacks
def async_mangle_response(self, request):
# This function gets called to mangle/edit respones passed through the proxy
retrsp = request.response
# Write original response to the temp file
with tempfile.NamedTemporaryFile(delete=False) as tf:
tfName = tf.name
tf.write(request.response.full_response)
# Have the console edit the file
yield edit_file(tfName, front=True)
# Create new mangled response from edited file
with open(tfName, 'r') as f:
text = f.read()
os.remove(tfName)
# Check if dropped
if text == '':
pappyproxy.proxy.log('Response dropped!')
defer.returnValue(None)
mangled_rsp = Response(text, update_content_length=True)
if mangled_rsp.full_response != request.response.full_response:
mangled_rsp.unmangled = request.response
retrsp = mangled_rsp
defer.returnValue(retrsp)
###############
## Helper funcs
def edit_file(fname, front=False):
global edit_queue
# Adds the filename to the edit queue. Returns a deferred that is fired once
# the file is edited and the editor is closed
d = defer.Deferred()
if front:
edit_queue = [(fname, d)] + edit_queue
else:
edit_queue.append((fname, d))
return d
@crochet.wait_for(timeout=None)
@defer.inlineCallbacks
def check_reqid(reqid):
# Used for the repeater command. Must not be async
try:
yield pappyproxy.http.Request.load_request(reqid)
except:
raise PappyException('"%s" is not a valid request id' % reqid)
defer.returnValue(None)
def start_editor(reqid):
script_loc = os.path.join(config.PAPPY_DIR, "plugins", "vim_repeater", "repeater.vim")
#print "RepeaterSetup %d %d"%(reqid, comm_port)
subprocess.call(["vim", "-S", script_loc, "-c", "RepeaterSetup %s %d"%(reqid, comm.comm_port)])
####################
## Command functions
def repeater(line):
"""
Open a request in the repeater
Usage: repeater <reqid>
"""
# This is not async on purpose. start_editor acts up if this is called
# with inline callbacks. As a result, check_reqid and get_unmangled
# cannot be async
args = shlex.split(line)
reqid = args[0]
check_reqid(reqid)
start_editor(reqid)
def intercept(line):
"""
Intercept requests and/or responses and edit them with before passing them along
Usage: intercept <reqid>
"""
global edit_queue
args = shlex.split(line)
intercept_requests = False
intercept_responses = False
req_names = ('req', 'request', 'requests')
rsp_names = ('rsp', 'response', 'responses')
if any(a in req_names for a in args):
intercept_requests = True
if any(a in rsp_names for a in args):
intercept_responses = True
if intercept_requests and intercept_responses:
intercept_str = 'Requests and responses'
elif intercept_requests:
intercept_str = 'Requests'
elif intercept_responses:
intercept_str = 'Responses'
else:
intercept_str = 'NOTHING'
mangle_macro = MangleInterceptMacro()
mangle_macro.intercept_requests = intercept_requests
mangle_macro.intercept_responses = intercept_responses
add_intercepting_macro('pappy_intercept', mangle_macro)
## Interceptor loop
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
try:
editnext = False
stdscr.nodelay(True)
while True:
stdscr.addstr(0, 0, "Currently intercepting: %s" % intercept_str)
stdscr.clrtoeol()
stdscr.addstr(1, 0, "%d item(s) in queue." % len(edit_queue))
stdscr.clrtoeol()
if editnext:
stdscr.addstr(2, 0, "Waiting for next item... Press 'q' to quit or 'b' to quit waiting")
else:
stdscr.addstr(2, 0, "Press 'n' to edit the next item or 'q' to quit interceptor.")
stdscr.clrtoeol()
c = stdscr.getch()
if c == ord('q'):
break
elif c == ord('n'):
editnext = True
elif c == ord('b'):
editnext = False
if editnext and edit_queue:
editnext = False
(to_edit, deferred) = edit_queue.pop(0)
editor = 'vi'
if 'EDITOR' in os.environ:
editor = os.environ['EDITOR']
subprocess.call([editor, to_edit])
stdscr.clear()
deferred.callback(None)
finally:
curses.nocbreak()
stdscr.keypad(0)
curses.echo()
curses.endwin()
try:
remove_intercepting_macro('pappy_intercept')
except PappyException:
pass
# Send remaining requests along
while len(edit_queue) > 0:
(fname, deferred) = edit_queue.pop(0)
deferred.callback(None)
###############
## Plugin hooks
def load_cmds(cmd):
cmd.set_cmds({
'intercept': (intercept, None),
'repeater': (repeater, None),
})
cmd.add_aliases([
('intercept', 'ic'),
('repeater', 'rp'),
])

@ -0,0 +1,85 @@
import crochet
import pappyproxy
import shlex
from pappyproxy.console import confirm, load_reqlist
from pappyproxy.util import PappyException
from twisted.internet import defer
def clrmem(line):
"""
Delete all in-memory only requests
Usage: clrmem
"""
to_delete = list(pappyproxy.context.Context.in_memory_requests)
for r in to_delete:
pappyproxy.context.Context.remove_request(r)
def gencerts(line):
"""
Generate CA cert and private CA file
Usage: gencerts [/path/to/put/certs/in]
"""
dest_dir = line or pappyproxy.config.CERT_DIR
message = "This will overwrite any existing certs in %s. Are you sure?" % dest_dir
if not confirm(message, 'n'):
return False
print "Generating certs to %s" % dest_dir
pappyproxy.proxy.generate_ca_certs(dest_dir)
def log(line):
"""
Display the log in real time. Honestly it probably doesn't work.
Usage: log [verbosity (default is 1)]
verbosity=1: Show connections as they're made/lost, some additional info
verbosity=3: Show full requests/responses as they are processed by the proxy
"""
try:
verbosity = int(line.strip())
except:
verbosity = 1
pappyproxy.config.DEBUG_VERBOSITY = verbosity
raw_input()
pappyproxy.config.DEBUG_VERBOSITY = 0
@crochet.wait_for(timeout=None)
@defer.inlineCallbacks
def export(line):
"""
Write the full request/response of a request/response to a file.
Usage: export [req|rsp] <reqid(s)>
"""
args = shlex.split(line)
if len(args) < 2:
print 'Requires req/rsp and and request id(s)'
defer.returnValue(None)
if args[0] not in ('req', 'rsp'):
raise PappyException('Request or response not specified')
reqs = yield load_reqlist(args[1])
for req in reqs:
try:
if args[0] == 'req':
fname = 'req_%s.txt'%req.reqid
with open(fname, 'w') as f:
f.write(req.full_request)
print 'Full request written to %s' % fname
elif args[0] == 'rsp':
fname = 'rsp_%s.txt'%req.reqid
with open(fname, 'w') as f:
f.write(req.full_response)
print 'Full response written to %s' % fname
except PappyException as e:
print 'Unable to export %s: %s' % (req.reqid, e)
def load_cmds(cmd):
cmd.set_cmds({
'clrmem': (clrmem, None),
'gencerts': (gencerts, None),
'export': (export, None),
'log': (log, None),
})
cmd.add_aliases([
#('rpy', ''),
])

@ -0,0 +1,102 @@
import crochet
import pappyproxy
import shlex
from pappyproxy.plugin import main_context
from pappyproxy.console import load_reqlist
from pappyproxy.util import PappyException
from twisted.internet import defer
@crochet.wait_for(timeout=None)
@defer.inlineCallbacks
def tag(line):
"""
Add a tag to requests.
Usage: tag <tag> [request ids]
You can tag as many requests as you want at the same time. If no
ids are given, the tag will be applied to all in-context requests.
"""
args = shlex.split(line)
if len(args) == 0:
raise PappyException('Tag name is required')
tag = args[0]
if len(args) > 1:
reqs = yield load_reqlist(args[1], False)
ids = [r.reqid for r in reqs]
print 'Tagging %s with %s' % (', '.join(ids), tag)
else:
print "Tagging all in-context requests with %s" % tag
reqs = main_context().active_requests
for req in reqs:
if tag not in req.tags:
req.tags.append(tag)
if req.saved:
yield req.async_save()
add_req(req)
else:
print 'Request %s already has tag %s' % (req.reqid, tag)
@crochet.wait_for(timeout=None)
@defer.inlineCallbacks
def untag(line):
"""
Remove a tag from requests
Usage: untag <tag> <request ids>
You can provide as many request ids as you want and the tag will
be removed from all of them. If no ids are given, the tag will
be removed from all in-context requests.
"""
args = shlex.split(line)
if len(args) == 0:
raise PappyException("Tag and request ids are required")
tag = args[0]
ids = []
if len(args) > 1:
reqs = yield load_reqlist(args[1], False)
ids = [r.reqid for r in reqs]
else:
print "Untagging all in-context requests with tag %s" % tag
reqs = main_context().active_requests
for req in reqs:
if tag in req.tags:
req.tags.remove(tag)
if req.saved:
yield req.async_save()
if ids:
print 'Tag %s removed from %s' % (tag, ', '.join(ids))
@crochet.wait_for(timeout=None)
@defer.inlineCallbacks
def clrtag(line):
"""
Clear all the tags from requests
Usage: clrtag <request ids>
"""
args = shlex.split(line)
if len(args) == 0:
raise PappyException('No request IDs given')
reqs = yield load_reqlist(args[0], False)
for req in reqs:
if req.tags:
req.tags = []
print 'Tags cleared from request %s' % (req.reqid)
if req.saved:
yield req.async_save()
###############
## Plugin hooks
def load_cmds(cmd):
cmd.set_cmds({
'clrtag': (clrtag, None),
'untag': (untag, None),
'tag': (tag, None),
})
cmd.add_aliases([
#('rpy', ''),
])

@ -0,0 +1,328 @@
import crochet
import datetime
import pappyproxy
import shlex
from pappyproxy.console import load_reqlist, print_table, print_requests
from pappyproxy.util import PappyException
from pappyproxy.plugin import main_context
from pappyproxy.http import Request
from twisted.internet import defer
###################
## Helper functions
def view_full_message(request, headers_only=False):
if headers_only:
print request.headers_section_pretty
else:
print request.full_message_pretty
def print_request_extended(request):
# Prints extended info for the request
title = "Request Info (reqid=%s)" % request.reqid
print title
print '-'*len(title)
reqlen = len(request.body)
reqlen = '%d bytes' % reqlen
rsplen = 'No response'
mangle_str = 'Nothing mangled'
if request.unmangled:
mangle_str = 'Request'
if request.response:
response_code = str(request.response.response_code) + \
' ' + request.response.response_text
rsplen = len(request.response.body)
rsplen = '%d bytes' % rsplen
if request.response.unmangled:
if mangle_str == 'Nothing mangled':
mangle_str = 'Response'
else:
mangle_str += ' and Response'
else:
response_code = ''
time_str = '--'
if request.time_start and request.time_end:
time_delt = request.time_end - request.time_start
time_str = "%.2f sec" % time_delt.total_seconds()
if request.is_ssl:
is_ssl = 'YES'
else:
is_ssl = 'NO'
if request.time_start:
time_made_str = request.time_start.strftime('%a, %b %d, %Y, %I:%M:%S %p')
else:
time_made_str = '--'
print 'Made on %s' % time_made_str
print 'ID: %s' % request.reqid
print 'Verb: %s' % request.verb
print 'Host: %s' % request.host
print 'Path: %s' % request.full_path
print 'Status Code: %s' % response_code
print 'Request Length: %s' % reqlen
print 'Response Length: %s' % rsplen
if request.response and request.response.unmangled:
print 'Unmangled Response Length: %s bytes' % len(request.response.unmangled.full_response)
print 'Time: %s' % time_str
print 'Port: %s' % request.port
print 'SSL: %s' % is_ssl
print 'Mangled: %s' % mangle_str
print 'Tags: %s' % (', '.join(request.tags))
if request.plugin_data:
print 'Plugin Data: %s' % (request.plugin_data)
def get_site_map(reqs):
# Takes in a list of requests and returns a tree representing the site map
paths_set = set()
for req in reqs:
paths_set.add(req.path_tuple)
paths = sorted(list(paths_set))
return paths
def print_tree(tree):
# Prints a tree. Takes in a sorted list of path tuples
_print_tree_helper(tree, 0, [])
def _get_tree_prefix(depth, print_bars, last):
if depth == 0:
return u''
else:
ret = u''
pb = print_bars + [True]
for i in range(depth):
if pb[i]:
ret += u'\u2502 '
else:
ret += u' '
if last:
ret += u'\u2514\u2500\u2500 '
else:
ret += u'\u251c\u2500\u2500 '
return ret
def _print_tree_helper(tree, depth, print_bars):
# Takes in a tree and prints it at the given depth
if tree == [] or tree == [()]:
return
while tree[0] == ():
tree = tree[1:]
if tree == [] or tree == [()]:
return
if len(tree) == 1 and len(tree[0]) == 1:
print _get_tree_prefix(depth, print_bars + [False], True) + tree[0][0]
return
curkey = tree[0][0]
subtree = []
for row in tree:
if row[0] != curkey:
if curkey == '':
curkey = '/'
print _get_tree_prefix(depth, print_bars, False) + curkey
if depth == 0:
_print_tree_helper(subtree, depth+1, print_bars + [False])
else:
_print_tree_helper(subtree, depth+1, print_bars + [True])
curkey = row[0]
subtree = []
subtree.append(row[1:])
if curkey == '':
curkey = '/'
print _get_tree_prefix(depth, print_bars, True) + curkey
_print_tree_helper(subtree, depth+1, print_bars + [False])
####################
## Command functions
def list_reqs(line):
"""
List the most recent in-context requests. By default shows the most recent 25
Usage: list [a|num]
If `a` is given, all the in-context requests are shown. If a number is given,
that many requests will be shown.
"""
args = shlex.split(line)
if len(args) > 0:
if args[0][0].lower() == 'a':
print_count = -1
else:
try:
print_count = int(args[0])
except:
print "Please enter a valid argument for list"
return
else:
print_count = 25
def key_reqtime(req):
if req.time_start is None:
return -1
else:
return (req.time_start-datetime.datetime(1970,1,1)).total_seconds()
to_print = sorted(main_context().active_requests, key=key_reqtime, reverse=True)
if print_count > 0:
to_print = to_print[:print_count]
print_requests(to_print)
@crochet.wait_for(timeout=None)
@defer.inlineCallbacks
def view_request_info(line):
"""
View information about request
Usage: view_request_info <reqid> [u]
If 'u' is given as an additional argument, the unmangled version
of the request will be displayed.
"""
args = shlex.split(line)
reqids = args[0]
reqs = yield load_reqlist(reqids)
for req in reqs:
print ''
print_request_extended(req)
print ''
@crochet.wait_for(timeout=None)
@defer.inlineCallbacks
def view_request_headers(line):
"""
View the headers of the request
Usage: view_request_headers <reqid> [u]
If 'u' is given as an additional argument, the unmangled version
of the request will be displayed.
"""
args = shlex.split(line)
reqid = args[0]
reqs = yield load_reqlist(reqid)
for req in reqs:
if len(reqs) > 1:
print 'Request %s:' % req.reqid
print ''
view_full_message(req, True)
if len(reqs) > 1:
print '-'*30
@crochet.wait_for(timeout=None)
@defer.inlineCallbacks
def view_full_request(line):
"""
View the full data of the request
Usage: view_full_request <reqid> [u]
If 'u' is given as an additional argument, the unmangled version
of the request will be displayed.
"""
args = shlex.split(line)
reqid = args[0]
reqs = yield load_reqlist(reqid)
for req in reqs:
if len(reqs) > 1:
print 'Request %s:' % req.reqid
print ''
view_full_message(req)
if len(reqs) > 1:
print '-'*30
@crochet.wait_for(timeout=None)
@defer.inlineCallbacks
def view_response_headers(line):
"""
View the headers of the response
Usage: view_response_headers <reqid>
"""
reqs = yield load_reqlist(line)
for req in reqs:
if req.response:
if len(reqs) > 1:
print '-'*15 + (' %s ' % req.reqid) + '-'*15
view_full_message(req.response, True)
else:
print "Request %s does not have a response" % req.reqid
@crochet.wait_for(timeout=None)
@defer.inlineCallbacks
def view_full_response(line):
"""
View the full data of the response associated with a request
Usage: view_full_response <reqid>
"""
reqs = yield load_reqlist(line)
for req in reqs:
if req.response:
if len(reqs) > 1:
print '-'*15 + (' %s ' % req.reqid) + '-'*15
view_full_message(req.response)
else:
print "Request %s does not have a response" % req.reqid
@crochet.wait_for(timeout=None)
@defer.inlineCallbacks
def dump_response(line):
"""
Dump the data of the response to a file.
Usage: dump_response <id> <filename>
"""
# dump the data of a response
args = shlex.split(line)
reqid = args[0]
req = yield Request.load_request(reqid)
rsp = req.response
if len(args) >= 2:
fname = args[1]
else:
fname = req.path.split('/')[-1]
with open(fname, 'w') as f:
f.write(rsp.body)
print 'Response data written to %s' % fname
def site_map(line):
"""
Print the site map. Only includes requests in the current context.
Usage: site_map
"""
to_print = [r for r in main_context().active_requests if not r.response or r.response.response_code != 404]
tree = get_site_map(to_print)
print_tree(tree)
###############
## Plugin hooks
def load_cmds(cmd):
cmd.set_cmds({
'list': (list_reqs, None),
'view_request_info': (view_request_info, None),
'view_request_headers': (view_request_headers, None),
'view_full_request': (view_full_request, None),
'view_response_headers': (view_response_headers, None),
'view_full_response': (view_full_response, None),
'site_map': (site_map, None),
'dump_response': (dump_response, None),
})
cmd.add_aliases([
('list', 'ls'),
('view_request_info', 'viq'),
('view_request_headers', 'vhq'),
('view_full_request', 'vfq'),
('view_response_headers', 'vhs'),
('site_map', 'sm'),
('view_full_response', 'vfs'),
#('dump_response', 'dr'),
])

@ -0,0 +1,135 @@
import base64
import vim
import sys
import socket
import json
class CommError(Exception):
pass
def escape(s):
return s.replace("'", "''")
def communicate(data):
global PAPPY_PORT
# Submits data to the comm port of the proxy
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1', int(vim.eval('s:commport'))))
datastr = json.dumps(data)
# Send our data
total_sent = 0
while total_sent < len(data):
sent = s.send(datastr[total_sent:])
assert sent != 0
total_sent += sent
s.send('\n')
# Get our response
retstr = ''
c = ''
while c != '\n':
retstr = retstr + c
c = s.recv(1)
assert c != ''
result = json.loads(retstr)
if not result['success']:
vim.command("echoerr '%s'" % escape(result['message']))
raise CommError(result['message'])
return result
def read_line(conn):
data = ''
c = ''
while c != '\n':
data = data + c
c = conn.read(1)
return data
def run_command(command):
funcs = {
"setup": set_up_windows,
"submit": submit_current_buffer,
}
if command in funcs:
funcs[command]()
def set_buffer_content(buf, text):
buf[:] = None
first = True
for l in text.split('\n'):
if first:
buf[0] = l
first = False
else:
buf.append(l)
def set_up_windows():
reqid = vim.eval("a:2")
comm_port = vim.eval("a:3")
vim.command("let s:commport=%d"%int(comm_port))
# Get the left buffer
vim.command("new")
vim.command("only")
b2 = vim.current.buffer
vim.command("let s:b2=bufnr('$')")
# Vsplit new file
vim.command("vnew")
b1 = vim.current.buffer
vim.command("let s:b1=bufnr('$')")
# Get the request
comm_data = {"action": "get_request", "reqid": reqid}
try:
reqdata = communicate(comm_data)
except CommError:
return
comm_data = {"action": "get_response", "reqid": reqid}
try:
rspdata = communicate(comm_data)
except CommError:
return
# Set up the buffers
set_buffer_content(b1, base64.b64decode(reqdata['full_message']))
if 'full_message' in rspdata:
set_buffer_content(b2, base64.b64decode(rspdata['full_message']))
# Save the port, ssl, host setting
vim.command("let s:repport=%d" % int(reqdata['port']))
vim.command("let s:rephost='%s'" % escape(reqdata['host']))
if reqdata['is_ssl']:
vim.command("let s:repisssl=1")
else:
vim.command("let s:repisssl=0")
def submit_current_buffer():
curbuf = vim.current.buffer
b2_id = int(vim.eval("s:b2"))
b2 = vim.buffers[b2_id]
vim.command("let s:b1=bufnr('$')")
vim.command("only")
vim.command("rightbelow vertical new")
vim.command("b %d" % b2_id)
vim.command("wincmd h")
full_request = '\n'.join(curbuf)
commdata = {'action': 'submit',
'full_message': base64.b64encode(full_request),
'port': int(vim.eval("s:repport")),
'host': vim.eval("s:rephost")}
if vim.eval("s:repisssl") == '1':
commdata["is_ssl"] = True
else:
commdata["is_ssl"] = False
result = communicate(commdata)
set_buffer_content(b2, base64.b64decode(result['response']['full_message']))
# (left, right) = set_up_windows()
# set_buffer_content(left, 'Hello\nWorld')
# set_buffer_content(right, 'Hello\nOther\nWorld')
#print "Arg is %s" % vim.eval("a:arg")
run_command(vim.eval("a:1"))

@ -0,0 +1,17 @@
if !has('python')
echo "Vim must support python in order to use the repeater"
finish
endif
let s:pyscript = resolve(expand('<sfile>:p:h') . '/repeater.py')
function! RepeaterAction(...)
execute 'pyfile ' . s:pyscript
endfunc
command! -nargs=* RepeaterSetup call RepeaterAction('setup', <f-args>)
command! RepeaterSubmitBuffer call RepeaterAction('submit')
" Bind forward to <leader>f
nnoremap <leader>f :RepeaterSubmitBuffer<CR>

@ -1,30 +1,18 @@
import copy import copy
import datetime import datetime
import gzip
import os import os
import random import random
import re
import schema.update
import shutil
import string
import StringIO
import sys
import urlparse
import zlib
from OpenSSL import SSL from OpenSSL import SSL
from OpenSSL import crypto
from pappyproxy import config from pappyproxy import config
from pappyproxy import console
from pappyproxy import context from pappyproxy import context
from pappyproxy import http from pappyproxy import http
from pappyproxy import mangle from pappyproxy.util import PappyException, printable_data
from pappyproxy.util import PappyException from twisted.internet import defer
from twisted.enterprise import adbapi
from twisted.internet import reactor, ssl from twisted.internet import reactor, ssl
from twisted.internet.protocol import ClientFactory, ServerFactory from twisted.internet.protocol import ClientFactory, ServerFactory
from twisted.protocols.basic import LineReceiver from twisted.protocols.basic import LineReceiver
from twisted.internet import defer
from OpenSSL import crypto
next_connection_id = 1 next_connection_id = 1
@ -43,7 +31,7 @@ def add_intercepting_macro(key, macro, int_macro_dict):
def remove_intercepting_macro(key, int_macro_dict): def remove_intercepting_macro(key, int_macro_dict):
if not key in int_macro_dict: if not key in int_macro_dict:
raise PappyException('Macro with key %s not loaded' % key) raise PappyException('Macro with key %s not currently running' % key)
del int_macro_dict[key] del int_macro_dict[key]
def log(message, id=None, symbol='*', verbosity_level=1): def log(message, id=None, symbol='*', verbosity_level=1):
@ -94,6 +82,12 @@ class ProxyClient(LineReceiver):
line = '' line = ''
self._response_obj.add_line(line) self._response_obj.add_line(line)
self.log(line, symbol='r<', verbosity_level=3) self.log(line, symbol='r<', verbosity_level=3)
if self.factory.stream_response:
self.log('Returning line back through stream')
self.factory.return_transport.write(line+'\r\n')
else:
self.log('Not streaming, not returning')
self.log(self.factory.stream_response)
if self._response_obj.headers_complete: if self._response_obj.headers_complete:
if self._response_obj.complete: if self._response_obj.complete:
self.handle_response_end() self.handle_response_end()
@ -103,9 +97,12 @@ class ProxyClient(LineReceiver):
def rawDataReceived(self, *args, **kwargs): def rawDataReceived(self, *args, **kwargs):
data = args[0] data = args[0]
self.log('Returning data back through stream')
if self.factory.stream_response:
self.factory.return_transport.write(data)
if not self._response_obj.complete: if not self._response_obj.complete:
if data: if data:
s = console.printable_data(data) s = printable_data(data)
dlines = s.split('\n') dlines = s.split('\n')
for l in dlines: for l in dlines:
self.log(l, symbol='<rd', verbosity_level=3) self.log(l, symbol='<rd', verbosity_level=3)
@ -127,23 +124,29 @@ class ProxyClient(LineReceiver):
sendreq = self.request sendreq = self.request
if context.in_scope(sendreq): if context.in_scope(sendreq):
to_mangle = copy.copy(self.factory.intercepting_macros).iteritems()
if self.factory.save_all: if self.factory.save_all:
yield sendreq.async_deep_save() # It isn't the actual time, but this should work in case
# we do an 'ls' before it gets a real time saved
sendreq.time_start = datetime.datetime.now()
if self.factory.stream_response and not to_mangle:
self.request.async_deep_save()
else:
yield self.request.async_deep_save()
## Run intercepting macros ## Run intercepting macros
# if we don't copy it, when we delete a macro from the console, # if we don't copy it, when we delete a macro from the console,
# we get a crash. We do a shallow copy to keep the macro # we get a crash. We do a shallow copy to keep the macro
# instances the same. # instances the same.
to_mangle = copy.copy(self.factory.intercepting_macros).iteritems()
for k, macro in to_mangle: for k, macro in to_mangle:
if macro.do_req: if macro.intercept_requests:
if macro.async_req: if macro.async_req:
sendreq = yield macro.async_mangle_request(sendreq) sendreq = yield macro.async_mangle_request(sendreq)
else: else:
sendreq = macro.mangle_request(sendreq) sendreq = macro.mangle_request(sendreq)
if sendreq is None: if sendreq is None:
self.log('Request dropped, losing connection')
self.transport.loseConnection() self.transport.loseConnection()
self.request = None self.request = None
self.data_defer.callback(None) self.data_defer.callback(None)
@ -153,7 +156,6 @@ class ProxyClient(LineReceiver):
if sendreq != self.request: if sendreq != self.request:
sendreq.unmangled = self.request sendreq.unmangled = self.request
if self.factory.save_all: if self.factory.save_all:
yield sendreq.async_deep_save() yield sendreq.async_deep_save()
else: else:
@ -174,6 +176,7 @@ class ProxyClient(LineReceiver):
def handle_response_end(self, *args, **kwargs): def handle_response_end(self, *args, **kwargs):
self.log("Remote response finished, returning data to original stream") self.log("Remote response finished, returning data to original stream")
self.request.response = self._response_obj self.request.response = self._response_obj
self.log('Response ended, losing connection')
self.transport.loseConnection() self.transport.loseConnection()
assert self._response_obj.full_response assert self._response_obj.full_response
self.factory.return_request_pair(self.request) self.factory.return_request_pair(self.request)
@ -181,15 +184,17 @@ class ProxyClient(LineReceiver):
class ProxyClientFactory(ClientFactory): class ProxyClientFactory(ClientFactory):
def __init__(self, request, save_all=False): def __init__(self, request, save_all=False, stream_response=False,
return_transport=None):
self.request = request self.request = request
#self.proxy_server = None
self.intercepting_macros = {}
self.connection_id = -1 self.connection_id = -1
self.data_defer = defer.Deferred() self.data_defer = defer.Deferred()
self.start_time = datetime.datetime.now() self.start_time = datetime.datetime.now()
self.end_time = None self.end_time = None
self.save_all = save_all self.save_all = save_all
self.stream_response = stream_response
self.return_transport = return_transport
self.intercepting_macros = {}
def log(self, message, symbol='*', verbosity_level=1): def log(self, message, symbol='*', verbosity_level=1):
log(message, id=self.connection_id, symbol=symbol, verbosity_level=verbosity_level) log(message, id=self.connection_id, symbol=symbol, verbosity_level=verbosity_level)
@ -208,22 +213,24 @@ class ProxyClientFactory(ClientFactory):
@defer.inlineCallbacks @defer.inlineCallbacks
def return_request_pair(self, request): def return_request_pair(self, request):
self.end_time = datetime.datetime.now() self.end_time = datetime.datetime.now()
log_request(console.printable_data(request.response.full_response), id=self.connection_id, symbol='<m', verbosity_level=3) log_request(printable_data(request.response.full_response), id=self.connection_id, symbol='<m', verbosity_level=3)
request.time_start = self.start_time request.time_start = self.start_time
request.time_end = self.end_time request.time_end = self.end_time
if context.in_scope(request): if context.in_scope(request):
to_mangle = copy.copy(self.intercepting_macros).iteritems()
if self.save_all: if self.save_all:
if self.stream_response and not to_mangle:
request.async_deep_save()
else:
yield request.async_deep_save() yield request.async_deep_save()
# if we don't copy it, when we delete a macro from the console, # if we don't copy it, when we delete a macro from the console,
# we get a crash. We do a shallow copy to keep the macro # we get a crash. We do a shallow copy to keep the macro
# instances the same. # instances the same.
to_mangle = copy.copy(self.intercepting_macros).iteritems()
old_rsp = request.response old_rsp = request.response
for k, macro in to_mangle: for k, macro in to_mangle:
if macro.do_rsp: if macro.intercept_responses:
if macro.async_rsp: if macro.async_rsp:
mangled_rsp = yield macro.async_mangle_response(request) mangled_rsp = yield macro.async_mangle_response(request)
else: else:
@ -234,6 +241,7 @@ class ProxyClientFactory(ClientFactory):
self.data_defer.callback(request) self.data_defer.callback(request)
if self.save_all: if self.save_all:
yield request.async_deep_save() yield request.async_deep_save()
self.log("Response dropped, losing connection")
self.transport.loseConnection() self.transport.loseConnection()
defer.returnValue(None) defer.returnValue(None)
@ -241,15 +249,11 @@ class ProxyClientFactory(ClientFactory):
if request.response != old_rsp: if request.response != old_rsp:
request.response.unmangled = old_rsp request.response.unmangled = old_rsp
if self.save_all: if self.save_all:
yield request.async_deep_save() yield request.async_deep_save()
# re-check after all the mangling
context.filter_recheck()
if request.response: if request.response:
log_request(console.printable_data(request.response.full_response), log_request(printable_data(request.response.full_response),
id=self.connection_id, symbol='<', verbosity_level=3) id=self.connection_id, symbol='<', verbosity_level=3)
else: else:
self.log("Response out of scope, passing along unmangled") self.log("Response out of scope, passing along unmangled")
@ -340,10 +344,19 @@ class ProxyServer(LineReceiver):
if self._forward: if self._forward:
self.log("Forwarding to %s on %d" % (self._request_obj.host, self._request_obj.port)) self.log("Forwarding to %s on %d" % (self._request_obj.host, self._request_obj.port))
if not self.factory.intercepting_macros:
stream = True
else:
# We only want to call send_response_back if we're not streaming
stream = False
self.log('Creating client factory, stream=%s' % stream)
factory = ProxyClientFactory(self._request_obj, factory = ProxyClientFactory(self._request_obj,
save_all=self.factory.save_all) save_all=self.factory.save_all,
stream_response=stream,
return_transport=self.transport)
factory.intercepting_macros = self.factory.intercepting_macros factory.intercepting_macros = self.factory.intercepting_macros
factory.connection_id = self.connection_id factory.connection_id = self.connection_id
if not stream:
factory.data_defer.addCallback(self.send_response_back) factory.data_defer.addCallback(self.send_response_back)
if self._request_obj.is_ssl: if self._request_obj.is_ssl:
self.log("Accessing over SSL...", verbosity_level=3) self.log("Accessing over SSL...", verbosity_level=3)
@ -364,6 +377,7 @@ class ProxyServer(LineReceiver):
def send_response_back(self, response): def send_response_back(self, response):
if response is not None: if response is not None:
self.transport.write(response.response.full_response) self.transport.write(response.response.full_response)
self.log("Response sent back, losing connection")
self.transport.loseConnection() self.transport.loseConnection()
def connectionLost(self, reason): def connectionLost(self, reason):

@ -19,9 +19,6 @@ update_queries = [
ALTER TABLE requests ADD COLUMN is_ssl INTEGER; ALTER TABLE requests ADD COLUMN is_ssl INTEGER;
""", """,
"""
UPDATE schema_meta SET version=2;
""",
] ]
@defer.inlineCallbacks @defer.inlineCallbacks
@ -29,9 +26,30 @@ def update(dbpool):
for query in update_queries: for query in update_queries:
yield dbpool.runQuery(query) yield dbpool.runQuery(query)
# Load each request and save them again for any request that specified a port # Update metadata for each request
# or protocol in the host header. reqrows = yield dbpool.runQuery(
http.init(dbpool) """
reqs = yield http.Request.load_from_filters([]) SELECT id, full_request
for req in reqs: FROM requests;
yield req.deep_save() """,
)
# Create an object and get its port/is_ssl
for reqrow in reqrows:
reqid = reqrow[0]
fullreq = reqrow[1]
r = http.Request(fullreq)
port = r.port
is_ssl = r.is_ssl
yield dbpool.runQuery(
"""
UPDATE requests SET port=?,is_ssl=? WHERE id=?;
""",
(port, is_ssl, reqid)
)
yield dbpool.runQuery(
"""
UPDATE schema_meta SET version=2;
"""
)

@ -0,0 +1,50 @@
from pappyproxy import http
from twisted.internet import defer
"""
Schema v4
Description:
Adds additional metadata to the database for requests. Mainly it stores the host
that a request was sent to so that pappy doesn't have to guess from the host
header.
"""
update_queries = [
"""
ALTER TABLE requests ADD COLUMN host TEXT;
""",
]
@defer.inlineCallbacks
def update(dbpool):
for query in update_queries:
yield dbpool.runQuery(query)
# Update metadata for each request
reqrows = yield dbpool.runQuery(
"""
SELECT id, full_request
FROM requests;
""",
)
# Create an object that will parse the host from the request
for reqrow in reqrows:
reqid = reqrow[0]
fullreq = reqrow[1]
r = http.Request(fullreq)
host = r.host
if r.host:
yield dbpool.runQuery(
"""
UPDATE requests SET host=? WHERE id=?;
""",
(host, reqid)
)
yield dbpool.runQuery(
"""
UPDATE schema_meta SET version=4;
"""
)

@ -0,0 +1,29 @@
from pappyproxy import http
from twisted.internet import defer
"""
Schema v5
Description:
Adds a column to the requests table which will store a dict that plugins can
use to store metadata about requests.
"""
update_queries = [
"""
ALTER TABLE requests ADD COLUMN plugin_data TEXT;
""",
"""
UPDATE requests SET plugin_data="{}";
""",
"""
UPDATE schema_meta SET version=5;
"""
]
@defer.inlineCallbacks
def update(dbpool):
for query in update_queries:
yield dbpool.runQuery(query)

@ -1,6 +1,7 @@
import os import os
import glob import glob
import imp import imp
import random
from twisted.internet import reactor from twisted.internet import reactor
from twisted.enterprise import adbapi from twisted.enterprise import adbapi
@ -30,8 +31,27 @@ def add_schema_files(schemas):
newmod = imp.load_source('%s'%module_name, mod) newmod = imp.load_source('%s'%module_name, mod)
schemas.append( (module_name, newmod) ) schemas.append( (module_name, newmod) )
def copy_file(a, b):
a_bytes = a.read(1024)
while a_bytes:
b.write(a_bytes)
a_bytes = a.read(1024)
def create_backup(filename):
backup = filename + '.bak'
while os.path.isfile(backup):
backup = '%s.%d' % (backup, random.randint(0, 9999999999))
# Make sure backup file has secure permissions
with os.fdopen(os.open(backup, os.O_CREAT, 0o0600), 'r') as f:
pass
# Copy the datafile
with open(filename, 'r') as a:
with open(backup, 'w') as b:
copy_file(a, b)
return backup
@defer.inlineCallbacks @defer.inlineCallbacks
def update_schema(dbpool): def update_schema(dbpool, filename):
# Update the database schema to the latest version # Update the database schema to the latest version
schema_version = yield get_schema_version(dbpool) schema_version = yield get_schema_version(dbpool)
if schema_version == 0: if schema_version == 0:
@ -41,11 +61,32 @@ def update_schema(dbpool):
schemas = [] schemas = []
add_schema_files(schemas) add_schema_files(schemas)
schemas = sorted(schemas, key=lambda tup: tup[0]) schemas = sorted(schemas, key=lambda tup: tup[0])
for i in range(schema_version, len(schemas)): to_run = range(schema_version, len(schemas))
if len(to_run) > 0:
# Back up data file
if verbose_update:
print 'Backing up data file'
backup = create_backup(filename)
if verbose_update:
print 'Backed up to %s' % backup
try:
for i in to_run:
# schemas[0] is v1, schemas[1] is v2, etc # schemas[0] is v1, schemas[1] is v2, etc
if verbose_update: if verbose_update:
print "Updating datafaile schema to version %d" % (i+1) print "Updating datafaile schema to version %d" % (i+1)
yield schemas[i][1].update(dbpool) yield schemas[i][1].update(dbpool)
# Delete backup
os.remove(backup)
if verbose_update:
print 'Update successful! Deleted backup'
except Exception as e:
# restore the backup
print 'Update failed, restoring backup'
with open(filename, 'w') as a:
with open(backup, 'r') as b:
copy_file(b, a)
os.remove(backup)
raise e
@defer.inlineCallbacks @defer.inlineCallbacks
def main(): def main():

@ -2,9 +2,15 @@ from pappyproxy.session import Session
MACRO_NAME = '{{macro_name}}' MACRO_NAME = '{{macro_name}}'
SHORT_NAME = '{{short_name}}' SHORT_NAME = '{{short_name}}'
runargs = []
def init(args):
runargs = args
def mangle_request(request): def mangle_request(request):
global runargs
return request return request
def mangle_response(request): def mangle_response(request):
global runargs
return request.response return request.response

@ -1,4 +1,4 @@
from pappyproxy.http import Request, get_request, post_request from pappyproxy.http import Request, get_request, post_request, request_by_id
from pappyproxy.context import set_tag from pappyproxy.context import set_tag
from pappyproxy.iter import * from pappyproxy.iter import *

@ -0,0 +1,211 @@
import pytest
import mock
import pappyproxy
from pappyproxy.mangle import async_mangle_request, async_mangle_response
from pappyproxy.http import Request, Response
from testutil import no_tcp, no_database, func_deleted, mock_deferred, mock_deep_save, fake_saving
def retf(r):
return False
@pytest.fixture
def ignore_edit(mocker):
new_edit = mock.MagicMock()
new_edit.return_value = mock_deferred(None)
new_plugin = mock.MagicMock()
new_plugin.return_value = new_edit
mocker.patch('pappyproxy.plugin.plugin_by_name', new=new_plugin)
@pytest.fixture
def ignore_delete(mocker):
new_os_remove = mock.MagicMock()
mocker.patch('os.remove', new=new_os_remove)
return new_os_remove
@pytest.fixture(autouse=True)
def no_logging(mocker):
mocker.patch('pappyproxy.proxy.log')
@pytest.fixture
def req():
r = Request()
r.start_line = 'GET / HTTP/1.1'
r.host = 'www.ffffff.eeeeee'
r.body = 'AAAA'
return r
@pytest.fixture
def req_w_rsp(req):
r = Response()
r.start_line = 'HTTP/1.1 200 OK'
r.headers['Test-Header'] = 'ABC123'
r.body = 'AAAA'
req.response = r
return req
@pytest.fixture
def mock_tempfile(mocker):
new_tfile_obj = mock.MagicMock()
tfile_instance = mock.MagicMock()
new_tfile_obj.return_value.__enter__.return_value = tfile_instance
tfile_instance.name = 'mockTemporaryFile'
mocker.patch('tempfile.NamedTemporaryFile', new=new_tfile_obj)
new_open = mock.MagicMock()
fake_file = mock.MagicMock(spec=file)
new_open.return_value.__enter__.return_value = fake_file
mocker.patch('__builtin__.open', new_open)
return (new_tfile_obj, tfile_instance, new_open, fake_file)
########################
## Test request mangling
@pytest.inlineCallbacks
def test_mangle_request_edit(req, mock_deep_save, mock_tempfile,
ignore_edit, ignore_delete):
tfile_obj, tfile_instance, new_open, fake_file = mock_tempfile
r = req
new_contents = ('GET / HTTP/1.1\r\n'
'Content-Length: 4\r\n\r\n'
'BBBB')
fake_file.read.return_value = new_contents
new_req = yield async_mangle_request(r)
assert not mock_deep_save.called
assert tfile_obj.called
assert tfile_instance.write.called
assert tfile_instance.write.call_args == ((r.full_request,),)
assert new_open.called
assert fake_file.read.called
assert new_req.full_request == new_contents
@pytest.inlineCallbacks
def test_mangle_request_edit_newlines(req, mock_deep_save, mock_tempfile,
ignore_edit, ignore_delete):
# Intercepting is off, request in scope
tfile_obj, tfile_instance, new_open, fake_file = mock_tempfile
r = req
new_contents = ('GET / HTTP/1.1\r\n'
'Test-Head: FOOBIE\n'
'Content-Length: 4\n\r\n'
'BBBB')
fake_file.read.return_value = new_contents
new_req = yield async_mangle_request(r)
assert new_req.full_request == ('GET / HTTP/1.1\r\n'
'Test-Head: FOOBIE\r\n'
'Content-Length: 4\r\n\r\n'
'BBBB')
assert new_req.headers['Test-Head'] == 'FOOBIE'
@pytest.inlineCallbacks
def test_mangle_request_drop(req, mock_deep_save, mock_tempfile,
ignore_edit, ignore_delete):
# Intercepting is off, request in scope
tfile_obj, tfile_instance, new_open, fake_file = mock_tempfile
r = req
new_contents = ''
fake_file.read.return_value = new_contents
new_req = yield async_mangle_request(r)
assert new_req is None
@pytest.inlineCallbacks
def test_mangle_request_edit_len(req, mock_deep_save, mock_tempfile,
ignore_edit, ignore_delete):
# Intercepting is off, request in scope
tfile_obj, tfile_instance, new_open, fake_file = mock_tempfile
r = req
new_contents = ('GET / HTTP/1.1\r\n'
'Test-Head: FOOBIE\n'
'Content-Length: 4\n\r\n'
'BBBBAAAA')
fake_file.read.return_value = new_contents
new_req = yield async_mangle_request(r)
assert new_req.full_request == ('GET / HTTP/1.1\r\n'
'Test-Head: FOOBIE\r\n'
'Content-Length: 8\r\n\r\n'
'BBBBAAAA')
#########################
## Test response mangling
@pytest.inlineCallbacks
def test_mangle_response_edit(req_w_rsp, mock_deep_save, mock_tempfile,
ignore_edit, ignore_delete):
# Intercepting is on, edit
tfile_obj, tfile_instance, new_open, fake_file = mock_tempfile
r = req_w_rsp
old_rsp = r.response.full_response
new_contents = ('HTTP/1.1 403 NOTOKIEDOKIE\r\n'
'Content-Length: 4\r\n'
'Other-Header: foobles\r\n\r\n'
'BBBB')
fake_file.read.return_value = new_contents
mangled_rsp = yield async_mangle_response(r)
assert not mock_deep_save.called
assert tfile_obj.called
assert tfile_instance.write.called
assert tfile_instance.write.call_args == ((old_rsp,),)
assert new_open.called
assert fake_file.read.called
assert mangled_rsp.full_response == new_contents
@pytest.inlineCallbacks
def test_mangle_response_newlines(req_w_rsp, mock_deep_save, mock_tempfile,
ignore_edit, ignore_delete):
# Intercepting is off, request in scope
tfile_obj, tfile_instance, new_open, fake_file = mock_tempfile
r = req_w_rsp
old_rsp = r.response.full_response
new_contents = ('HTTP/1.1 403 NOTOKIEDOKIE\n'
'Content-Length: 4\n'
'Other-Header: foobles\r\n\n'
'BBBB')
fake_file.read.return_value = new_contents
mangled_rsp = yield async_mangle_response(r)
assert mangled_rsp.full_response == ('HTTP/1.1 403 NOTOKIEDOKIE\r\n'
'Content-Length: 4\r\n'
'Other-Header: foobles\r\n\r\n'
'BBBB')
assert mangled_rsp.headers['Other-Header'] == 'foobles'
@pytest.inlineCallbacks
def test_mangle_response_drop(req_w_rsp, mock_deep_save, mock_tempfile,
ignore_edit, ignore_delete):
# Intercepting is off, request in scope
tfile_obj, tfile_instance, new_open, fake_file = mock_tempfile
r = req_w_rsp
old_rsp = r.response.full_response
new_contents = ''
fake_file.read.return_value = new_contents
mangled_rsp = yield async_mangle_response(r)
assert mangled_rsp is None
@pytest.inlineCallbacks
def test_mangle_response_new_len(req_w_rsp, mock_deep_save, mock_tempfile,
ignore_edit, ignore_delete):
# Intercepting is off, request in scope
tfile_obj, tfile_instance, new_open, fake_file = mock_tempfile
r = req_w_rsp
old_rsp = r.response.full_response
new_contents = ('HTTP/1.1 403 NOTOKIEDOKIE\n'
'Content-Length: 4\n'
'Other-Header: foobles\r\n\n'
'BBBBAAAA')
fake_file.read.return_value = new_contents
mangled_rsp = yield async_mangle_response(r)
assert mangled_rsp.full_response == ('HTTP/1.1 403 NOTOKIEDOKIE\r\n'
'Content-Length: 8\r\n'
'Other-Header: foobles\r\n\r\n'
'BBBBAAAA')

@ -11,8 +11,8 @@ def test_filter_reqs():
pass pass
def test_gen_filter_by_all_request(): def test_gen_filter_by_all_request():
f = context.gen_filter_by_all(context.cmp_contains, 'hello') f = context.gen_filter_by_all(['ct', 'hello'])
fn = context.gen_filter_by_all(context.cmp_contains, 'hello', negate=True) fn = context.gen_filter_by_all(['nct', 'hello'])
# Nowhere # Nowhere
r = Request('GET / HTTP/1.1\r\n') r = Request('GET / HTTP/1.1\r\n')
@ -31,7 +31,7 @@ def test_gen_filter_by_all_request():
# Data # Data
r = Request('GET / HTTP/1.1\r\n') r = Request('GET / HTTP/1.1\r\n')
r.raw_data = 'hello' r.body = 'hello'
assert f(r) assert f(r)
assert not fn(r) assert not fn(r)
@ -73,8 +73,8 @@ def test_gen_filter_by_all_request():
def test_gen_filter_by_all_response(http_request): def test_gen_filter_by_all_response(http_request):
f = context.gen_filter_by_all(context.cmp_contains, 'hello') f = context.gen_filter_by_all(['ct', 'hello'])
fn = context.gen_filter_by_all(context.cmp_contains, 'hello', negate=True) fn = context.gen_filter_by_all(['nct', 'hello'])
# Nowhere # Nowhere
r = Response('HTTP/1.1 200 OK\r\n') r = Response('HTTP/1.1 200 OK\r\n')
@ -91,7 +91,7 @@ def test_gen_filter_by_all_response(http_request):
# Data # Data
r = Response('HTTP/1.1 200 OK\r\n') r = Response('HTTP/1.1 200 OK\r\n')
http_request.response = r http_request.response = r
r.raw_data = 'hello' r.body = 'hello'
assert f(http_request) assert f(http_request)
assert not fn(http_request) assert not fn(http_request)
@ -138,8 +138,8 @@ def test_gen_filter_by_all_response(http_request):
assert fn(http_request) assert fn(http_request)
def test_filter_by_host(http_request): def test_filter_by_host(http_request):
f = context.gen_filter_by_host(context.cmp_contains, 'sexy') f = context.gen_filter_by_host(['ct', 'sexy'])
fn = context.gen_filter_by_host(context.cmp_contains, 'sexy', negate=True) fn = context.gen_filter_by_host(['nct', 'sexy'])
http_request.headers['Host'] = 'google.com' http_request.headers['Host'] = 'google.com'
http_request.headers['MiscHeader'] = 'vim.sexy' http_request.headers['MiscHeader'] = 'vim.sexy'
@ -152,55 +152,55 @@ def test_filter_by_host(http_request):
assert not fn(http_request) assert not fn(http_request)
def test_filter_by_body(): def test_filter_by_body():
f = context.gen_filter_by_body(context.cmp_contains, 'sexy') f = context.gen_filter_by_body(['ct', 'sexy'])
fn = context.gen_filter_by_body(context.cmp_contains, 'sexy', negate=True) fn = context.gen_filter_by_body(['nct', 'sexy'])
# Test request bodies # Test request bodies
r = Request() r = Request()
r.status_line = 'GET /sexy HTTP/1.1' r.start_line = 'GET /sexy HTTP/1.1'
r.headers['Header'] = 'sexy' r.headers['Header'] = 'sexy'
r.raw_data = 'foo' r.body = 'foo'
assert not f(r) assert not f(r)
assert fn(r) assert fn(r)
r.raw_data = 'sexy' r.body = 'sexy'
assert f(r) assert f(r)
assert not fn(r) assert not fn(r)
# Test response bodies # Test response bodies
r = Request() r = Request()
rsp = Response() rsp = Response()
rsp.status_line = 'HTTP/1.1 200 OK' rsp.start_line = 'HTTP/1.1 200 OK'
rsp.headers['sexy'] = 'sexy' rsp.headers['sexy'] = 'sexy'
r.status_line = 'GET /sexy HTTP/1.1' r.start_line = 'GET /sexy HTTP/1.1'
r.headers['Header'] = 'sexy' r.headers['Header'] = 'sexy'
r.response = rsp r.response = rsp
assert not f(r) assert not f(r)
assert fn(r) assert fn(r)
rsp.raw_data = 'sexy' rsp.body = 'sexy'
assert f(r) assert f(r)
assert not fn(r) assert not fn(r)
def test_filter_by_response_code(http_request): def test_filter_by_response_code(http_request):
f = context.gen_filter_by_response_code(context.cmp_eq, 200) f = context.gen_filter_by_response_code(['eq', '200'])
fn = context.gen_filter_by_response_code(context.cmp_eq, 200, negate=True) fn = context.gen_filter_by_response_code(['neq', '200'])
r = Response() r = Response()
http_request.response = r http_request.response = r
r.status_line = 'HTTP/1.1 404 Not Found' r.start_line = 'HTTP/1.1 404 Not Found'
assert not f(http_request) assert not f(http_request)
assert fn(http_request) assert fn(http_request)
r.status_line = 'HTTP/1.1 200 OK' r.start_line = 'HTTP/1.1 200 OK'
assert f(http_request) assert f(http_request)
assert not fn(http_request) assert not fn(http_request)
def test_filter_by_raw_headers_request(): def test_filter_by_raw_headers_request():
f1 = context.gen_filter_by_raw_headers(context.cmp_contains, 'Sexy:') f1 = context.gen_filter_by_raw_headers(['ct', 'Sexy:'])
fn1 = context.gen_filter_by_raw_headers(context.cmp_contains, 'Sexy:', negate=True) fn1 = context.gen_filter_by_raw_headers(['nct', 'Sexy:'])
f2 = context.gen_filter_by_raw_headers(context.cmp_contains, 'sexy\r\nHeader') f2 = context.gen_filter_by_raw_headers(['ct', 'sexy\r\nHeader'])
fn2 = context.gen_filter_by_raw_headers(context.cmp_contains, 'sexy\r\nHeader', negate=True) fn2 = context.gen_filter_by_raw_headers(['nct', 'sexy\r\nHeader'])
r = Request('GET / HTTP/1.1\r\n') r = Request('GET / HTTP/1.1\r\n')
rsp = Response('HTTP/1.1 200 OK\r\n') rsp = Response('HTTP/1.1 200 OK\r\n')
@ -228,10 +228,10 @@ def test_filter_by_raw_headers_request():
assert not fn2(r) assert not fn2(r)
def test_filter_by_raw_headers_response(): def test_filter_by_raw_headers_response():
f1 = context.gen_filter_by_raw_headers(context.cmp_contains, 'Sexy:') f1 = context.gen_filter_by_raw_headers(['ct', 'Sexy:'])
fn1 = context.gen_filter_by_raw_headers(context.cmp_contains, 'Sexy:', negate=True) fn1 = context.gen_filter_by_raw_headers(['nct', 'Sexy:'])
f2 = context.gen_filter_by_raw_headers(context.cmp_contains, 'sexy\r\nHeader') f2 = context.gen_filter_by_raw_headers(['ct', 'sexy\r\nHeader'])
fn2 = context.gen_filter_by_raw_headers(context.cmp_contains, 'sexy\r\nHeader', negate=True) fn2 = context.gen_filter_by_raw_headers(['nct', 'sexy\r\nHeader'])
r = Request('GET / HTTP/1.1\r\n') r = Request('GET / HTTP/1.1\r\n')
rsp = Response('HTTP/1.1 200 OK\r\n') rsp = Response('HTTP/1.1 200 OK\r\n')
@ -259,25 +259,24 @@ def test_filter_by_raw_headers_response():
assert not fn2(r) assert not fn2(r)
def test_filter_by_path(http_request): def test_filter_by_path(http_request):
f = context.gen_filter_by_path(context.cmp_contains, 'porn') # find the fun websites f = context.gen_filter_by_path(['ct', 'porn']) # find the fun websites
fn = context.gen_filter_by_path(context.cmp_contains, 'porn', negate=True) # find the boring websites fn = context.gen_filter_by_path(['nct', 'porn']) # find the boring websites
http_request.status_line = 'GET / HTTP/1.1' http_request.start_line = 'GET / HTTP/1.1'
assert not f(http_request) assert not f(http_request)
assert fn(http_request) assert fn(http_request)
http_request.status_line = 'GET /path/to/great/porn HTTP/1.1' http_request.start_line = 'GET /path/to/great/porn HTTP/1.1'
assert f(http_request) assert f(http_request)
assert not fn(http_request) assert not fn(http_request)
http_request.status_line = 'GET /path/to/porn/great HTTP/1.1' http_request.start_line = 'GET /path/to/porn/great HTTP/1.1'
assert f(http_request) assert f(http_request)
assert not fn(http_request) assert not fn(http_request)
def test_gen_filter_by_submitted_cookies(): def test_gen_filter_by_submitted_cookies():
f1 = context.gen_filter_by_submitted_cookies(context.cmp_contains, 'Session') f1 = context.gen_filter_by_submitted_cookies(['ct', 'Session'])
f2 = context.gen_filter_by_submitted_cookies(context.cmp_contains, 'Cookie', f2 = context.gen_filter_by_submitted_cookies(['ct', 'Cookie', 'nct', 'CookieVal'])
context.cmp_contains, 'CookieVal')
r = Request(('GET / HTTP/1.1\r\n' r = Request(('GET / HTTP/1.1\r\n'
'Cookie: foo=bar\r\n' 'Cookie: foo=bar\r\n'
'\r\n')) '\r\n'))
@ -294,18 +293,17 @@ def test_gen_filter_by_submitted_cookies():
'Cookie: Session=bar; CookieThing=NoMatch\r\n' 'Cookie: Session=bar; CookieThing=NoMatch\r\n'
'\r\n')) '\r\n'))
assert f1(r) assert f1(r)
assert not f2(r) assert f2(r)
r = Request(('GET / HTTP/1.1\r\n' r = Request(('GET / HTTP/1.1\r\n'
'Cookie: Session=bar; CookieThing=CookieValue\r\n' 'Cookie: Session=bar; CookieThing=CookieValue\r\n'
'\r\n')) '\r\n'))
assert f1(r) assert f1(r)
assert f2(r) assert not f2(r)
def test_gen_filter_by_set_cookies(): def test_gen_filter_by_set_cookies():
f1 = context.gen_filter_by_set_cookies(context.cmp_contains, 'Session') f1 = context.gen_filter_by_set_cookies(['ct', 'Session'])
f2 = context.gen_filter_by_set_cookies(context.cmp_contains, 'Cookie', f2 = context.gen_filter_by_set_cookies(['ct', 'Cookie', 'ct', 'CookieVal'])
context.cmp_contains, 'CookieVal')
r = Request('GET / HTTP/1.1\r\n\r\n') r = Request('GET / HTTP/1.1\r\n\r\n')
rsp = Response(('HTTP/1.1 200 OK\r\n' rsp = Response(('HTTP/1.1 200 OK\r\n'
@ -345,9 +343,8 @@ def test_gen_filter_by_set_cookies():
assert f2(r) assert f2(r)
def test_filter_by_params_get(): def test_filter_by_params_get():
f1 = context.gen_filter_by_params(context.cmp_contains, 'Session') f1 = context.gen_filter_by_params(['ct', 'Session'])
f2 = context.gen_filter_by_params(context.cmp_contains, 'Cookie', f2 = context.gen_filter_by_params(['ct', 'Cookie', 'ct', 'CookieVal'])
context.cmp_contains, 'CookieVal')
r = Request('GET / HTTP/1.1\r\n\r\n') r = Request('GET / HTTP/1.1\r\n\r\n')
assert not f1(r) assert not f1(r)
@ -366,30 +363,29 @@ def test_filter_by_params_get():
assert f2(r) assert f2(r)
def test_filter_by_params_post(): def test_filter_by_params_post():
f1 = context.gen_filter_by_params(context.cmp_contains, 'Session') f1 = context.gen_filter_by_params(['ct', 'Session'])
f2 = context.gen_filter_by_params(context.cmp_contains, 'Cookie', f2 = context.gen_filter_by_params(['ct', 'Cookie', 'ct', 'CookieVal'])
context.cmp_contains, 'CookieVal')
r = Request(('GET / HTTP/1.1\r\n' r = Request(('GET / HTTP/1.1\r\n'
'Content-Type: application/x-www-form-urlencoded\r\n\r\n')) 'Content-Type: application/x-www-form-urlencoded\r\n\r\n'))
r.raw_data = 'foo=bar' r.body = 'foo=bar'
assert not f1(r) assert not f1(r)
assert not f2(r) assert not f2(r)
r = Request(('GET / HTTP/1.1\r\n' r = Request(('GET / HTTP/1.1\r\n'
'Content-Type: application/x-www-form-urlencoded\r\n\r\n')) 'Content-Type: application/x-www-form-urlencoded\r\n\r\n'))
r.raw_data = 'Session=bar' r.body = 'Session=bar'
assert f1(r) assert f1(r)
assert not f2(r) assert not f2(r)
r = Request(('GET / HTTP/1.1\r\n' r = Request(('GET / HTTP/1.1\r\n'
'Content-Type: application/x-www-form-urlencoded\r\n\r\n')) 'Content-Type: application/x-www-form-urlencoded\r\n\r\n'))
r.raw_data = 'Session=bar&Cookie=foo' r.body = 'Session=bar&Cookie=foo'
assert f1(r) assert f1(r)
assert not f2(r) assert not f2(r)
r = Request(('GET / HTTP/1.1\r\n' r = Request(('GET / HTTP/1.1\r\n'
'Content-Type: application/x-www-form-urlencoded\r\n\r\n')) 'Content-Type: application/x-www-form-urlencoded\r\n\r\n'))
r.raw_data = 'Session=bar&CookieThing=CookieValue' r.body = 'Session=bar&CookieThing=CookieValue'
assert f1(r) assert f1(r)
assert f2(r) assert f2(r)

@ -86,7 +86,7 @@ def test_chunked_simple():
full_data += '0\r\n\r\n' full_data += '0\r\n\r\n'
c.add_data(full_data) c.add_data(full_data)
assert c.complete assert c.complete
assert c.raw_data == 'A'*5 assert c.body == 'A'*5
def test_chunked_hex(): def test_chunked_hex():
# Test hex lengths # Test hex lengths
@ -97,7 +97,7 @@ def test_chunked_hex():
full_data += '0\r\n\r\n' full_data += '0\r\n\r\n'
c.add_data(full_data) c.add_data(full_data)
assert c.complete assert c.complete
assert c.raw_data == 'A'*0xAF assert c.body == 'A'*0xAF
c = http.ChunkedData() c = http.ChunkedData()
full_data = 'AF\r\n' full_data = 'AF\r\n'
@ -106,7 +106,7 @@ def test_chunked_hex():
full_data += '0\r\n\r\n' full_data += '0\r\n\r\n'
c.add_data(full_data) c.add_data(full_data)
assert c.complete assert c.complete
assert c.raw_data == 'A'*0xAF assert c.body == 'A'*0xAF
c = http.ChunkedData() c = http.ChunkedData()
full_data = 'aF\r\n' full_data = 'aF\r\n'
@ -115,7 +115,7 @@ def test_chunked_hex():
full_data += '0\r\n\r\n' full_data += '0\r\n\r\n'
c.add_data(full_data) c.add_data(full_data)
assert c.complete assert c.complete
assert c.raw_data == 'A'*0xAF assert c.body == 'A'*0xAF
def test_chunked_leading_zeros(): def test_chunked_leading_zeros():
# Test leading zeros # Test leading zeros
@ -126,7 +126,7 @@ def test_chunked_leading_zeros():
full_data += '0\r\n\r\n' full_data += '0\r\n\r\n'
c.add_data(full_data) c.add_data(full_data)
assert c.complete assert c.complete
assert c.raw_data == 'A'*0xAF assert c.body == 'A'*0xAF
def test_chunked_one_char_add(): def test_chunked_one_char_add():
# Test adding one character at a time # Test adding one character at a time
@ -138,7 +138,7 @@ def test_chunked_one_char_add():
for ch in full_data: for ch in full_data:
c.add_data(ch) c.add_data(ch)
assert c.complete assert c.complete
assert c.raw_data == 'A'*0xAF assert c.body == 'A'*0xAF
def test_chunked_incomplete(): def test_chunked_incomplete():
# Tests that complete isn't true until the data is received # Tests that complete isn't true until the data is received
@ -168,11 +168,11 @@ def test_length_data_simple():
assert not l.complete assert not l.complete
l.add_data('A'*100) l.add_data('A'*100)
assert l.complete assert l.complete
assert l.raw_data == 'A'*100 assert l.body == 'A'*100
l = http.LengthData(0) l = http.LengthData(0)
assert l.complete assert l.complete
assert l.raw_data == '' assert l.body == ''
# Test incomplete # Test incomplete
l = http.LengthData(100) l = http.LengthData(100)
@ -185,7 +185,7 @@ def test_length_one_character():
for i in range(100): for i in range(100):
l.add_data('A') l.add_data('A')
assert l.complete assert l.complete
assert l.raw_data == 'A'*100 assert l.body == 'A'*100
# Test adding one character at a time (incomplete) # Test adding one character at a time (incomplete)
l = http.LengthData(100) l = http.LengthData(100)
@ -198,7 +198,7 @@ def test_length_overflow():
l = http.LengthData(100) l = http.LengthData(100)
l.add_data('A'*400) l.add_data('A'*400)
assert l.complete assert l.complete
assert l.raw_data == 'A'*100 assert l.body == 'A'*100
# Test throwing an exception when adding data after complete # Test throwing an exception when adding data after complete
l = http.LengthData(100) l = http.LengthData(100)
@ -370,6 +370,79 @@ def test_response_cookie_blank():
assert c.path == '/' assert c.path == '/'
assert c.secure assert c.secure
####################
## HTTPMessage tests
def test_message_simple():
raw = ('foobar\r\n'
'a: b\r\n'
'Content-Length: 100\r\n\r\n')
raw += 'A'*100
m = http.HTTPMessage(raw)
assert m.complete
assert m.malformed == False
assert m.start_line == 'foobar'
assert m.body == 'A'*100
assert m.headers.all_pairs() == [('a', 'b'), ('Content-Length', '100')]
assert m.headers['A'] == 'b'
assert m.headers_section == ('foobar\r\n'
'a: b\r\n'
'Content-Length: 100\r\n\r\n')
assert m.full_message == raw
def test_message_build():
raw = ('foobar\r\n'
'a: b\r\n'
'Content-Length: 100\r\n\r\n')
raw += 'A'*100
m = http.HTTPMessage()
m.add_line('foobar')
m.add_line('a: b')
m.add_line('Content-Length: 100')
m.add_line('')
assert not m.complete
m.add_data('A'*50)
assert not m.complete
m.add_data('A'*50)
assert m.complete
assert m.malformed == False
assert m.start_line == 'foobar'
assert m.body == 'A'*100
assert m.headers.all_pairs() == [('a', 'b'), ('Content-Length', '100')]
assert m.headers['A'] == 'b'
assert m.headers_section == ('foobar\r\n'
'a: b\r\n'
'Content-Length: 100\r\n\r\n')
assert m.full_message == raw
def test_message_build_chunked():
raw = ('foobar\r\n'
'a: b\r\n'
'Content-Length: 100\r\n\r\n')
raw += 'A'*100
m = http.HTTPMessage()
m.add_line('foobar')
m.add_line('a: b')
m.add_line('Transfer-Encoding: chunked')
m.add_line('')
assert not m.complete
m.add_data('%x\r\n' % 50)
m.add_data('A'*50)
m.add_data('\r\n')
m.add_data('%x\r\n' % 50)
m.add_data('A'*50)
m.add_data('\r\n')
m.add_data('0\r\n')
assert m.complete
assert m.malformed == False
assert m.start_line == 'foobar'
assert m.body == 'A'*100
assert m.headers.all_pairs() == [('a', 'b'), ('Content-Length', '100')]
assert m.headers['A'] == 'b'
assert m.headers_section == ('foobar\r\n'
'a: b\r\n'
'Content-Length: 100\r\n\r\n')
assert m.full_message == raw
#################### ####################
## Request tests ## Request tests
@ -398,7 +471,7 @@ def test_request_simple():
assert r.is_ssl == False assert r.is_ssl == False
assert r.path == '/' assert r.path == '/'
assert r.port == 80 assert r.port == 80
assert r.status_line == 'GET / HTTP/1.1' assert r.start_line == 'GET / HTTP/1.1'
assert r.verb == 'GET' assert r.verb == 'GET'
assert r.version == 'HTTP/1.1' assert r.version == 'HTTP/1.1'
assert r.headers['Content-Length'] == '100' assert r.headers['Content-Length'] == '100'
@ -409,7 +482,7 @@ def test_request_simple():
assert r.headers['Host'] == 'www.test.com' assert r.headers['Host'] == 'www.test.com'
assert r.headers['Connection'] == 'Keep-Alive' assert r.headers['Connection'] == 'Keep-Alive'
assert r.headers['Cache-Control'] == 'no-cache' assert r.headers['Cache-Control'] == 'no-cache'
assert r.raw_data == 'A'*100 assert r.body == 'A'*100
test(rf) test(rf)
test(rl) test(rl)
test(ru) test(ru)
@ -536,6 +609,7 @@ def test_request_parse_host():
rf, rl, ru, rj = req_by_lines_and_full(header_lines) rf, rl, ru, rj = req_by_lines_and_full(header_lines)
def test(r): def test(r):
assert r.complete assert r.complete
assert r.port == 443
assert r.host == 'www.test.com' assert r.host == 'www.test.com'
assert r.is_ssl assert r.is_ssl
test(rf) test(rf)
@ -574,7 +648,7 @@ def test_repeated_request_headers():
def test_request_update_statusline(): def test_request_update_statusline():
r = http.Request() r = http.Request()
r.status_line = 'GET / HTTP/1.1' r.start_line = 'GET / HTTP/1.1'
assert r.verb == 'GET' assert r.verb == 'GET'
assert r.path == '/' assert r.path == '/'
assert r.version == 'HTTP/1.1' assert r.version == 'HTTP/1.1'
@ -584,7 +658,7 @@ def test_request_update_statusline():
def test_request_update_cookies(): def test_request_update_cookies():
r = http.Request() r = http.Request()
r.status_line = 'GET / HTTP/1.1' r.start_line = 'GET / HTTP/1.1'
# Check new cookies # Check new cookies
r.cookies['foo'] = 'bar' r.cookies['foo'] = 'bar'
@ -607,7 +681,7 @@ def test_request_update_cookies():
def test_request_update_headers(): def test_request_update_headers():
r = http.Request() r = http.Request()
r.status_line = 'GET / HTTP/1.1' r.start_line = 'GET / HTTP/1.1'
r.headers['Content-Length'] = '0' r.headers['Content-Length'] = '0'
r.headers['Test-Header'] = 'Test Value' r.headers['Test-Header'] = 'Test Value'
r.headers['Other-Header'] = 'Other Value' r.headers['Other-Header'] = 'Other Value'
@ -624,11 +698,11 @@ def test_request_update_headers():
def test_request_modified_headers(): def test_request_modified_headers():
r = http.Request() r = http.Request()
r.status_line = 'GET / HTTP/1.1' r.start_line = 'GET / HTTP/1.1'
r.headers['content-length'] = '100' r.headers['content-length'] = '100'
r.headers['cookie'] = 'abc=123' r.headers['cookie'] = 'abc=123'
r.cookies['abc'] = '456' r.cookies['abc'] = '456'
r.raw_data = 'AAAA' r.body = 'AAAA'
assert r.full_request == ('GET / HTTP/1.1\r\n' assert r.full_request == ('GET / HTTP/1.1\r\n'
'content-length: 4\r\n' 'content-length: 4\r\n'
'cookie: abc=456\r\n\r\n' 'cookie: abc=456\r\n\r\n'
@ -638,33 +712,34 @@ def test_request_modified_headers():
def test_request_update_data(): def test_request_update_data():
r = http.Request() r = http.Request()
r.status_line = 'GET / HTTP/1.1' r.start_line = 'GET / HTTP/1.1'
r.headers['content-length'] = 500 r.headers['content-length'] = 500
r.raw_data = 'AAAA' r.body = 'AAAA'
assert r.full_request == ('GET / HTTP/1.1\r\n' assert r.full_request == ('GET / HTTP/1.1\r\n'
'content-length: 4\r\n' 'content-length: 4\r\n'
'\r\n' '\r\n'
'AAAA') 'AAAA')
def test_request_to_json(): def test_request_to_json():
r = http.Request() r = http.Request()
r.status_line = 'GET / HTTP/1.1' r.start_line = 'GET / HTTP/1.1'
r.headers['content-length'] = 500 r.headers['content-length'] = 500
r.tags = ['foo', 'bar'] r.tags = ['foo', 'bar']
r.raw_data = 'AAAA' r.body = 'AAAA'
r.reqid = '1' r.reqid = '1'
rsp = http.Response() rsp = http.Response()
rsp.status_line = 'HTTP/1.1 200 OK' rsp.start_line = 'HTTP/1.1 200 OK'
rsp.rspid = '2' rsp.rspid = '2'
r.response = rsp r.response = rsp
expected_reqdata = {u'full_request': unicode(base64.b64encode(r.full_request)), expected_reqdata = {u'full_message': unicode(base64.b64encode(r.full_request)),
u'response_id': str(rsp.rspid), u'response_id': str(rsp.rspid),
u'port': 80, u'port': 80,
u'is_ssl': False, u'is_ssl': False,
u'tags': ['foo', 'bar'], u'tags': ['foo', 'bar'],
u'reqid': str(r.reqid), u'reqid': str(r.reqid),
u'host': '',
} }
assert json.loads(r.to_json()) == expected_reqdata assert json.loads(r.to_json()) == expected_reqdata
@ -764,7 +839,7 @@ def test_request_copy():
def test_request_url_blankpath(): def test_request_url_blankpath():
r = http.Request() r = http.Request()
r.status_line = 'GET / HTTP/1.1' r.start_line = 'GET / HTTP/1.1'
r.url = 'https://www.google.com' r.url = 'https://www.google.com'
r.headers['Host'] = r.host r.headers['Host'] = r.host
r.url_params.from_dict({'foo': 'bar'}) r.url_params.from_dict({'foo': 'bar'})
@ -789,10 +864,10 @@ def test_response_simple():
rf, rl, ru, rj = rsp_by_lines_and_full(header_lines, data) rf, rl, ru, rj = rsp_by_lines_and_full(header_lines, data)
def test(r): def test(r):
assert r.complete assert r.complete
assert r.raw_data == data assert r.body == data
assert r.response_code == 200 assert r.response_code == 200
assert r.response_text == 'OK' assert r.response_text == 'OK'
assert r.status_line == 'HTTP/1.1 200 OK' assert r.start_line == 'HTTP/1.1 200 OK'
assert r.version == 'HTTP/1.1' assert r.version == 'HTTP/1.1'
assert r.headers['Date'] == 'Thu, 22 Oct 2015 00:37:17 GMT' assert r.headers['Date'] == 'Thu, 22 Oct 2015 00:37:17 GMT'
@ -826,7 +901,7 @@ def test_response_chunked():
rf, rl, ru, rj = rsp_by_lines_and_full(header_lines, data) rf, rl, ru, rj = rsp_by_lines_and_full(header_lines, data)
def test(r): def test(r):
assert r.complete assert r.complete
assert r.raw_data == 'A'*0xAF + 'B'*0xBF assert r.body == 'A'*0xAF + 'B'*0xBF
test(rf) test(rf)
test(rl) test(rl)
@ -851,7 +926,7 @@ def test_response_gzip():
rf, rl, ru, rj = rsp_by_lines_and_full(header_lines, data_comp) rf, rl, ru, rj = rsp_by_lines_and_full(header_lines, data_comp)
def test(r): def test(r):
assert r.complete assert r.complete
assert r.raw_data == data_decomp assert r.body == data_decomp
test(rf) test(rf)
test(rl) test(rl)
@ -876,7 +951,7 @@ def test_response_deflate():
rf, rl, ru, rj = rsp_by_lines_and_full(header_lines, data_comp) rf, rl, ru, rj = rsp_by_lines_and_full(header_lines, data_comp)
def test(r): def test(r):
assert r.complete assert r.complete
assert r.raw_data == data_decomp assert r.body == data_decomp
test(rf) test(rf)
test(rl) test(rl)
@ -907,7 +982,7 @@ def test_response_chunked_gzip():
rf, rl, ru, rj = rsp_by_lines_and_full(header_lines, data_chunked) rf, rl, ru, rj = rsp_by_lines_and_full(header_lines, data_chunked)
def test(r): def test(r):
assert r.complete assert r.complete
assert r.raw_data == data_decomp assert r.body == data_decomp
assert r.headers['Content-Length'] == str(len(data_decomp)) assert r.headers['Content-Length'] == str(len(data_decomp))
assert r.full_response == ('HTTP/1.1 200 OK\r\n' assert r.full_response == ('HTTP/1.1 200 OK\r\n'
'Date: Thu, 22 Oct 2015 00:37:17 GMT\r\n' 'Date: Thu, 22 Oct 2015 00:37:17 GMT\r\n'
@ -924,7 +999,7 @@ def test_response_chunked_gzip():
def test_response_early_completion(): def test_response_early_completion():
r = http.Response() r = http.Response()
r.status_line = 'HTTP/1.1 200 OK' r.start_line = 'HTTP/1.1 200 OK'
r.add_line('Content-Length: 0') r.add_line('Content-Length: 0')
assert not r.complete assert not r.complete
r.add_line('') r.add_line('')
@ -992,7 +1067,7 @@ def test_repeated_response_headers():
def test_response_update_statusline(): def test_response_update_statusline():
r = http.Response() r = http.Response()
r.status_line = 'HTTP/1.1 200 OK' r.start_line = 'HTTP/1.1 200 OK'
assert r.version == 'HTTP/1.1' assert r.version == 'HTTP/1.1'
assert r.response_code == 200 assert r.response_code == 200
assert r.response_text == 'OK' assert r.response_text == 'OK'
@ -1002,7 +1077,7 @@ def test_response_update_statusline():
def test_response_update_headers(): def test_response_update_headers():
r = http.Response() r = http.Response()
r.status_line = 'HTTP/1.1 200 OK' r.start_line = 'HTTP/1.1 200 OK'
r.headers['Test-Header'] = 'Test Value' r.headers['Test-Header'] = 'Test Value'
r.headers['Other-Header'] = 'Other Value' r.headers['Other-Header'] = 'Other Value'
@ -1018,9 +1093,9 @@ def test_response_update_headers():
def test_response_update_modified_headers(): def test_response_update_modified_headers():
r = http.Response() r = http.Response()
r.status_line = 'HTTP/1.1 200 OK' r.start_line = 'HTTP/1.1 200 OK'
r.headers['content-length'] = '500' r.headers['content-length'] = '500'
r.raw_data = 'AAAA' r.body = 'AAAA'
assert r.full_response == ('HTTP/1.1 200 OK\r\n' assert r.full_response == ('HTTP/1.1 200 OK\r\n'
'content-length: 4\r\n\r\n' 'content-length: 4\r\n\r\n'
'AAAA') 'AAAA')
@ -1028,7 +1103,7 @@ def test_response_update_modified_headers():
def test_response_update_cookies(): def test_response_update_cookies():
r = http.Response() r = http.Response()
r.status_line = 'HTTP/1.1 200 OK' r.start_line = 'HTTP/1.1 200 OK'
# Test by adding headers # Test by adding headers
r.headers['Set-Cookie'] = 'abc=123' r.headers['Set-Cookie'] = 'abc=123'
assert r.full_response == ('HTTP/1.1 200 OK\r\n' assert r.full_response == ('HTTP/1.1 200 OK\r\n'
@ -1042,7 +1117,7 @@ def test_response_update_cookies():
assert r.cookies['abc'].val == '456' assert r.cookies['abc'].val == '456'
r = http.Response() r = http.Response()
r.status_line = 'HTTP/1.1 200 OK' r.start_line = 'HTTP/1.1 200 OK'
# Test by adding cookie objects # Test by adding cookie objects
c = http.ResponseCookie('abc=123; secure') c = http.ResponseCookie('abc=123; secure')
r.cookies['abc'] = c r.cookies['abc'] = c
@ -1060,10 +1135,10 @@ def test_response_update_content_length():
def test_response_to_json(): def test_response_to_json():
rsp = http.Response() rsp = http.Response()
rsp.status_line = 'HTTP/1.1 200 OK' rsp.start_line = 'HTTP/1.1 200 OK'
rsp.rspid = 2 rsp.rspid = 2
expected_reqdata = {'full_response': base64.b64encode(rsp.full_response), expected_reqdata = {'full_message': base64.b64encode(rsp.full_response),
'rspid': rsp.rspid, 'rspid': rsp.rspid,
#'tag': r.tag, #'tag': r.tag,
} }

@ -6,7 +6,6 @@ import twisted.test
from pappyproxy import http from pappyproxy import http
from pappyproxy import macros from pappyproxy import macros
from pappyproxy import mangle
from pappyproxy import config from pappyproxy import config
from pappyproxy.proxy import ProxyClient, ProxyClientFactory, ProxyServerFactory from pappyproxy.proxy import ProxyClient, ProxyClientFactory, ProxyServerFactory
from testutil import mock_deferred, func_deleted, func_ignored_deferred, func_ignored, no_tcp from testutil import mock_deferred, func_deleted, func_ignored_deferred, func_ignored, no_tcp
@ -18,7 +17,7 @@ from twisted.internet import defer, reactor
## Fixtures ## Fixtures
MANGLED_REQ = 'GET /mangled HTTP/1.1\r\n\r\n' MANGLED_REQ = 'GET /mangled HTTP/1.1\r\n\r\n'
MANGLED_RSP = 'HTTP/1.1 500 MANGLED\r\n\r\n' MANGLED_RSP = 'HTTP/1.1 500 MANGLED\r\nContent-Length: 0\r\n\r\n'
@pytest.fixture @pytest.fixture
def unconnected_proxyserver(mocker): def unconnected_proxyserver(mocker):
@ -140,25 +139,25 @@ def gen_mangle_macro(modified_req=None, modified_rsp=None,
macro = mock.MagicMock() macro = mock.MagicMock()
if modified_req or drop_req: if modified_req or drop_req:
macro.async_req = True macro.async_req = True
macro.do_req = True macro.intercept_requests = True
if drop_req: if drop_req:
newreq = None newreq = None
else: else:
newreq = http.Request(modified_req) newreq = http.Request(modified_req)
macro.async_mangle_request.return_value = mock_deferred(newreq) macro.async_mangle_request.return_value = mock_deferred(newreq)
else: else:
macro.do_req = False macro.intercept_requests = False
if modified_rsp or drop_rsp: if modified_rsp or drop_rsp:
macro.async_rsp = True macro.async_rsp = True
macro.do_rsp = True macro.intercept_responses = True
if drop_rsp: if drop_rsp:
newrsp = None newrsp = None
else: else:
newrsp = http.Response(modified_rsp) newrsp = http.Response(modified_rsp)
macro.async_mangle_response.return_value = mock_deferred(newrsp) macro.async_mangle_response.return_value = mock_deferred(newrsp)
else: else:
macro.do_rsp = False macro.intercept_responses = False
return macro return macro
def notouch_mangle_req(request): def notouch_mangle_req(request):
@ -255,7 +254,7 @@ def test_proxy_client_mangle_rsp(mocker, proxy_connection, in_scope_true):
prot.lineReceived('') prot.lineReceived('')
req = yield retreq_deferred req = yield retreq_deferred
response = req.response.full_response response = req.response.full_response
assert response == 'HTTP/1.1 500 MANGLED\r\n\r\n' assert response == 'HTTP/1.1 500 MANGLED\r\nContent-Length: 0\r\n\r\n'
@pytest.inlineCallbacks @pytest.inlineCallbacks
def test_proxy_drop_req(mocker, proxy_connection, in_scope_true): def test_proxy_drop_req(mocker, proxy_connection, in_scope_true):

@ -5,13 +5,13 @@ from pappyproxy.http import Request, Response, ResponseCookie
@pytest.fixture @pytest.fixture
def req(): def req():
r = Request() r = Request()
r.status_line = 'GET / HTTP/1.1' r.start_line = 'GET / HTTP/1.1'
return r return r
@pytest.fixture @pytest.fixture
def rsp(): def rsp():
r = Response() r = Response()
r.status_line = 'HTTP/1.1 200 OK' r.start_line = 'HTTP/1.1 200 OK'
return r return r
def test_session_basic(req, rsp): def test_session_basic(req, rsp):
@ -96,7 +96,7 @@ def test_session_mixed(req, rsp):
assert 'auth' not in rsp.headers assert 'auth' not in rsp.headers
r = Response() r = Response()
r.status_line = 'HTTP/1.1 200 OK' r.start_line = 'HTTP/1.1 200 OK'
r.set_cookie(ResponseCookie('state=bazzers')) r.set_cookie(ResponseCookie('state=bazzers'))
r.set_cookie(ResponseCookie('session=buzzers')) r.set_cookie(ResponseCookie('session=buzzers'))
s.get_rsp(r) s.get_rsp(r)

@ -1,3 +1,24 @@
import string
class PappyException(Exception): class PappyException(Exception):
"""
The exception class for Pappy. If a plugin command raises one of these, the
message will be printed to the console rather than displaying a traceback.
"""
pass pass
def printable_data(data):
"""
Return ``data``, but replaces unprintable characters with periods.
:param data: The data to make printable
:type data: String
:rtype: String
"""
chars = []
for c in data:
if c in string.printable:
chars += c
else:
chars += '.'
return ''.join(chars)

@ -4,12 +4,12 @@ import pkgutil
from setuptools import setup, find_packages from setuptools import setup, find_packages
setup(name='pappyproxy', setup(name='pappyproxy',
version='0.1.1', version='0.2.0',
description='The Pappy Intercepting Proxy', description='The Pappy Intercepting Proxy',
author='Rob Glew', author='Rob Glew',
author_email='rglew56@gmail.com', author_email='rglew56@gmail.com',
url='https://www.github.com/roglew/pappy-proxy', url='https://www.github.com/roglew/pappy-proxy',
packages=['pappyproxy', 'pappyproxy.schema'], packages=['pappyproxy', 'pappyproxy.schema', 'pappyproxy.plugins'],
include_package_data = True, include_package_data = True,
license='MIT', license='MIT',
entry_points = { entry_points = {

Loading…
Cancel
Save