简体   繁体   中英

How to send a POST request to a .php page in python

Context: So I'm trying to build a python program that will send a POST request to a specific .php file, and return the output. I've done a little bit of research, and this is the code I have so far:

def ForcePush():
 params = urllib.urlencode({'log': 'admin', 'pwd':'password'})
 headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain", "Accept-Language":"en-Us,en;q=0.5" ,
 "Referer":"http://192.168.18.138/wp-login.php"}
 conn = httplib.HTTPConnection(raw_input("Where would you like to browse to: "))

 conn.request("POST", "", params, headers)
 response = conn.getresponse()
 data = response.read()
 print data
 conn.close()

The code works fine for a normal website, like www.google.com, but if I try to go to a php page I get this error:

Traceback (most recent call last):
File "WPEnum.py", line 24, in <module>
ForcePush()
File "WPEnum.py", line 18, in ForcePush
conn.request("POST", "", params, headers)
File "C:\Python27\lib\httplib.py", line 1057, in request
self._send_request(method, url, body, headers)
File "C:\Python27\lib\httplib.py", line 1097, in _send_request
self.endheaders(body)
File "C:\Python27\lib\httplib.py", line 1053, in endheaders
self._send_output(message_body)
File "C:\Python27\lib\httplib.py", line 897, in _send_output
self.send(msg)
File "C:\Python27\lib\httplib.py", line 859, in send
self.connect()
File "C:\Python27\lib\httplib.py", line 836, in connect
self.timeout, self.source_address)
File "C:\Python27\lib\socket.py", line 557, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 11001] getaddrinfo failed

In case you are wondering, I'm making the program to enumerate the WPAdmin for the Mr.Robot vulnverable VM on VMWare. Doing this for educational purposes. This is the request I'm trying to emulate:

POST /wp-login.php HTTP/1.1
Host: 192.168.18.138
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101   Firefox/38.0 Iceweasel/38.8.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: https://192.168.18.138/wp-login.php
Cookie: s_fid=2692E4153C7D3D30-158A9B35CCC16635; s_nr=1473166726975; s_cc=true; s_sq=%5B%5BB%5D%5D; wordpress_test_cookie=WP+Cookie+check
Connection: close
Content-Type: application/x-www-form-urlencoded
Content-Length: 104

log=admin&pwd=login&wp-submit=Log+In&redirect_to=https%3A%2F%2F192.168.18.138%2Fwp-admin%2F&testcookie=1

I know I don't have all of the headers, but that doesn't seem to be what the error is suggesting is wrong. Does anyone know what the problem is?

As you see from exception, you have connection-related error (on opening socket), so obviously host/port you're using in request is wrong, or inaccessible from python environment (and obviously from system) for some reason.

To debug try to get same page with curl or wget (even with GET method). curl http://my_host:my_port/my_page.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM