I am trying to upload files to a wordpress site using python. So far it looks like I can not use the API with session cookies without an extension. So at this point I am trying to follow along with the following post
Login Wordpress with requests - Python3
Uploading of image to WordPress through Python's requests
Here is what I have so far.
#!/usr/bin/python3
import sys, requests
f = 'test.txt'
user='username'
password='password'
url1='https://example.com/wp-login.php'
url2='https://example.com/wp-admin/media-new.php'
headerauth= {'Cookie':'wordpress_test_cookie=WP Cookie check'}
dataauth = {'log':user, 'pwd':password, 'wp-submit':'Log In'}
dataupload = {'post_id': '0', '_wp_http_referer': '/wp-admin/media-new.php', 'action': 'upload_attachement', 'html-upload': 'Upload'}
image = {'async-upload':('test.txt', open(f, "rb"))}
session1=requests.session()
r1 = session1.post(url1, headers=headerauth, data=dataauth)
print(r1)
r2 = session1.get(url2)
print(r2)
r3 = session1.post(url2, data=dataupload, files=image)
print(r3)
When running this I get the following responses, obviously the last one is of interest.
./upload.py
<Response [200]>
<Response [200]>
<Response [403]>
I have also tried pulling the data fields from Chrome after manually uploading a file, posting directly to async-upload.php with similar results.
Update: The response page I get has the following title.
<title>Something went wrong.</title>
...
<body id="error-page">
<div class="wp-die-message">The link you followed has expired.</div>
</body>
I also added the nonce value after digging around the source for the page. This is what I found
<input type="hidden" id="_wpnonce" name="_wpnonce" value="74bdb561c5">
This is what I added.
r2 = session1.get(url2)
test = re.search('value="[0-9a-z]{10}"', r2.text)
nonce = re.search('[0-9a-z]{10}', test.group(0))
nonce = nonce.group(0)
dataupload = {'post_id':'0', '_wp_http_referer':'/wp-admin/media-new.php', '_wpnonce':nonce, 'action':'upload_attachement', 'html-upload':'Upload'}
Still no luck. I also noticed that there is a lack of cookies when compared to my browser based sessions. I am going to assume I am not actually authenticating.