Hello There, Guest!
View New Posts  |  View Today's Posts
[VB.Net] MyBB Login Utility - Login to most MyBB websites

  • 1 Vote(s) - 5 Average


03-01-2013, 11:22 PM #1
KoBE
¯\_(ツ)_/¯
******
Global Moderators
Posts: 4,862 Threads:494 Joined: Jun 2011 Reputation: 67

MyBB Login Utility - Login to most MyBB websites
In response to another users request... I've put together an example in VB.Net to log into MyBB Websites. I ported this from something else I was building in C#, so if anyone's interested, I can whip together a C# example really quick.

It's real basic and only shows a status when it logs in, but it's up to you what you do with it from here. I just hope that my code doesn't make it's way into anything malicious.

This also has captcha support. When a captcha is needed, it will show a form and have you type it in.

Example use:
Code:
Util.ForumURL = txtURL.Text
Util.Username = txtUsername.Text
Util.Password = txtPassword.Text

Util.Login()


Screenshot:


Download:

.zip   vbMyBBlogin.zip (Size: 19.77 KB / Downloads: 323)

Utility.vb Source Code (still need frmCaptcha from the download):
Code:
Imports System.IO
Imports System.Net
Imports System.Threading

Public Class Utility

    ' private data member for ForumURL property
    Private _ForumURL As String

    ' auto properties for Username/Password
    Public Property Username As String
    Public Property Password As String
    Public Property ForumURL As String
        Get
            Return _ForumURL
        End Get
        Set(value As String)
            ' force http:// at the beginning
            If Not value.ToLower().StartsWith("http://") Then
                value = "http://" & value
            End If

            ' force a / at the end
            If Not value.EndsWith("/") Then
                _ForumURL = value & "/"
            Else
                _ForumURL = value
            End If
        End Set
    End Property

    ' auto property for UserAgent
    Public Property UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0"

    ' event to fire the status
    Public Event StatusReceived(ByVal status As String)

    ' cookie container to hold cookies for multiple requests
    Private cookieMoster As New CookieContainer()

    ''' <summary>
    ''' Log into the forum
    ''' </summary>
    ''' <remarks>Uses an HttpWebRequest to login to the forum</remarks>
    Public Sub Login()
        Dim t As New Thread(AddressOf _Login)
        t.IsBackground = True
        t.Start()

    End Sub
    Private Sub _Login()
        ' create the post data
        Dim sPost As String = "username=" & Username & "&password=" & Password & "&remember=yes&submit=Login&action=do_login&url=" & _ForumURL & "member.php?action=login"

        ' tell the user what we're doing
        RaiseEvent StatusReceived("Attempting to log in")

        ' make a request and grab the source
        Dim sSource As String = MakePostRequest(_ForumURL & "member.php", sPost)

        ' if we need to handle the captcha
        If sSource.Contains("captcha_img") Then

            ' tell the user what we're doing
            RaiseEvent StatusReceived("Captcha needed")

            ' get the imagehash needed to fetch the captcha
            Dim sFind As String = "?imagehash="
            Dim sImageHash As String = sSource.Substring(sSource.IndexOf(sFind) + sFind.Length, 32)

            ' tell the user what we're doing
            RaiseEvent StatusReceived("Fetching Captcha..")

            ' create an instance of frmCaptcha
            Dim fCaptcha As New frmCaptcha

            ' fetch the captcha and set the image in frmCaptcha
            fCaptcha.CaptchaImage = FetchCaptch(sImageHash)

            ' show dialoge prevents them from using frmMain
            fCaptcha.ShowDialog()

            ' grab what they typed in then dispose the form
            Dim sImgageString As String = fCaptcha.Answer
            fCaptcha.Dispose()

            ' create new post data
            sPost = "username=" & Username & "&password=" & Password + "&remember=yes" + "&imagestring=" + sImgageString + "&imagehash=" + sImageHash + "&submit=Login&action=do_login&url=" & _ForumURL & "member.php?action=login"

            ' tell the user what we're doing
            RaiseEvent StatusReceived("Sending Captcha")

            ' send the captcha and grab a new source
            sSource = MakePostRequest(_ForumURL & "member.php", sPost)

        End If

        ' if the source has the logout link then we're in
        If sSource.Contains("member.php?action=logout") Then
            RaiseEvent StatusReceived("Login Successful")
        Else
            RaiseEvent StatusReceived("Login Failed")
        End If
    End Sub
    Private Function MakePostRequest(ByVal url As String, ByVal postData As String) As String

        Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
        request.CookieContainer = cookieMoster
        request.UserAgent = UserAgent
        request.KeepAlive = True
        request.Method = "POST"
        request.ContentType = "application/x-www-form-urlencoded"

        Dim postBytes As Byte() = System.Text.Encoding.ASCII.GetBytes(postData)
        request.ContentLength = postBytes.Length

        Dim requestStream As Stream = request.GetRequestStream()
        requestStream.Write(postBytes, 0, postBytes.Length)
        requestStream.Close()

        Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
        Dim sr As New StreamReader(response.GetResponseStream())
        Dim sReturn As String = sr.ReadToEnd()

        sr.Dispose()

        Return sReturn
    End Function
    Private Function FetchCaptch(ByVal imgHash As String) As Image
        Try
            Dim request As HttpWebRequest = DirectCast(WebRequest.Create(_ForumURL & "captcha.php?imagehash=" & imgHash), HttpWebRequest)

            request.Method = "GET"
            request.CookieContainer = cookieMoster

            Dim img As Image = Image.FromStream(request.GetResponse().GetResponseStream())

            request = Nothing

            Return img
        Catch ex As Exception
            RaiseEvent StatusReceived("Error fetching captcha...")
        End Try
        Return Nothing
    End Function
End Class
This post was last modified: 03-01-2013, 11:24 PM by KoBE.

03-01-2013, 11:25 PM #2
AceInfinity
Developer
*******
Administrators
Posts: 9,733 Threads:1,026 Joined: Jun 2011 Reputation: 76

RE: MyBB Login Utility - Login to most MyBB websites
Good job KoBE

cheers


Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲
 ▲ ▲

03-03-2013, 07:36 AM #3
BreShiE
Knowledge Is Power
*****
Posts: 1,130 Threads:158 Joined: Sep 2011 Reputation: 12

RE: MyBB Login Utility - Login to most MyBB websites
This is a really nice example of logging into MyBB Forums. Well done KoBE! Yet again, showing us your sexy coding skills. ;)


"It was pretty impressive to watch a missile fly down an air vent, but couldn't we feasibly use that same technology to shoot food at hungry people?"

03-03-2013, 11:39 AM #4
KoBE
¯\_(ツ)_/¯
******
Global Moderators
Posts: 4,862 Threads:494 Joined: Jun 2011 Reputation: 67

RE: MyBB Login Utility - Login to most MyBB websites
I try to make everything I do look sexy.. :P

Thanks for the feedback. I tried to comment it well enough that someone else could build on it if they needed. I didn't comment the MakePostRequest() or FetchCaptcha(), since they are simple WebRequests.

Like always, if you have any questions. Feel free to ask.

03-09-2013, 12:57 AM #5
JennVBnet
swag += 1
**
Posts: 7 Threads:0 Joined: Mar 2013 Reputation: 0

RE: MyBB Login Utility - Login to most MyBB websites
Awesome I was wondering exactly how I was ever going to deal with those image tests during login. So many websites I use every day ask you type one of those every time you login. Like logging into a gmail you haven't used in awhile etc. That example will be extremely helpful. Another "real-world" well documented code example you would never find on the other programming forums.
 [Image: jennvb.png]
nerd + aspiring coder
interests
egyptology/computers/a.i.

03-09-2013, 02:02 AM #6
KoBE
¯\_(ツ)_/¯
******
Global Moderators
Posts: 4,862 Threads:494 Joined: Jun 2011 Reputation: 67

RE: MyBB Login Utility - Login to most MyBB websites
Yep, there are a few ways.. pulling up a form and sending it is the way I prefer. Thanks for the kind words.

04-09-2013, 08:21 PM #7
d³Σx²
Junior Member
**
Posts: 1 Threads:0 Joined: Apr 2013 Reputation: 0

RE: MyBB Login Utility - Login to most MyBB websites
Is there a way to navigate a forum with this? I need it to visit myBB's User CP to get some information.

Thanks in advance.
This post was last modified: 04-09-2013, 08:21 PM by d³Σx².

04-09-2013, 09:33 PM #8
KoBE
¯\_(ツ)_/¯
******
Global Moderators
Posts: 4,862 Threads:494 Joined: Jun 2011 Reputation: 67

RE: MyBB Login Utility - Login to most MyBB websites
Yeah, instead of a MakePostRequest() function, you could do something similiar and make a MakeGetRequest() function, and have it return the source that way.

04-10-2013, 12:48 AM #9
AceInfinity
Developer
*******
Administrators
Posts: 9,733 Threads:1,026 Joined: Jun 2011 Reputation: 76

RE: MyBB Login Utility - Login to most MyBB websites
(04-09-2013, 09:33 PM)KoBE Wrote:  Yeah, instead of a MakePostRequest() function, you could do something similiar and make a MakeGetRequest() function, and have it return the source that way.

From the UserCP I don't think so though lol, You'd still need to make a POST request to login.


Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲
 ▲ ▲

04-10-2013, 01:57 AM #10
KoBE
¯\_(ツ)_/¯
******
Global Moderators
Posts: 4,862 Threads:494 Joined: Jun 2011 Reputation: 67

RE: MyBB Login Utility - Login to most MyBB websites
Once you're logged in though, you would use a get request to http://tech.reboot.pro/usercp.php

The class already does the login, all one would need to do is modify it and tell it to navigate to whatever link they wanted. So long as it used the same cookie container.




Forum Jump:


Possibly Related Threads...
Thread Author Replies Views Last Post
   Screenshot Utility with Auto Upload KoBE 12 8,929 08-28-2014, 08:12 PM
Last Post: KoBE
   [Source] Twitter Login/Post Tweet Example (Without API, SDK or Webbrowser) idb.exe 10 11,476 06-06-2014, 11:16 PM
Last Post: hoangseo
  login to the site with get method kenshiro 3 2,445 05-19-2014, 09:58 AM
Last Post: KoBE
   Login to a Website using WebRequests. KoBE 74 55,417 04-12-2014, 02:03 PM
Last Post: Mariusz12345
  How to make httpwebrequest login facebok account in listbox DilarangMasuk 3 3,368 10-05-2013, 10:51 AM
Last Post: AceInfinity


Users browsing this thread: 1 Guest(s)