Python Forum
How to intercept and modify form POST values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to intercept and modify form POST values
#1
I have a simple form on a remote webserver:

<form action="post.php" method="post">
    <input type="text" name="p" value="abc">
    <input type="submit">
</form>
I load the remote page on my local browser by calling the url as usual.

The target file post.php simply print the value of the POST param:

Output:
Array ( [p] => abc )


I need a Python code that:
  1. Runs on my local computer (the same one where the browser runs),
  2. Intercept the POST sent by the browser, modify the param p value (let's say xyz), and lastly
  3. Send the results to the original remote post.php, so that it will print back:
    Output:
    Array ( [p] => xyz )

I think it can be done by a Python proxy that acts as mitm (even using libraries, may be mitmproxy?).
I'll configure my browser to point to this local python proxy.
I am a newbie and don't know how to start: a simple code will help me to understand.

Thank you in advance for your time,
Gilberto
Reply
#2
I guess the first step is getting a proxy server that just works, and after that try modifying post data. To that end, here's a pretty simple example: http://effbot.org/librarybook/simplehttpserver.htm
Quote:
# a truly minimal HTTP proxy

import SocketServer
import SimpleHTTPServer
import urllib

PORT = 1234

class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler):
    def do_GET(self):
        self.copyfile(urllib.urlopen(self.path), self.wfile)

httpd = SocketServer.ForkingTCPServer(('', PORT), Proxy)
print "serving at port", PORT
httpd.serve_forever()
Reply
#3
Thank you @nilamo, I'm trying the code right now.

One note: the code is not for Python 3.6.5 (I have this version).

SocketServer becomes socketserver

I get the error:
Error:
ModuleNotFoundError: No module named 'SimpleHTTPServer'
I don't know how to adapt the code to v. 3.6.5

Any help?
Reply
#4
https://docs.python.org/3.7/library/http...estHandler
Reply
#5
Thank you. I'll study the doc.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020