Sabtu, November 02, 2013

VB.NET : Tumblr.com login + captcha

Imports System.Net
Imports System.IO
Imports System.Text
Imports System.Web

Public Class frmMain
#Region "Structures"
    Private Structure Sessions
  Dim recaptcha_public_key As String
  Dim form_key As String
  Dim recaptcha_challenge_field As String

  Public Sub Reset()
    recaptcha_challenge_field = String.Empty
    form_key = String.Empty
    recaptcha_public_key = String.Empty
  End Sub
    End Structure
#End Region

    Public Enum Verb
  [GET] = 0
  POST = 1
    End Enum
    Private ReadOnly Verbs() As String = New String() {"GET", "POST"}

    Dim CookieJar As New CookieContainer
    Private Session As New Sessions
    Function GetResponse(ByVal Method As Verb, ByVal Uri As String, Optional ByVal PostData As String = "")

  Dim byteData As Byte() = Nothing
  If Not String.IsNullOrEmpty(PostData.Trim) Then byteData = UTF8Encoding.UTF8.GetBytes(PostData)

  Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create(Uri), HttpWebRequest)
  postReq.Method = Verbs(Method)
  postReq.KeepAlive = True
  postReq.CookieContainer = CookieJar
  postReq.ContentType = "application/x-www-form-urlencoded"
  postReq.Referer = "https://www.tumblr.com/login"
  postReq.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0"
  postReq.ContentLength = If(IsNothing(byteData), 0, byteData.Length)

  If (Method.Equals(Verb.POST)) Then
    If Not postReq.ContentLength.Equals(0) Then
    Dim dataStream As Stream = postReq.GetRequestStream()
    With dataStream
    .Write(byteData, 0, byteData.Length)
    .Close() : .Dispose()
    End With
    End If
  End If

  Dim postresponse As HttpWebResponse
  postresponse = DirectCast(postReq.GetResponse(), HttpWebResponse)
  CookieJar.Add(postresponse.Cookies)

  Dim postreqreader As New StreamReader(postresponse.GetResponseStream())
  Dim html As String = postreqreader.ReadToEnd
  Return html
    End Function

    Sub UpdateLog(ByVal text As String)
  EventLog.AppendText(String.Format("[{0}]: {1}", Date.Now.ToShortTimeString, text.Trim))
  EventLog.AppendText(Environment.NewLine)
    End Sub
    Public Shared Function ParseBetween(ByVal Html As String, ByVal Before As String, ByVal After As String, Optional Offset As Integer = 0) As String
  If Offset = 0 Then Offset = Before.Length
  If String.IsNullOrEmpty(Html) Then Return String.Empty
  If Html.Contains(Before) Then
    Dim Result As String = Html.Substring(Html.IndexOf(Before) + Offset)
    If Result.Contains(After) AndAlso Not String.IsNullOrEmpty(After) Then Result = Result.Substring(0, Result.IndexOf(After))
    Return Result
  Else
    Return String.Empty
  End If
    End Function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  UpdateLog("Logging in with: " & username_txt.Text)
  If Not captcha_txt.Text = String.Empty Or username_txt.Text = String.Empty Or password_txt.Text = String.Empty Then
    Dim PostData As New StringBuilder
    PostData.Append(HttpUtility.UrlEncode("user[email]") & "=" & HttpUtility.UrlEncode(username_txt.Text))
    PostData.Append("&" & HttpUtility.UrlEncode("user[password]") & "=" & HttpUtility.UrlEncode(password_txt.Text))
    PostData.Append("&" & HttpUtility.UrlEncode("tumblelog[name]") & "=")
    PostData.Append("&recaptcha_public_key=" & Session.recaptcha_public_key)
    PostData.Append("&recaptcha_challenge_field=" & Session.recaptcha_challenge_field)
    PostData.Append("&recaptcha_response_field=" & captcha_txt.Text)
    PostData.Append("&" & HttpUtility.UrlEncode("user[age]") & "=")
    PostData.Append("&context=login")
    PostData.Append("&version=STANDARD")
    PostData.Append("&follow=")
    PostData.Append("&http_referer=" & HttpUtility.UrlEncode("https://www.tumblr.com/login"))
    PostData.Append("&form_key=" & HttpUtility.UrlEncode(Session.form_key))
    PostData.Append("&seen_suggestion=0")
    PostData.Append("&used_suggestion=0")

    Dim html As String = GetResponse(Verb.POST, "https://www.tumblr.com/login", PostData.ToString)

    html = GetResponse(Verb.GET, "http://www.tumblr.com/dashboard")

    If html.Contains(">Log out</a>") Then
    UpdateLog("Successfully Logged in !")
    UpdateLog("Your blog is: " & ParseBetween(html, "class=""open_blog_link"" href=""", """"))
    Else
    UpdateLog("Couldn't Login. Check username/password.")

    End If

  Else
    MsgBox("Make sure you've entered all the inputs. Including the captcha")
  End If

    End Sub

    Sub GetCaptcha()
  UpdateLog("Fetching Captcha ...")
  Dim Html As String = GetResponse(Verb.GET, "https://www.tumblr.com/login")
  Session.recaptcha_public_key = ParseBetween(Html, "name=""recaptcha_public_key"" value=""", """")
  Session.form_key = ParseBetween(Html, "name=""form_key"" value=""", """")
  Dim Challenge As String = GetResponse(Verb.GET, String.Format("https://www.google.com/recaptcha/api/challenge?k={0}&ajax=1&cachestop=0.7610605780430966", Session.recaptcha_public_key))
  Challenge = ParseBetween(Challenge, "challenge : '", "',")
  Session.recaptcha_challenge_field = Challenge
  PictureBox1.ImageLocation = "https://www.google.com/recaptcha/api/image?c=" & Session.recaptcha_challenge_field
  UpdateLog("Successfully fetched Captcha !")
    End Sub

    Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  GetCaptcha()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  GetCaptcha()
    End Sub
End Class 

Download : Tumblr login (mediafire)

0 ulasan:

Catat Ulasan

Pesanan daripada penulis :
Selamat datang ke 0x2013LΣΣT. Sekiranya anda mempunyai persoalan, pandangan, permintaan, bantuan, cadangan dan sebagainya. Tinggalkan pesanan anda ke dalam kotak komen. Terima kasih !
- http://0x2013.blogspot.com -