Jump to content

Uploading files with AutoIT in Wordress


Recommended Posts

Hello everyone!

I am trying to make a script that uploads files for me in wordpress(All I need to do is to manually choose which files to upload)
Here is my script so far:
 

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>

Upload()

Func Upload()
    
    $sPD = "log=username&pwd=mypassword&wp-submit=Log+ind&redirect_to=http%3A%2F%2Fmyurl.com%2Fwp-admin%2F&testcookie=1"
oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
$oHTTP.Open("POST", "http://myurl.com/wp-login.php " & $sUSR, False) ; Post url
$oHTTP.SetRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
$oHTTP.SetRequestHeader("Accept-Language", "en-US,en;q=0.5")
$oHTTP.SetRequestHeader("User-Agent", "User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.52 YaBrowser/15.12.2490.3614 (beta) Yowser/2.5 Safari/537.36")
$oHTTP.SetRequestHeader("Referer", "http://myurl.com/wp-login.php?redirect_to=http%3A%2F%2Fmyurl.com%2Fwp-admin%2F&reauth=1")
$oHTTP.SetRequestHeader("Cookie", "wordpress_test_cookie=WP+Cookie+check")
$oHTTP.SetRequestHeader("Connection", "keep-alive")
$oHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded") ; Header data >


$oHTTP.Send($sPD)
$oHTTP.Open("GET", "http://myurl.com/wp-admin/media-new.php", False) ; Post url
$oHTTP.SetRequestHeader("Host", "myurl.com")
$oHTTP.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0")
$oHTTP.SetRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
$oHTTP.SetRequestHeader("Accept-Language", "en-US,en;q=0.5")
$oHTTP.SetRequestHeader("Referer", "http://myurl.com/wp-admin/")
$oHTTP.Send()

Endfunc

The upload button = "Choose Files"

Example code from:

https://www.autoitscript.com/autoit3/docs/functions/FileOpenDialog.htm

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>

Example()

Func Example()
    ; Create a constant variable in Local scope of the message to display in FileOpenDialog.
    Local Const $sMessage = "Select a single file of any type."

    ; Display an open dialog to select a file.
    Local $sFileOpenDialog = FileOpenDialog($sMessage, @WindowsDir & "\", "All (*.*)", $FD_FILEMUSTEXIST)
    If @error Then
        ; Display the error message.
        MsgBox($MB_SYSTEMMODAL, "", "No file was selected.")

        ; Change the working directory (@WorkingDir) back to the location of the script directory as FileOpenDialog sets it to the last accessed folder.
        FileChangeDir(@ScriptDir)
    Else
        ; Change the working directory (@WorkingDir) back to the location of the script directory as FileOpenDialog sets it to the last accessed folder.
        FileChangeDir(@ScriptDir)

        ; Replace instances of "|" with @CRLF in the string returned by FileOpenDialog.
        $sFileOpenDialog = StringReplace($sFileOpenDialog, "|", @CRLF)

        ; Display the selected file.
        MsgBox($MB_SYSTEMMODAL, "", "You chose the following file:" & @CRLF & $sFileOpenDialog)
    EndIf
EndFunc   ;==>Example

The HTML:
 

<p class="drag-drop-buttons"><input style="position: relative; z-index: 1;" id="plupload-browse-button" value="Choose Files" class="button" type="button"></p>

Some header info in the HTTP request:
 

POST http://myurl.com/wp-admin/async-upload.php HTTP/1.1
Host: myurl.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.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: http://myurl.com/wp-admin/media-new.php
Content-Length: 4555551
Content-Type: multipart/form-data; boundary=---------------------------12345678910
Cookie: LONG COOKIE HERE!
DNT: 1
Connection: keep-alive

-----------------------------102062144610664
Content-Disposition: form-data; name="name"

hdfhfg.jpg
-----------------------------102062144610664
Content-Disposition: form-data; name="post_id"

0
-----------------------------102062144610664
Content-Disposition: form-data; name="_wpnonce"

123456
-----------------------------102062144610664
Content-Disposition: form-data; name="type"


-----------------------------102062144610664
Content-Disposition: form-data; name="tab"


-----------------------------102062144610664
Content-Disposition: form-data; name="short"

1
-----------------------------102062144610664
Content-Disposition: form-data; name="async-upload"; filename="hdfhfg.jpg"
Content-Type: image/jpeg

This wouldn't be much of a problem if I just had some simple post data, but this makes my head spin....

Thanks in advance Masters!

(I could probably easily do this with IE, but I'd rather not use IE, I am not a big fan of it)
 

Link to comment
Share on other sites

  • Moderators

@RyukShini you posted what you want to do, and where you pulled your code from, but I see nothing regarding what is not working for you. Are you getting an error? Are the files just not uploading? How about some actual detail rather than having us guess ;)

 

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

3 hours ago, JLogan3o13 said:

@RyukShini you posted what you want to do, and where you pulled your code from, but I see nothing regarding what is not working for you. Are you getting an error? Are the files just not uploading? How about some actual detail rather than having us guess ;)

 

Everything is working, I made all this happen without a problem.
Now my problem is that I don't have a clue about this form of data on how to implement it in a http request with AutoIT:
 

-----------------------------102062144610664
Content-Disposition: form-data; name="name"

hdfhfg.jpg
-----------------------------102062144610664
Content-Disposition: form-data; name="post_id"

0
-----------------------------102062144610664
Content-Disposition: form-data; name="_wpnonce"

123456
-----------------------------102062144610664
Content-Disposition: form-data; name="type"


-----------------------------102062144610664
Content-Disposition: form-data; name="tab"


-----------------------------102062144610664
Content-Disposition: form-data; name="short"

1
-----------------------------102062144610664
Content-Disposition: form-data; name="async-upload"; filename="hdfhfg.jpg"
Content-Type: image/jpeg

Normally a post data would simple look like this:
File=image.jpg&somedata=oakdoaksd
or in another script I made at some point:
fileurl=http://imur.com/oksokasd.png&somedata=blabla

But this is not the case here, I am not sure what to do with this sort of data.


Thanks in advance @JLogan3o13

(I coded all of it, learned from this forum and brugbart.dk, made many other web based scripts like these, but never with such HTTP Headers/Post info)

Link to comment
Share on other sites

np

It could be like this:

#include "WinHttp.au3"


$sDomain = "http://myurl.com/"
$sLoginPage = "/wp-login.php"
$sUploadPage = "/wp-admin/media-new.php"

$sLoginName = "username"
$sPassword = "mypassword"

; Choose file to upload
$sFileToUpload = FileOpenDialog("Choose something (Image better maybe?)", "", "All (*.*)")
If $sFileToUpload Then
    $hOpen = _WinHttpOpen()
    $hConnect = _WinHttpConnect($hOpen, $sDomain)

    ; Login first
    _WinHttpSimpleFormFill($hConnect, _
            $sLoginPage, _
            "loginform", _
            'user_login', $sLoginName, _
            'user_pass', $sPassword)

    ConsoleWrite('! Error for login = ' & @error & @LF)

    _WinHttpSimpleFormFill($hConnect, _
            $sUploadPage, _
            "file-form", _
            'async-upload', $sFileToUpload)

    ConsoleWrite('! Error for upload = ' & @error & @LF)


    _WinHttpCloseHandle($hConnect)
    _WinHttpCloseHandle($hOpen)
EndIf

 

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

19 hours ago, trancexx said:

np

It could be like this:

#include "WinHttp.au3"


$sDomain = "http://myurl.com/"
$sLoginPage = "/wp-login.php"
$sUploadPage = "/wp-admin/media-new.php"

$sLoginName = "username"
$sPassword = "mypassword"

; Choose file to upload
$sFileToUpload = FileOpenDialog("Choose something (Image better maybe?)", "", "All (*.*)")
If $sFileToUpload Then
    $hOpen = _WinHttpOpen()
    $hConnect = _WinHttpConnect($hOpen, $sDomain)

    ; Login first
    _WinHttpSimpleFormFill($hConnect, _
            $sLoginPage, _
            "loginform", _
            'user_login', $sLoginName, _
            'user_pass', $sPassword)

    ConsoleWrite('! Error for login = ' & @error & @LF)

    _WinHttpSimpleFormFill($hConnect, _
            $sUploadPage, _
            "file-form", _
            'async-upload', $sFileToUpload)

    ConsoleWrite('! Error for upload = ' & @error & @LF)


    _WinHttpCloseHandle($hConnect)
    _WinHttpCloseHandle($hOpen)
EndIf

 

Makes perfectly sense @trancexx
Thank you so much, I will try to finish my script now!

 

EDIT:
 

! Error for login = 4
! Error for upload = 3

I am looking through WinHTTP.au3 now to find out what this error means, but any help is appreciated.

Using simpleformfill is it possible to add costum headers?
I don't really see an example in the help file with costum Headers.

Thanks in advance @trancexx

Edited by RyukShini
error for login & upload
Link to comment
Share on other sites

21 hours ago, trancexx said:

Of course it's possible, but shouldn't really be needed in case of usual wordpress installation.

 

Check addresses. Maybe there's specific htaccess configuration on the server in place which redirects in case of .php access.

Okay, I will be sure to check the redirects and what not.
Thanks a lot! ;)

 

I changed something in the wp-login.php?redirect thingy
but still no luck I get errors
! Error for login = 4
! Error for upload = 3 (This error being understandable since we aren't logged in)

What does 4 error in "Error for login = 4????" stand for?
Thanks in advance @trancexx

Edited by RyukShini
Link to comment
Share on other sites

21 hours ago, RyukShini said:

Okay, I will be sure to check the redirects and what not.
Thanks a lot! ;)

 

I changed something in the wp-login.php?redirect thingy
but still no luck I get errors
! Error for login = 4
! Error for upload = 3 (This error being understandable since we aren't logged in)

What does 4 error in "Error for login = 4????" stand for?
Thanks in advance @trancexx

It means server isn't serving you for some reason. Could you create new user and send me credentials via mail, I would like to see it first hand. I won't touch anything else, no worries.

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

3 hours ago, trancexx said:

It means server isn't serving you for some reason. Could you create new user and send me credentials via mail, I would like to see it first hand. I won't touch anything else, no worries.

Sorry I am not able to do so.
It is not my company, but I have another wordpress site I personally own, same principle?
you could try using that if you want.

You have messages disabled?
I can't sent it to you.

e80780a4c9d755054e33b32c941cf25e.png

Edited by RyukShini
Cannot receive messages
Link to comment
Share on other sites

2 hours ago, RyukShini said:

Replied, thanks in advance.

Well, it works for me.
If you check uploads you'll see mine. I used this exact code (with my credentials):

#include "WinHttp.au3"

$sLoginPage = '/wp-login.php'
$sUploadPage = '/wp-admin/media-new.php'


$sDomain = 'theyogapants.club'
$sLogin = 'that_username'
$sPassword = 'that_passwrd'


$sFileToUpload = FileOpenDialog("Choose something (Image better maybe?)", "", "All (*.*)")


$hOpen = _WinHttpOpen()
$hConnect = _WinHttpConnect($hOpen, $sDomain)

; Login first
_WinHttpSimpleFormFill($hConnect, _
        $sLoginPage, _
        "loginform", _
        'user_login', $sLogin, _
        'user_pass', $sPassword)

ConsoleWrite('! Error for login = ' & @error & @LF)


$sResponse = _WinHttpSimpleFormFill($hConnect, _
        $sUploadPage, _
        "file-form", _
        'async-upload', $sFileToUpload)

ConsoleWrite('! Error for upload = ' & @error & @LF)


_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)

 

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

3 hours ago, trancexx said:

Well, it works for me.
If you check uploads you'll see mine. I used this exact code (with my credentials):

#include "WinHttp.au3"

$sLoginPage = '/wp-login.php'
$sUploadPage = '/wp-admin/media-new.php'


$sDomain = 'theyogapants.club'
$sLogin = 'that_username'
$sPassword = 'that_passwrd'


$sFileToUpload = FileOpenDialog("Choose something (Image better maybe?)", "", "All (*.*)")


$hOpen = _WinHttpOpen()
$hConnect = _WinHttpConnect($hOpen, $sDomain)

; Login first
_WinHttpSimpleFormFill($hConnect, _
        $sLoginPage, _
        "loginform", _
        'user_login', $sLogin, _
        'user_pass', $sPassword)

ConsoleWrite('! Error for login = ' & @error & @LF)


$sResponse = _WinHttpSimpleFormFill($hConnect, _
        $sUploadPage, _
        "file-form", _
        'async-upload', $sFileToUpload)

ConsoleWrite('! Error for upload = ' & @error & @LF)


_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)

 

First of all, amazing support, second of all so simple!
will do this first thing in the morning, thanks.

Link to comment
Share on other sites

17 hours ago, trancexx said:

Well, it works for me.
If you check uploads you'll see mine. I used this exact code (with my credentials):

#include "WinHttp.au3"

$sLoginPage = '/wp-login.php'
$sUploadPage = '/wp-admin/media-new.php'


$sDomain = 'theyogapants.club'
$sLogin = 'that_username'
$sPassword = 'that_passwrd'


$sFileToUpload = FileOpenDialog("Choose something (Image better maybe?)", "", "All (*.*)")


$hOpen = _WinHttpOpen()
$hConnect = _WinHttpConnect($hOpen, $sDomain)

; Login first
_WinHttpSimpleFormFill($hConnect, _
        $sLoginPage, _
        "loginform", _
        'user_login', $sLogin, _
        'user_pass', $sPassword)

ConsoleWrite('! Error for login = ' & @error & @LF)


$sResponse = _WinHttpSimpleFormFill($hConnect, _
        $sUploadPage, _
        "file-form", _
        'async-upload', $sFileToUpload)

ConsoleWrite('! Error for upload = ' & @error & @LF)


_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)

 

Received this:

! Error for login = 1
! Error for upload = 3

:(

 

I checked the HTML/PHP on both pages and they are identical in the upload stage, no idea whats wrong.

Works for Yoga, does not work for other site(will check headers, cookies and everything)
Headers, Cookies are also identical(except for the domain of course)

 

Might be due to an extra layer of security and I may need to use user-agent in my header.
how would you add a costum header in the simpleformfill ?
Thanks in advance

Edited by RyukShini
user-agent
Link to comment
Share on other sites

6 hours ago, RyukShini said:

Might be due to an extra layer of security and I may need to use user-agent in my header.
how would you add a costum header in the simpleformfill ?

If you want another user agent then do this:

$hOpen = _WinHttpOpen("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586")

...and it will be added to all requests that use that $hOpen.

Also you can add whatever header with _WinHttpSimpleFormFill:

_WinHttpSimpleFormFill($hConnect, _
        $sLoginPage, _
        "loginform", _
        'user_login', $sLogin, _
        'user_pass', $sPassword, _
        "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586")

You can also add more than one, just use @CRLF to separate each other:

_WinHttpSimpleFormFill($hConnect, _
        $sLoginPage, _
        "loginform", _
        'user_login', $sLogin, _
        'user_pass', $sPassword, _
        "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586" & @CRLF & _
        "Referer: http://google.com" & @CRLF & _
        "DNT: 1")

 

Edited by trancexx

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

18 hours ago, trancexx said:

If you want another user agent then do this:

$hOpen = _WinHttpOpen("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586")

...and it will be added to all requests that use that $hOpen.

Also you can add whatever header with _WinHttpSimpleFormFill:

_WinHttpSimpleFormFill($hConnect, _
        $sLoginPage, _
        "loginform", _
        'user_login', $sLogin, _
        'user_pass', $sPassword, _
        "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586")

You can also add more than one, just use @CRLF to separate each other:

_WinHttpSimpleFormFill($hConnect, _
        $sLoginPage, _
        "loginform", _
        'user_login', $sLogin, _
        'user_pass', $sPassword, _
        "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586" & @CRLF & _
        "Referer: http://google.com" & @CRLF & _
        "DNT: 1")

 

Amazing I am gonna try.
However I found out it might not be the error afterall.
It may be due to the wordpress installation being in Danish on the other site, but still the IDs, Class, all of the HTML code is still in English of course, so I am not sure if WinHTTP takes that in to consideration?
Will try to fool around with it a bit.

 

<form name="loginform" id="loginform" action="http://url.dk/wp-login.php" method="post">
    <p>
        <label for="user_login">Brugernavn eller e-mail<br />
        <input type="text" name="log" id="user_login" class="input" value="" size="20" /></label>
    </p>
    <p>
        <label for="user_pass">Adgangskode<br />
        <input type="password" name="pwd" id="user_pass" class="input" value="" size="20" /></label>
    </p>
        <p class="forgetmenot"><label for="rememberme"><input name="rememberme" type="checkbox" id="rememberme" value="forever"  /> Husk mig</label></p>
    <p class="submit">
        <input type="submit" name="wp-submit" id="wp-submit" class="button button-primary button-large" value="Log ind" />
        <input type="hidden" name="redirect_to" value="http://url.dk/wp-admin/" />
        <input type="hidden" name="testcookie" value="1" />
    </p>
<form name="loginform" id="loginform" action="http://url.com/wp-login.php" method="post">
    <p>
        <label for="user_login">Username or Email<br />
        <input type="text" name="log" id="user_login" class="input" value="" size="20" /></label>
    </p>
    <p>
        <label for="user_pass">Password<br />
        <input type="password" name="pwd" id="user_pass" class="input" value="" size="20" /></label>
    </p>
        <p class="forgetmenot"><label for="rememberme"><input name="rememberme" type="checkbox" id="rememberme" value="forever"  /> Remember Me</label></p>
    <p class="submit">
        <input type="submit" name="wp-submit" id="wp-submit" class="button button-primary button-large" value="Log In" />
        <input type="hidden" name="redirect_to" value="http://url.com/wp-admin/" />
        <input type="hidden" name="testcookie" value="1" />
    </p>

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...