Sep 9, 2015 09:16 PM
(This post was last modified: Sep 9, 2015 10:02 PM by Judicious⭐️.)
I'm working on a special project and I thought I would share some code with you guys. As far as I know this is not available anywhere else in visual basic. This is just the core login and I do not intend to go into the icloud protocol or basic encryption at this time.
I'm sure some of you might find this useful so I figured I would share it with the community for a limited time. I say a "limited time" because I do NOT intend on having this posted for too long for obvious reasons. I'm not a self entitled "master coder" by any means so criticizing my code with over analysis is going to get you no where. Please refrain from posting pointless replies fueled by your internet ego and filled with flames. If you wish to simplify the code a bit let me know via pm and I will add your suggestions with credit.
Note: This is the login method used to retrieve backups from the cloud. A great deal of documentation regarding icloud helped me in making this code. That specific documentation can be found here: https://code.google.com/p/iphone-dataprotection/
I will be building on this source as time passes.
Now let's get into it. If you just want to view the full code you can do so right here: Full Code (Click to View)
'Imports:
'We set these variables for usage:
'Setting the Client Info string:
'Encoding the login creds to bytes:
'Setting the auth string and converting the bytes to an encrytped Base64 string:
'This is the request URL:
'This is the method for the request:
'We're gonna set the postdata to nothing and then convert it to bytes. I did this to build on it in the future if needed. Rather than just setting the byte to nothing.
'Setting the Header for authorization:
'DataStreams and writing our post data:
'Using Try so we can catch errors and display feedback for invalid logins, etc.
'Getting our response and saving it as a string:
'Looking for a specific key for a successful login.
'Parsing response to get the dsPrsID:
'Parsing response to get mmeAuthToken
'Displaying a message box on successful login and ending the IF
'Close Connections/Streams
'This is where we handle the errors.
I'm sure some of you might find this useful so I figured I would share it with the community for a limited time. I say a "limited time" because I do NOT intend on having this posted for too long for obvious reasons. I'm not a self entitled "master coder" by any means so criticizing my code with over analysis is going to get you no where. Please refrain from posting pointless replies fueled by your internet ego and filled with flames. If you wish to simplify the code a bit let me know via pm and I will add your suggestions with credit.
Note: This is the login method used to retrieve backups from the cloud. A great deal of documentation regarding icloud helped me in making this code. That specific documentation can be found here: https://code.google.com/p/iphone-dataprotection/
I will be building on this source as time passes.
Now let's get into it. If you just want to view the full code you can do so right here: Full Code (Click to View)
'Imports:
Code
Imports System.IO
Imports System.Net
Imports System.Text'We set these variables for usage:
Code
Dim auth As String, dsPrsID As String, mmeAuthToken As String, postData As String, CLIENT_INFO As String
Dim ibyt As Byte(), byteArray As Byte()
Dim webClient As New System.Net.WebClient, request As WebRequest, dataStream As Stream, Response As WebResponse, responseFromServer As String'Setting the Client Info string:
Code
CLIENT_INFO = "<iPhone2,1> <iPhone OS;5.1.1;9B206> <com.apple.AppleAccount/1.0 ((null)/(null))>"'Encoding the login creds to bytes:
Code
ibyt = System.Text.Encoding.UTF8.GetBytes("[email protected]" & ":" & "Password")'Setting the auth string and converting the bytes to an encrytped Base64 string:
Code
auth = "Basic " & System.Convert.ToBase64String(ibyt)'This is the request URL:
Code
request = WebRequest.Create("https://setup.icloud.com/setup/authenticate/$APPLE_ID$")'This is the method for the request:
Code
request.Method = "POST"'We're gonna set the postdata to nothing and then convert it to bytes. I did this to build on it in the future if needed. Rather than just setting the byte to nothing.
Code
postData = ""
byteArray = Encoding.UTF8.GetBytes(postData)'Setting the Header for authorization:
Code
request.Headers.Set("Authorization", auth)'DataStreams and writing our post data:
Code
dataStream = request.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()'Using Try so we can catch errors and display feedback for invalid logins, etc.
Code
Try'Getting our response and saving it as a string:
Code
Response = request.GetResponse()
dataStream = Response.GetResponseStream()
Dim reader As New StreamReader(dataStream)
responseFromServer = reader.ReadToEnd()'Looking for a specific key for a successful login.
Code
If InStr(responseFromServer, "AccountInfo") Then'Parsing response to get the dsPrsID:
Code
dsPrsID = Split(responseFromServer, "dsPrsID</key>")(1)
dsPrsID = Split(dsPrsID, "<integer>")(1)
dsPrsID = Split(dsPrsID, "</integer>")(0)'Parsing response to get mmeAuthToken
Code
mmeAuthToken = Split(responseFromServer, "mmeAuthToken</key>")(1)
mmeAuthToken = Split(mmeAuthToken, "<string>")(1)
mmeAuthToken = Split(mmeAuthToken, "</string>")(0)'Displaying a message box on successful login and ending the IF
Code
MsgBox("Login Successful." & vbCrLf & vbCrLf & "dsPrsID = " & dsPrsID & vbCrLf & "mmeAuthToken = " & mmeAuthToken)
End If'Close Connections/Streams
Code
reader.Close() : dataStream.Close() : Response.Close()'This is where we handle the errors.
Code
Catch ex As Exception
If InStr(ex.Message, "Unauthorized") Then MsgBox("Login Failed. Invalid AppleID or Password.") : Exit Sub
MsgBox("Error: " & ex.Message)
End Try

![[Image: lYc5kac.png]](https://i.imgur.com/lYc5kac.png)