首页 > 解决方案 > Python 将文件上传到 Wordpress

问题描述

我正在尝试使用 python 将文件上传到 wordpress 站点。到目前为止,我似乎无法在没有扩展的情况下将 API 与会话 cookie 一起使用。所以在这一点上,我正在尝试跟随以下帖子

使用请求登录 Wordpress - Python3

通过 Python 的请求将图像上传到 WordPress

这是我到目前为止所拥有的。

#!/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)

运行此程序时,我得到以下响应,显然最后一个很有趣。

./upload.py
<Response [200]>
<Response [200]>
<Response [403]>

在手动上传文件后,我还尝试从 Chrome 中提取数据字段,直接发布到 async-upload.php,结果相似。

更新:我得到的响应页面具有以下标题。

<title>Something went wrong.</title>
...
<body id="error-page">
    <div class="wp-die-message">The link you followed has expired.</div> 
</body>

在挖掘页面的源代码后,我还添加了 nonce 值。这是我发现的

<input type="hidden" id="_wpnonce" name="_wpnonce" value="74bdb561c5">

这是我添加的。

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'}

仍然没有运气。我还注意到,与基于浏览器的会话相比,缺少 cookie。我将假设我实际上并没有进行身份验证。

标签: pythonwordpresspython-requestsupload

解决方案


使用了错误的随机数。要提取正确的随机数,请使用以下内容并发布到 async-upload.php。关键是从 media-new.php 页面中提取 _wpnonce 表单。

如果您不从表单参数中提取,那么您最终可能会得到十几个其他随机数之一。

test = re.search('"multipart_params":.*_wpnonce":"[0-9a-z]+"', r1.text)
nonce = re.search('(?<=_wpnonce":")[0-9a-z]{10}', test.group(0))
nonce = nonce.group(0)

完整的代码是

#!/usr/bin/python3
import sys, requests, re

f = 'test.txt'

user='user'
password='password'
url1='https://example.com/wp-login.php'
redirecturl='https://example.com/wp-admin/media-new.php'
url2='https://example.com/wp-admin/async-upload.php'

headerauth= {
        'Cookie':'wordpress_test_cookie=WP Cookie check; ROUTEID=.1',
        'Host':'example.com',
        'Content-Type': 'application/x-www-form-urlencoded'
        }
dataauth = {
        'log':user,
        'pwd':password,
        'wp-submit':'Log In',
        'redirect_to': redirecturl,
        'testcookie': 1
        }
image = {'async-upload':('test.txt', open(f, "rb"))}
testimage = open(f, "rb")

session1=requests.session()
session1.get(url1)
r1 = session1.post(url1, headers=headerauth, data=dataauth)

test = re.search('"multipart_params":.*_wpnonce":"[0-9a-z]+"', r1.text)
nonce = re.search('(?<=_wpnonce":")[0-9a-z]{10}', test.group(0))
nonce = nonce.group(0)

uploadheaders = {
        'Connection': 'keep-alive',
        'Referer': 'https://example.com/wp-admin/upload.php',
        'Sec-Fetch-Dest': 'empty',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Site': 'same-origin'
        }
dataupload = {
        'name': 'test.txt',
        'action': 'upload-attachement',
        '_wpnonce': nonce,
        'wpmf_folder': '0',
        }

r2 = session1.post(url2, data=dataupload, headers=uploadheaders, files=image)

推荐阅读