Jump to content

is there an easy way to implement ssl/tls into autoit sockets?


Recommended Posts

i know nothing about ssl or tls, even on a basic level. im making a fully socket based http udf for autoit that handles cookies, does post and get etc. it works perfectly, im going to add in http proxy support which i know how to do, but ive no clue how to implement ssl/tls

#include <Array.au3>
TCPStartup()
Global $ret
Global $noret = 0
Dim $cookarr
DirCreate("c:autCookies")
Func URLEncode($urlText)
$url = ""
For $i = 1 To StringLen($urlText)
     $acode = Asc(StringMid($urlText, $i, 1))
     Select
         Case ($acode >= 48 And $acode <= 57) Or _
                 ($acode >= 65 And $acode <= 90) Or _
                 ($acode >= 97 And $acode <= 122)
             $url = $url & StringMid($urlText, $i, 1)
         Case $acode = 32
             $url = $url & "%20"
         Case Else
             $url = $url & "%" & Hex($acode, 2)
     EndSelect
Next
Return $url
EndFunc ;==>URLEncode
Func httpget($host,$page)
;$gcook = DELIVER_COOKIES()
;if $gcook <> 'none' Then
;MsgBox(0, "", $gcook)
;EndIf
$gcookie = _getcookie()
$gsock = TCPConnect(tcpnametoip($host),80)
If @error Then
TCPCloseSocket($gsock)
Else
Local $sCommand = "GET " & $page & " HTTP/1.1" & @CRLF

$sCommand &= "Host: " & $host & @CRLF
$sCommand &= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0" & @CRLF
$sCommand &= "Referer: " & $host & @CRLF
$sCommand &= "Connection: close" & @CRLF
If $gcookie <> -1 Then
$sCommand &= $gcookie & @CRLF & @CRLF
EndIf
TCPSend($gsock,$sCommand)
If @error Then
TCPCloseSocket($gsock)
Else
Return _httprecv($gsock)
EndIf
EndIf
EndFunc
Func httppost($host,$page,$pdata)
$gcookie = _getcookie()
     $psock = TCPConnect(tcpnametoip($host),80)
If @error Then
TCPCloseSocket($gsock)
Else
$command = "POST "&$page&" HTTP/1.1"&@CRLF
$command &= "Host: " &$host&@CRLF
$command &= "User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:16.0) Gecko/20100101 Firefox/16.0"&@CRLF
$command &= "Connection: close"&@CRLF
$command &= "Content-Type: application/x-www-form-urlencoded"&@CRLF
$command &= "Content-Length: "&StringLen($pdata)&@CRLF
If $gcookie <> -1 Then
$command &= $gcookie & @CRLF
EndIf
$command &= ""&@CRLF
$command &= $pdata&@CRLF
TCPSend($psock,$command)
If @error Then
TCPCloseSocket($psock)
Else
Return _httprecv($psock)
EndIf
EndIf
EndFunc
Func _httprecv($hsock)
while 1
$data = TCPRecv($hsock,2048)
If @error Then
TCPCloseSocket($hsock)
ExitLoop
Else
If $data <> "" Then
$ret &= $data
If StringInStr($data, "Set-Cookie:") Then
;MsgBox(0, "", $data)
_cookie($ret)
EndIf
EndIf
EndIf
WEnd
Return StringReplace($ret, ">", @crlf)
EndFunc
func _cookie($retcook)
$array = StringRegExp($retcook, 'Set-Cookie: (.+)rn', 3)
$cookies = ''
for $i = 0 to UBound($array) - 1
; Add all cookies to a single string, and then clean it up.
$cookies = $array[$i] & ';'

; Getting the name of the Current Cookie
$csplit = StringSplit($cookies, "=")
$cookname = $csplit[1]

; Removing parts we do not use..
$cookies = StringRegExpReplace($cookies, "( path| domain| expires)=[^;]+", "")
$cookies = StringRegExpReplace($cookies, " HttpOnly", "")
$cookies = StringRegExpReplace($cookies, "[;]{2,}", ";")

; Save the cookies to .txt files
; Delete the file if it already exists
FileDelete ("c:autCookies"&$cookname&".txt")
$file = FileOpen("c:autCookies" & $cookname & ".txt", 1)
FileWrite($file, $cookies)
FileClose($file)

Next
EndFunc
_getcookie()
Func _getcookie()
$cookrr = "Cookie:"
$search = FileFindFirstFile("c:autCookies*.txt")
If @error Then
Return -1
$noret = 1
EndIf
If $noret <> 1 Then
while 1
$file = FileFindNextFile($search)
If @error Then
Return $cookrr
ExitLoop
Else
$cookrr &= " " & FileRead("c:autCookies"&$file)
EndIf
WEnd
EndIf
$noret = 0
EndFunc

all of this works perfectly, once im completely finished im going to submit it as a udf. just need help with ssl support. also, some of the code such as urlencode and the cookie handler function i grabbed off other authors and modified. urlencode() converts stuff to %encoding for posts that require it. im forgetting who the author was, and the cookie handler is something i grabbed from a winhttp udf that obviously works fine for what im doing here as well

Edited by lionfaggot
Link to comment
Share on other sites

i want to know if there is a udf to make ssl easier to use with autoit sockets, this has everything to do with autoit. i dont know how to use ssl or tls at the socket level. i wish to learn how or perhaps there is a library that makes this easier. also your comment made very little sense. i dont even know where to start with https or tls, i dont know how they work on even a basic level. maybe someone can help me learn

Edited by lionfaggot
Link to comment
Share on other sites

i searched for ssl and it doesnt bring up what im looking for. meh forget it. there are no autoit socket examples for how to use ssl in autoit sockets that i have seen. i like how you cant even paste me a snippet, mainly because there are none. thanks for being useless

Edited by lionfaggot
Link to comment
Share on other sites

i searched for ssl and it doesnt bring up what im looking for. meh forget it. there are no autoit socket examples for how to use ssl in autoit sockets that i have seen. i like how you cant even paste me a snippet, mainly because there are none. thanks for being useless

I suppose this attitude would be expected from a name like "lionfaggot" :thumbsdown:

Everyone here is helping you on their own time, not yours. If you learned the headers you can learn ssl just like FireFox said.

Try looking in to some linux python scripts if you can, I'm sure there are lots of ssl tidbits in them. Ask on the ubuntu forums. That's where I would look.

Link to comment
Share on other sites

from what im reading it'd be too slow to incorporate ssl/tls into autoit sockets. which means its need help from some outside library. also as to my attitude. he told me to do a search on the forums for informations that isnt even there. which kind of makes him look like an idiot "oh a simple search will give you the answers" this forum is about as useful as a bag of hammers. i have yet to have any question ive ever asked answered here. i always end up figuring it out on my own. also, unlike the other http socket udf, mine actually works. it properly incorporates cookies and everything. but i definitely wont be sharing it on this forum. its superior in every way

Edited by lionfaggot
Link to comment
Share on other sites

  • Developers

but i definitely wont be sharing it on this forum. its superior in every way

Shit ...was really looking forward to your SSL socket implementation in AutoIt.

Oh well ... So be it.

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

@lionfaggot

You're welcome.

I know you made a search before posting, when I said "this will answer your question", I meant : there is nothing available on the forum, unless someone comes from nowhere, you won't have help.

Link to comment
Share on other sites

from what im reading it'd be too slow to incorporate ssl/tls into autoit sockets. which means its need help from some outside library. also as to my attitude. he told me to do a search on the forums for informations that isnt even there. which kind of makes him look like an idiot "oh a simple search will give you the answers" this forum is about as useful as a bag of hammers. i have yet to have any question ive ever asked answered here. i always end up figuring it out on my own. also, unlike the other http socket udf, mine actually works. it properly incorporates cookies and everything. but i definitely wont be sharing it on this forum. its superior in every way

This is the most helpful forum I have ever been on. If you have reason to believe otherwise, we all already know why. Good luck with your script.

Link to comment
Share on other sites

well just because im bored, ill post my http proxy implementations for this udf, here it is, i updated the cookie function to have cookie directories include the host name of the website, this way you can be logged into multiple sites at once while using it. it also supports http proxies for both post and get, im going to find some library that will help me implement ssl into this. if not i guess i can make a library in vb for it. heres the code:

#include <Array.au3>
TCPStartup()
Global $noret = 0
;postencode("post data")
; encodes a string in % encoding for use with post data that requires it
Func postencode($urlText)
    $url = ""
    For $i = 1 To StringLen($urlText)
        $acode = Asc(StringMid($urlText, $i, 1))
        Select
            Case ($acode >= 48 And $acode <= 57) Or _
                    ($acode >= 65 And $acode <= 90) Or _
                    ($acode >= 97 And $acode <= 122)
                $url = $url & StringMid($urlText, $i, 1)
            Case $acode = 32
                $url = $url & "%20"
            Case Else
                $url = $url & "%" & Hex($acode, 2)
        EndSelect
    Next
    Return $url
 EndFunc   ;==>URLEncode
 ;httpget("www.google.com", "/")
;retrieves data from a webpage
Func httpget($host,$page)
   $gcookie = _getcookie($host)
   $gsock = TCPConnect(tcpnametoip($host),80)
   If @error Then
      TCPCloseSocket($gsock)
   Else
 Local $sCommand = "GET " & $page & " HTTP/1.1" & @CRLF
   
    $sCommand &= "Host: " & $host & @CRLF
    $sCommand &= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0" & @CRLF
    $sCommand &= "Referer: " & $host & @CRLF
    $sCommand &= "Connection: close" & @CRLF
    If $gcookie <> -1 Then
       $sCommand &= $gcookie & @CRLF & @CRLF
    EndIf
    TCPSend($gsock,$sCommand)
    If @error Then
       TCPCloseSocket($gsock)
    Else
      Return _httprecv($host,$gsock)
    EndIf
   EndIf
EndFunc
;proxget("proxyip:port", "http://www.google.com/")
;retrieves data from a webpage using an http proxy
Func proxget($prox,$hostpage)
   $psplit = StringSplit($prox, ":")
    $hh = StringSplit($hostpage, "/")
    $host = $hh[3]     
   $gcookie = _getcookie($host)
      $psock = TCPConnect($psplit[1],$psplit[2])
   If @error Then
      TCPCloseSocket($psock)
   Else
 Local $sCommand = "GET " & $hostpage & " HTTP/1.1" & @CRLF
   
    $sCommand &= "Host: " & $host & @CRLF
    $sCommand &= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0" & @CRLF
    $sCommand &= "Referer: " & $host & @CRLF
    $sCommand &= "Proxy-Connection: close" & @CRLF
    If $gcookie <> -1 Then
       $sCommand &= $gcookie & @CRLF & @CRLF
    EndIf
    TCPSend($psock,$sCommand)
    If @error Then
       TCPCloseSocket($psock)
    Else
      Return _httprecv($host,$psock)
    EndIf
   EndIf
EndFunc
;httppost("somesite.com", "/login", "username=user&pass=pass")
;posts data to a webpage
Func httppost($host,$page,$pdata)
   $gcookie = _getcookie($host)
      $psock = TCPConnect(tcpnametoip($host),80)
   If @error Then
      TCPCloseSocket($gsock)
   Else
    $command = "POST "&$page&" HTTP/1.1"&@CRLF
    $command &= "Host: " &$host&@CRLF
    $command &= "User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:16.0) Gecko/20100101 Firefox/16.0"&@CRLF
    $command &= "Connection: close"&@CRLF
    $command &= "Content-Type: application/x-www-form-urlencoded"&@CRLF
    $command &= "Content-Length: "&StringLen($pdata)&@CRLF
    If $gcookie <> -1 Then
       $command &= $gcookie & @CRLF
    EndIf
    $command &= ""&@CRLF
    $command &= $pdata&@CRLF
    TCPSend($psock,$command)
    If @error Then
       TCPCloseSocket($psock)
    Else
      Return _httprecv($host,$psock)
   EndIf
   EndIf
EndFunc
;proxpost("proxyip:port", "http://www.somesite.com/login", "username=user&pass=pass")
;posts data to a webpage using an http proxy
Func proxpost($prox,$hostpage,$pdata)
   $psplit = StringSplit($prox, ":")
    $hh = StringSplit($hostpage, "/")
    $host = $hh[3]   
   $gcookie = _getcookie($host)
      $psock = TCPConnect($psplit[1],$psplit[2])
   If @error Then
      TCPCloseSocket($psock)
   Else
    $command = "POST "&$hostpage&" HTTP/1.1"&@CRLF
    $command &= "Host: " &$host&@CRLF
    $command &= "User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:16.0) Gecko/20100101 Firefox/16.0"&@CRLF
    $command &= "Proxy-Connection: close"&@CRLF
    $command &= "Content-Type: application/x-www-form-urlencoded"&@CRLF
    $command &= "Content-Length: "&StringLen($pdata)&@CRLF
    If $gcookie <> -1 Then
       $command &= $gcookie & @CRLF
    EndIf
    $command &= ""&@CRLF
    $command &= $pdata&@CRLF
    TCPSend($psock,$command)
    If @error Then
       TCPCloseSocket($psock)
    Else
      Return _httprecv($host,$psock)
   EndIf
   EndIf
EndFunc
Func _httprecv($host,$hsock)
   Local $ret
   while 1
   $data = TCPRecv($hsock,2048)
   If @error Then
      TCPCloseSocket($hsock)
      ExitLoop
   Else
      If $data <> "" Then
         $ret &= $data
         If StringInStr($data, "Set-Cookie:") Then
         ;MsgBox(0, "", $data)
         _cookie($host,$ret)
         EndIf
      EndIf
   EndIf
   WEnd
   Return StringReplace($ret, ">", @crlf)
EndFunc
func _cookie($host,$retcook)
DirCreate("Cookies" & $host & "")
$array = StringRegExp($retcook, 'Set-Cookie: (.+)rn', 3)
$cookies = ''
for $i = 0 to UBound($array) - 1
  ; Add all cookies to a single string, and then clean it up.
  $cookies = $array[$i] & ';'
   
  ; Getting the name of the Current Cookie
  $csplit = StringSplit($cookies, "=")
  $cookname = $csplit[1]

  ; Removing parts we do not use..
  $cookies = StringRegExpReplace($cookies, "( path| domain| expires)=[^;]+", "")
  $cookies = StringRegExpReplace($cookies, " HttpOnly", "")
  $cookies = StringRegExpReplace($cookies, "[;]{2,}", ";")

; Save the cookies to .txt files
 ; Delete the file if it already exists
 FileDelete ("Cookies" & $host & ""&$cookname&".txt")
 $file = FileOpen("Cookies" & $host & "" & $cookname & ".txt", 1)
 FileWrite($file, $cookies)
 FileClose($file)
   
Next
EndFunc
Func _getcookie($host)
   $cookrr = "Cookie:"
   $search = FileFindFirstFile("Cookies" & $host & "*.txt")
   If @error Then
      Return -1
      $noret = 1
   EndIf
   If $noret <> 1 Then
   while 1
   $file = FileFindNextFile($search)
   If @error Then
      Return $cookrr
      ExitLoop
   Else
   $cookrr &= " " & FileRead("Cookies" & $host & ""&$file)
   EndIf
   WEnd
EndIf
$noret = 0
EndFunc

also, i suppose a function to alter user agent as well as content type wouldnt hurt, this doesnt yet support multipart posts. additionally i should include an option for keep-alive requests where required

Edited by lionfaggot
Link to comment
Share on other sites

I normally just lurk for long periods of time, but I saw this topic, and decided to post here. I looked into what it would take to properly implement SSL/TLS in Autoit. You have working sockets that would do for the connecting, the hard part comes in with the encryption. SSL and TLS encrypt all communication, so you'll need to implement or use someone's encryption algorithms right before sending/after receiving anything over the sockets. Having translated some different encryption algorithms myself, I can tell you it's not that easy.

Another thing you'll have to fight with is compression, which is ZLib if I recall correctly, the deflate algorithm specifically. I keep trying to talk myself into converting it into pure AutoIt code, but I always decide I have better uses of my time.

Best solution you might find is DLL's with some of these needed functions already implemented in them, so you get their raw speed. You'll still have to handle managing keys, sessions, and all that, but it's the only solution currently available.

If you really want to try to tackle doing all this, here's the route I took to see what all it would take. SSL and TLS are usually used with the OpenSSL project, and is a very nice project to browse through the source of it. Of course, not everyone reads C code that well, and even less so translates it into a scripting language. So, the best thing to do is find another scripted solution, and see if that could be used. In my searches, the only one that didn't just call the OpenSSL installed on most systems were some Javascript implementations of SSL/TLS. Googling for "Javascript SSL" is the fastest way to find several of the attempts at doing this, and they give information on how they did it, as well as share more source to browse and build an AutoIt clone from as well. They do use a lot of libraries from others, some of which there are no AutoIt counterparts, which means you'd need to either build those libraries as well, or at least enough of them for your purposes.

As for the helpfulness of this forum, as I started off with, I lurk far more than I post, and search even more than that. I've been around for a number of years, and haven't found many situations where the code I'm writing is complex enough where no one has ever asked, even in part, about what I am attempting to do. I may have had to change my search terms a few times, and look for other things related to what I'm looking for, and I've also had to follow links to external places for other things as well. I will have to say that, if one searches hard enough, you'll likely find at least parts of what you need here on this forum, long before you ever have to ask a question. Very few other places can boast such a thing, so I would say this forum is extremely helpful, even before asking a question. Whether new people provide answers or not shouldn't matter, as the forum shouldn't be just the people who currently happen to be reading the forum at this time.

Hopefully this is useful to not only you, but any one else who tries to work out SSL/TLS within AutoIt.

Link to comment
Share on other sites

to do SSL this is what you first must do.

build a bignum library for handling integers. you will need it.

implement the RSA algorithm. this is a bitch.

write out the SSL algorithm. even more than a bitch.

for starters.

should take you a few months. good luck!

ongoing projects:-firestorm: Largescale P2P Social NetworkCompleted Autoit Programs/Scripts: Variable Pickler | Networked Streaming Audio (in pure autoIT) | firenet p2p web messenger | Proxy Checker | Dynamic Execute() Code Generator | P2P UDF | Graph Theory Proof of Concept - Breadth First search

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...