Jump to content
Sign in to follow this  
bLaZiN

Saving PHP generated captcha to disk?

Recommended Posts

bLaZiN

hi ladies and gents!

i am trying to write a semi-automized download manager for myself. the site which i am downloading from protects it's links with captchas. i am sending a GET request to it and get back a lot of HEX stuff in which even the "%png [...]" stuff is in it. now i am trying to write the png stuff into a file called captcha.png, then there should pop up a dialogue with the captcha and some input field so i can simply type in the captcha in the program instead of the website.

here is how i tried to realize the GET thingy:

Func _writeCaptchaPNG($bytes)
    $png = FileOpen('captcha.png', 2)
    FileWrite($png, $bytes)
    FileClose($png)
EndFunc

Func _SendGet3($ID, $PHPSESSID)
    $start3 = 1
    $sock3 = TCPConnect(TCPNameToIP('www.duckload.com') ,80)
    $GET3  = 'GET /design/Captcha2.php?key='&$ID&' HTTP/1.1'&@CRLF
    $GET3 &= 'Host: duckload.com'&@CRLF
    $GET3 &= 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.0.11) Gecko/2009060215 Firefox/3.0.11 (.NET CLR 3.5.30729)'&@CRLF
    $GET3 &= 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'&@CRLF
    $GET3 &= 'Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3'&@CRLF
    $GET3 &= 'Accept-Encoding: gzip,deflate'&@CRLF
    $GET3 &= 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7'&@CRLF
    $GET3 &= 'Keep-Alive: 300'&@CRLF
    $GET3 &= 'Connection: keep-alive'&@CRLF
    $GET3 &= 'Cookie: PHPSESSID='&$PHPSESSID&';'&@CRLF
    $GET3 &= ''&@CRLF
    TCPSend($sock3, $GET3)
    MsgBox(0, "", "sent once..")
EndFunc

[...]

    If($start3 = 1) Then
        $recv3 = TCPRecv($sock3, 24576)
        If($recv3 <> "") Then
            MsgBox(0, "", $recv3)
            _writeCaptchaPNG($recv3)
        EndIf
    EndIf

[...]

and here it what is written into the 'captcha.png' when i open it in editor:

HTTP/1.1 200 OK
X-Powered-By: PHP/5.2.0-8+etch13
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-type: image/png
Content-Length: 3766
Date: Fri, 19 Jun 2009 18:04:40 GMT
Server: duckload

PNG

   
IHDR   ú   P   µ  JPLTEÿÿÿ"Ò'ÕóÞ¬cW¹áIDûèç÷ÑÐð¤¡ä_[ìóº¸èvr>ÃݲloU²ÇàUGÞ_Þ[àaK§akôëìÈ¢²t}é×ÚÞÃÇQ9«½Ó°µ~G¸Ñg¯o|YwsIbA/)ÅQwW©Àk~u{ftv Ds«¸ÊWrs§ÇÐÜ;g;[ãçí¡¹"z£%°Ó$Ã!h#³&Âã V*º3¢.®P°&L{-UBpÆe7Qqgkmñz@ýîçùÍ·ö«òWø¼ûÝÏôo¡YvÖoR¼ddázâZîDéMìH?òòçæåÏÚÙ·©¦Wµ³oÁ¿ÎÌ=@­}|c5ÍÜS¾¯p°¨lb·¡UƨJ÷ôèðéÑâÓ¤éÞ»ÛÈÔ½wͲ`î  
'IDATxíçskGÀW½y¨«^!ÉxLlY-Õ,fhVÿÿ¿²{¶·ëk_'ã!9Þ³öùwÏÙÓv¯Qêk+(QºYö9¿,¡|µè/ü¹Ñ¸ÛKGÏ$»_¼(r¯Ö±GÉÿ7zÖ_¹®Yì/K¾A·${ýû7è®<?û#ÿ²Ð«ÎðW¾j({:;ܯf³gQû²Óiw|[!³yäám|öÇ¡ç¾aîW·Ð»¿èµ¨þéÓTººÝܯ¬åíênµZ.Wwk÷!lvÛÿ¦ýÞ~(ÁÞÃ^(úµ©]6êúXa!ØV¯í»0-1ìáÍxB~ºÒág«[CîfæMv÷û½¥øÍv8Þßo^(çr¹r¡R)øÏçÂÐk¸ÒrìR·.Ý~»Õê)ÍëÀL Êôý?¼qüùòÖþåû{.§ÓÉIûØè:{è;G¥\yÀä³pË*Æ
>h¹òËw¿Bè£_kÄL&[©4ú£]Qìk.9
9ÇØc¢çË;«¨fz
c$ÑÛ·÷îcõóÀ5ûÉÔÆ&W³}¡ëy¾^9zß8-ùëÚQu,tƯqpÃÐØ
_HD°`|.Ñ»ªuý÷Ý«¯&Éxj«ý3«Ù=»cìrd{bØHèû°Ódû8ôçά^Å^µzc|AÑé~'Yéüî+øKÏåW#ÛØGãñ\ZäHÌý;þisÔ°uç3þdÙ}T&S,3£íÛªT¯^òtú=þ>ÅÊÚì>óyÞï'WYþT?xç/7GnçÝádñvvÈDGOsçѬañzh"äï+ßÞ§¶823m[/
Ow°UNu}Øy½\to.#ã×/0þaÚ,üû>!>÷kL×´W®cWë±dÇN¼¡ñ|wOl=ãD¡ÓßÀ¿õmЦAùƤ­ÐijôªtÓÀÁÇìè'öPûdê Ò¾>c%Z/ÐIËõ1}ðåøx~NÉk5 SÝôçÝhµO®ôÂ5¡_f´[jáQ}½Â&¹?y«2/®KVA?BõÞoùÄ#ù8ðÍWÈAwæ-\ÖzhßS¦KMóï²7¢öº9ÂÑIÐ視AÑ{~¥#1Þ~«ñðÆD'ç=u­Ã ïÞó\    J9ÖJß
]GÕÞÐyõ:øækê²lqc8;Ô±óX!áÈ@©¾òºÖ"; o¡Wr¥
ÍÚáºLéWî
ÞkmoÞ¾N¥É§ß´~è¸'~à,c±ÿúópø9ü¦òçw] oÜíè\1Æ<\æl1¿Ã+=òzÒ2ÆnQZ>ôB9æÅp{§:ǵæ+Âÿ~àsr üH}ê¼7¶¹è( z[.Z@íµ*ß5ùQø8@oÖ)ÆĦº·5Ñç¡ÝÏn¡Gº¹
ì¤èDèD?c?úÙÏkU-´c
Ы¸ÊÐ,Ò·ø~ÝÊäHºò·;.yзPó£¥Òè¸J(ß:70ôl7*k»ùmÈæ¹¾¾½ó§4ô¨â¢ä±Íq<Kv/zbºé>öÂÍ&å h/jç½,OìÐyØG7#SóBèõsKÝàãîpÜùs:/:(ýÌÿóËè\Y¼@oâF»ýT¼©¶:æ²i`×æ·Ko®>33\ס[öÇ{V·Y/:(½èëÕ!´C2+W.êÕ:dªw5pد!ÓGô¦&ÓõÚÝ=©Úv§ãnGÑûý.P»y=< «äÝcñ)R ÑÄÍLfëµì´¸÷:xåâAlökHâ'Ú´*¶ÞK3Í#6~$©,Íäe¯âðsÞ¸n·'\vÚu­WÌ6p£Á&¶ æBGz»æaô9oÇêsÖTÆ°s²ssP³é«
^'Nâ7±qÍÞk¸Áé{QTDtCâtM&ºhÈz^¨¶$Cßì<}9ÖzÖÏ/çS;˪uDnïàè²½Zi2Ð-v:-Öú5µÁµ®iü$»5ô¡üLõÒÙCèÀmn´K[÷¡ÞD7Ù}èOx¦yñ4¿Ýn¡9w¢ä-x=OC[¹3óx\U'¬Ì61Ëc©Ë¦émA7دüèÈZs³`xsG6)^)$¶R0ëÃi#JO¡èàæ ëì!t¥wM´u7þ Æ­ul6ºÃήªaÌ4=*ÿÞ~ºÆDGs¿ZÙn£ã­±»sV.ù¢ÑxÐy£k
̺:£È"ofi£Øèî.Ï!ÀÏÐßg#ØèÔ¿#Gë6{çÔÆyùvcáAD/}OÜèa­²´+Ùdè4G¶ó:ùspçÂâΩÛõ'µÈ$±°«§4B»ÒÅha$C§½XbõNõb²Ó«21Áü³Bwrî§2Ê·E§FÆÙÇñÐ4Ñ1кÂ@pÐI2W0³9Õ±§ýwÚ@¯Ýõshuøôî\2ôáõ~cÆ--tLë¤p+ú
v¸%o¬|kbÆÞÔÑû>?ÇEo ÆVÆØc¢¯îV·Ïiðù2 ;®Ú©£«sÎ&>ÂÕ¬Hm4ò¢,ê#®ôA¢w®tZ¯Çøí+«?=O,ds¹s®Wí?ÕtdÎtyJÛÈâ³ørp)ÌE·w1¯µO¾%ÅÙ37"¹RÅתpÑi2[§ÉlúC6Óq-k¢ú4<±öJF5x;M´Nÿ¿16yè.í>)zZ¼§A¥³t®N4O*wØ9?d ÒÑ"{6Co Ýi6×úì:FCÿDpÄZL^Ì|~ÎN
têßΫÜÚq©mpô¾jGSgþøYK¸³ =tÍà½ûCiFcö*·±3ø¨¶¤³ìü2fñ"SëÎÀ/ÕÅ
ýÁg;xW½$[Á³-tî®KÐntx)57ÿ6:ѹHãHJ{þSóæbõjúÖh©÷v 0?[Ø'P¹ó,¿&E/ð-cQN_V§.Y;-hÓóå«Ò×µû¾]3c­Èr¹XÜZü3«jy.tk ò #¥s\mü̾¹(ÌzYs#,gÿüóÎ_]µOØЧ±ííÍ¡zµ½~Rô|NJÙ²zÉ..k`¥ô×-Â
¤»Ê¿["©\AZóÀ4﹯3x®kè6¼¤½7ÐOì-)eÛZ.ýëß?²M~
*'³Ý¼¸´åÂ1<|ÌmþåFÚÖàõCûVÃÀxJm~ ?Èm    /GÔ0ÜÇ¿*3;¾ïîV`V þ§ÑâzÌUf?z©S§Ù]QNzWíLp#[ÅoRæø`nGý¶x!fy]µ-ö¿Ç#¡òáÔMoE÷yõ¯óÝü:²Ðgbh Ýó Å9W{êÍÏí»*éuéV"á^[[¡ü#ëVóÅÂîÇzÖàQµúÑÏò×HræñËÅÊ÷t**¬c½[`0ÛvÈÁø»¬7öC#A®6*³µû
çåLë4 ç+Ųð¥Ìéy¢'¯{óö->oÔ½os1t}0>/å:VÔ,pvÔ1øÙb½þϹosMü>´¡DJ¾P)ëÅò÷' ¿.oùTï±ÑàHWZ}Ïxç¹¾eëôãyß 3öúÃr=[zvYÀÊC"7gI¾R>+åJåb]vi×ѨÀ+|)­ÙmïÆxõ%1ê^¦D3pokO    O[Ü7ÝâÿÁ<Øé¬Èy]èåÅTÜ·afF×Æ×éW|²*bÈ#ÿV1ytVÈ°+uIÎÄzñ
ÇNdÿÕÎ'ý=Ü~ÛÍü¾L,?RöJ2ÈD ÜÞ}µ/BÈY~·H®_]{â_&Nxý'Ëÿ.©ö(sO=WqÕsÒIè5é$u¯OF¯WOýkD
§mý&[?ðà`újýJeÎYþ
OhIï"Ë#òy|ýä|pÎÐKÙ  IEND®B`

in "MsgBox(0, "", $recv3)" the whole stuff that i receive after the GET request was shown encoded in HEX.

i tried to remove all the stuff above "PNG" manually via editor and view the picture then but it seems i am doing it wrong somehow and it says invalid picture, can't be shown, when opening.

what am i doing wrong? any easier way of saving the *.png image which i receive as answer of the GET request, to disk?

regards blazing

Share this post


Link to post
Share on other sites
oMBRa

look at this:

#include <IE.au3>
#include <Clipboard.au3>
#include <GDIPlus.au3>
#Include <Memory.au3>
$oIE = _IECreate('http://www.autoitscript.com/', 0, 0)
$oImg = _IEImgGetCollection($oIE, 4)
$oRange = $oIE.document.Body.CreateControlRange()
$oRange.Add($oImg)
$oRange.ExecCommand('Copy')

$hImage = _ClipBoard_GetData($CF_BITMAP)

_GDIPlus_Startup()
$oBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hImage)
$nHeight = _GDIPlus_ImageGetHeight($oBitmap)
$nWidth = _GDIPlus_ImageGetWidth($oBitmap)
$Gui = GUICreate('IE Image', $nWidth, $nHeight, 250, 250)
GUISetState()
$hGraphic = _GDIPlus_GraphicsCreateFromHWND($Gui)
_GDIPlus_GraphicsDrawImage($hGraphic, $oBitmap, 0, 0)

Sleep(5000)

_GDIPlus_ImageDispose($oBitmap)
_GDIPlus_GraphicsDispose($hGraphic)
_GDIPlus_Shutdown()

you can easily change it to suits your needs

Share this post


Link to post
Share on other sites
crashdemons

This does not necessarily require an IE object, you can use a TCP-based HTTP library written by forum users to get the response data.

Or, alternatively, you can use the WinHTTP COM object:

$oHTTP=ObjCreate("winhttp.winhttprequest.5.1")
$oDATA=ObjCreate("ADODB.Stream")
$oERRO=ObjEvent("AutoIt.Error","_WinHTTP_Error")

_SendGet3(2, '0'); whatever you need to actually put in here...



Func _SendGet3($ID, $PHPSESSID)
    With $oHTTP
        .Open('GET', 'http://www.duckload.com/design/Captcha2.php?key='&$ID, False)
        .SetRequestHeader('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.0.11) Gecko/2009060215 Firefox/3.0.11 (.NET CLR 3.5.30729)')
        .SetRequestHeader('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
        .SetRequestHeader('Accept-Language', 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3')
        .SetRequestHeader('Accept-Encoding', 'gzip,deflate')
        .SetRequestHeader('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.7')
        .SetRequestHeader('Cookie', 'PHPSESSID='&$PHPSESSID&';')
        .Send()
    EndWith

    ;use the ADO-DB Object to write the data of the HTTP Response
    With $oDATA
        .Type=1
        .Open
        .Write($oHTTP.ResponseBody)
        .SaveToFile(@ScriptDir&'\captcha.png', 2)
        .Close
    EndWith
EndFunc


Func _WinHTTP_Error()
    Local $errtxt="!> We intercepted a COM Error !"      & @CRLF  & @CRLF & _
         "!>    err.description is: "    & @TAB & $oERRO.description    & @CRLF & _
         "!>    err.windescription:"     & @TAB & $oERRO.windescription & @CRLF & _
         "!>    err.number is: "         & @TAB & hex($oERRO.number,8)  & @CRLF & _
         "!>    err.lastdllerror is: "   & @TAB & $oERRO.lastdllerror   & @CRLF & _
         "!>    err.scriptline is: "     & @TAB & $oERRO.scriptline     & @CRLF & _
         "!>    err.source is: "         & @TAB & $oERRO.source         & @CRLF & _
         "!>    err.helpfile is: "       & @TAB & $oERRO.helpfile       & @CRLF & _
         "!>    err.helpcontext is: "    & @TAB & $oERRO.helpcontext  & @CRLF
    ConsoleWrite($errtxt)
    MsgBox(0,'COM Error!',$errtxt)
    Exit
EndFunc

Note: there are also a few other ways to accomplish this :D

Edited by crashdemons

My Projects - WindowDarken (Darken except the active window) Yahsmosis Chat Client (Discontinued) StarShooter Game (Red alert! All hands to battlestations!) YMSG Protocol Support (Discontinued) Circular Keyboard and OSK example. (aka Iris KB) Target Screensaver Drive Toolbar Thingy Rollup Pro (Minimize-to-Titlebar & More!) 2D Launcher physics example Ascii Screenshot AutoIt3 Quine Example ("Is a Quine" is a Quine.) USB Lock (Another system keydrive - with a toast.)

Share this post


Link to post
Share on other sites
bLaZiN

i can't imagine how to get the first solution run.

well i tried already a little with the second, this might work, thank you guys i'll think i'll figure it out soon

Share this post


Link to post
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
Sign in to follow this  

×