Re: Changement de serveur, connexion en MITM
Oui, désolé, je pensais que ça pouvais être un truc énorme qui ne vient même pas des codes :).
Voici mes codes (je les mets tous, ou presque).
Code du serveur (auquel se connecte le client D..)
Cliquez pour révéler
Cliquez pour masquer
Démarrage du serveur :
Imports System.Net.Sockets
Imports System.Net
Imports System.Threading
Namespace serveur
Public Class serveur
Public Shared _port As String = "443"
Public Shared _clients_list As List(Of client) 'Liste destinée à contenir les clients connectés
Public Shared Sock As Socket
Public Shared Function _initialize_serveur(ByVal port As String)
_port = port
Return True
End Function
Sub _initialize_socket()
Console.WriteLine("[SERVEUR] initialisation")
'Initialisation
'Crée le socket et l'IP EP
Sock = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Dim MonEP As IPEndPoint = New IPEndPoint(IPAddress.Any, _port)
_clients_list = New List(Of client) 'Initialise la liste
'On lance le serveur
Sock.Bind(MonEP) 'Lie le socket à cette IP
Sock.Listen(1) 'Se met en écoute
Console.WriteLine("[SERVEUR] initialisation terminée " & _port)
While True 'Boucle à l'infini
Console.WriteLine("[SERVEUR] en attente d'un client Dofus")
'Se met en attente de connexion et appelle TraitementConnexion() lors d'une connexion.
Dim sock_to_send As Socket = Sock.Accept() 'Bloquant tant que pas de connexion
_traitement(sock_to_send) 'Traite la connexion du client
End While
End Sub
Sub _traitement(ByVal sock_to_send As Socket)
Console.WriteLine("[SERVEUR] nouveau client Dofus")
Dim _client As New client 'Crée une instance de « client »
_client._initialize(sock_to_send, _clients_list)
_clients_list.Add(_client) 'Ajoute le client à la liste
'Crée un thread pour traiter ce client et le démarre
Dim ThreadClient As New Thread(AddressOf _client._traitement)
ThreadClient.Start()
End Sub
Public Shared Function _getSock() As Socket
Return Sock
End Function
Public Shared Function _close()
Sock.Close()
Return True
End Function
End Class
End Namespace
Listener du client D.. :
Imports System.Net.Sockets
Imports System.Net
Imports System.Threading
Imports System.Text
Namespace serveur
Public Class client
Public Shared Sock As Socket 'Le socket du client
Dim _clients_list As List(Of client)
Dim serveur As New serveur
Dim login As New mitm.client.login
Dim buffer(8191) As Byte
'Constructeur
Sub _initialize(ByVal sock_ As Socket, ByVal ClientList As List(Of client))
Sock = sock_
_clients_list = ClientList
End Sub
Public Shared Function _getSock() As Socket
Return Sock
End Function
Sub _traitement()
Console.WriteLine("[SERVEUR] écoute du client Dofus commencée")
Console.WriteLine("[SYSTEM] Démarrage du client login MITM demandé")
login._initialize_socket()
'Connecté au serveur de log
While (Sock.Connected)
Dim i = Sock.Receive(buffer) ' on met dans i le nombre d'octets recu
Dim data(i - 1) As Byte ' on créé un tableau de bytes du nombre de bytes recus
Array.Copy(buffer, data, i) ' on copie buffer dans data
Reader.parser(data)
End While
End Sub
Public Shared Sub _send_to_client(ByVal data() As Byte)
Dim Envoi As Integer = Sock.Send(data)
Dim id As String = Reader._getId(data)
Console.WriteLine("[MITM] id : " & id & " bytes =" & Envoi & " envoyés au client dofus")
End Sub
End Class
End Namespace
Reader / parser :
Imports System.Net.Sockets
Imports mitm.Utils
Namespace serveur
Public Class Reader 'permet de lire les paquets
Public Data_Out(0) As Byte
Public Waiting As Integer
Public Shared Sub parser(ByVal data() As Byte)
Dim index As Integer ' pour savoir ou on est
Dim id_and_length As UShort ' les 2 premiers octets (16 bits)
Dim packet_id As UShort ' les 14 premiers bits des 16
Dim packet_length_of As Byte ' les 2 derniers bits des 16
Dim packet_length As Integer ' la longueur du packet
Dim Packet_Start As Integer
Do Until index = data.Length
Packet_Start = index
id_and_length = data(index) * 256 + data(index + 1)
packet_length_of = id_and_length And 3 ' on veut les 2 derniers bits donc on masque (and) avec 11 en binaire (3 en decimal)
packet_id = id_and_length >> 2 ' on veut les 14 premiers bits donc on decale les 16 bits de 2 bits vers la droite
index += 2 + packet_length_of ' on avance des 2 octets de id_and_length + du nombre d'octets de la taille de taille
Select Case packet_length_of
Case 0
packet_length = 0
Case 1
packet_length = data(index - 1)
Case 2
packet_length = 256 * data(index - 2) + data(index - 1)
Case 3
packet_length = 65536 * data(index - 3) + 256 * data(index - 2) + data(index - 1)
End Select
Console.WriteLine("envoi : id -> " & packet_id & " len -> " & packet_length)
index += packet_length
Loop
If mitm.client.game.active Then
mitm.client.game._send_to_serveur(data)
Else
mitm.client.login._send_to_serveur(data)
End If
End Sub
Public Shared Function _getId(ByVal data() As Byte)
Dim index As Integer ' pour savoir ou on est
Dim id_and_length As UShort ' les 2 premiers octets (16 bits)
Dim packet_id As UShort ' les 14 premiers bits des 16
Dim packet_length_of As Byte ' les 2 derniers bits des 16
Dim packet_length As Integer ' la longueur du packet
Dim Packet_Start As Integer
Dim _String As String = ""
Do Until index = data.Length
Packet_Start = index
id_and_length = data(index) * 256 + data(index + 1)
packet_length_of = id_and_length And 3 ' on veut les 2 derniers bits donc on masque (and) avec 11 en binaire (3 en decimal)
packet_id = id_and_length >> 2 ' on veut les 14 premiers bits donc on decale les 16 bits de 2 bits vers la droite
index += 2 + packet_length_of ' on avance des 2 octets de id_and_length + du nombre d'octets de la taille de taille
Select Case packet_length_of
Case 0
packet_length = 0
Case 1
packet_length = data(index - 1)
Case 2
packet_length = 256 * data(index - 2) + data(index - 1)
Case 3
packet_length = 65536 * data(index - 3) + 256 * data(index - 2) + data(index - 1)
End Select
index += packet_length
_String = _String & "," & packet_id
Loop
Return _String
End Function
End Class
End Namespace
La fonction _send_to_server est dans les codes du client (qui se connecte au serveur D..)
Codes du client MITM, qui se connecte au serveur D..
Cliquez pour révéler
Cliquez pour masquer
Le reader / parser avec la fonction _send_to_client dans la classe serveur.client au dessus :
Imports System.Net.Sockets
Imports mitm.Utils
Namespace client
Public Class Reader 'permet de lire les paquets
Shared buffer(8191) As Byte
Shared Data_Out(0) As Byte
Shared Waiting As Integer
Shared Sub parser(ByVal data() As Byte)
Dim packetid As UShort
Try
Dim index As Integer ' pour savoir ou on est
Dim id_and_length As UShort ' les 2 premiers octets (16 bits)
Dim packet_id As UShort ' les 14 premiers bits des 16
Dim packet_length_of As Byte ' les 2 derniers bits des 16
Dim packet_length As Integer ' la longueur du packet
Dim Packet_Start As Integer
Dim Packet_End As Integer
If Waiting > 1 Then ' le buffer etait trop petit ?
Dim data_temps(data.Length + Data_Out.Length - 1) As Byte ' on créé un tableau de byte temporaire
Array.Copy(Data_Out, 0, data_temps, 0, Data_Out.Length) ' on met le debut du paquet trop long dans le tableau temporaire
Array.Copy(data, 0, data_temps, Data_Out.Length, data.Length) ' on met la reception a la suite
data = data_temps ' on met le tableau temporaire dans le tableau de travail
End If
Do Until index = data.Length ' on traite jusque la fin
Packet_Start = index
id_and_length = data(index) * 256 + data(index + 1) ' les 2 premiers octets
packet_length_of = id_and_length And 3 ' on veut les 2 derniers bits donc on masque (and) avec 11 en binaire (3 en decimal)
packet_id = id_and_length >> 2 ' on veut les 14 premiers bits donc on decale les 16 bits de 2 bits vers la droite
index += 2 + packet_length_of ' on avance des 2 octets de id_and_length + du nombre d'octets de la taille de taille
Select Case packet_length_of ' on lit le bon nombre d'octet pour connaitre la taille des données
Case 0
packet_length = 0
Case 1
packet_length = data(index - 1)
Case 2
packet_length = 256 * data(index - 2) + data(index - 1)
Case 3
packet_length = 65536 * data(index - 3) + 256 * data(index - 2) + data(index - 1)
End Select
If index + packet_length > data.Length Then ' buffer trop petit ?
Waiting = packet_length + index - Packet_Start ' alors on le signale
ReDim Data_Out(data.Length - Packet_Start - 1) ' on redimensionne le tableau de debut du paquet trop long
Array.Copy(data, Packet_Start, Data_Out, 0, data.Length - Packet_Start) ' on copie le debut du paquet trop long
Exit Sub ' on sort
End If
Dim debug As String = "recu : id = " & packet_id & ", taille = " & packet_length & vbCrLf ' on ecrit l'ID et la taille
packetid = packet_id
Dim packet(0) As Byte ' on prepare le paquet
If packet_length > 0 Then ' si sa taille est plus grande que 0 on redimensionne
ReDim packet(packet_length - 1)
Array.Copy(data, index, packet, 0, packet_length) ' et on copie les donnée
End If
Dim j As Integer = 1
For i = 0 To packet_length - 1 ' on fait defiler tout les octets recus
debug &= packet(i).ToString("X2") ' on ecrit l'octet proprement pour de bon ce coup ci
j += 1
If j = 16 Then
debug &= vbCrLf
j = 1
End If
Next ' au suivant
If packet_length >= 0 Then
Console.WriteLine("[C/G] reception id " & packet_id & ", Lenght -> " & packet_length)
End If
If packet_id = 42 Then
Dim _SSD As New Network.Connection.Server.Select.SelectedServerDataMessage
_SSD.deserialize(New DofusReader(New System.IO.MemoryStream(packet)))
'on lui balance un faux truc hé hé hé
Dim SSD As New Network.Connection.Server.Select.SelectedServerDataMessage
SSD.init(_SSD.serverId, "127.0.0.1", 443, _SSD.canCreateNewCharacter, _SSD.ticket) ' ces qui qui ecoute le porte 443
SSD.pack(mitm.serveur.client._getSock())
'On détruit l'objet du login
If login._close() Then
Console.WriteLine("[CLIENT] fermeture du serveur de login")
End If
Dim game As New game
game._initialize_client(_SSD.address, _SSD.port)
game._initialize_socket()
End If
index += packet_length ' on met l'index a jour
Packet_End = index
If Packet_End = data.Length Then ' si ca tombe pile poil alors le buffer etait assez grand
Waiting = 0 ' on reset
ReDim Data_Out(0) ' on reset
End If
Loop
If Not packet_id = 42 Then
serveur.client._send_to_client(data)
End If
Catch e As Exception
If Not e.Message = "Le thread a été abandonné." Then
Console.WriteLine("parsing()/c " & e.TargetSite.Name & " -> " & e.Message)
End If
End Try
End Sub
End Class
End Namespace
Connexion au serveur de login :
Imports System.Net.Sockets
Imports System.Net
Imports System.Threading
Namespace client
Public Class login
'connexion
Public _ip As String = "213.248.126.39"
Public _port As String = "5555"
'socket
Public Shared Sock As Socket
Public Thread As Thread
Private buffer(8191) As Byte
Public Sub _initialize_socket() 'initialisation du socket CLIENT
Console.WriteLine("[SYSTEM] initialisation client")
Sock = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Dim MonEP As IPEndPoint = New IPEndPoint(IPAddress.Parse(_ip), _port)
Sock.Connect(MonEP)
Console.WriteLine("[SYSTEM] initialisation terminée " & _port)
Console.WriteLine("[CLIENT] connecté au serveur Dofus. En attente de reception")
_traitement(Sock)
End Sub
Private Sub _traitement(ByVal Sock As Socket)
Console.Write("[CLIENT] client MITM connecté")
Try
Console.WriteLine("[CLIENT] démarrage du thread") 'Affichage
Thread = New Thread(AddressOf _reception)
Thread.Start()
Catch ex As Exception
Console.WriteLine("Erreur lors de la réception des données : " & ex.ToString)
End Try
End Sub
Private Sub _reception()
While (Sock.Connected) 'Tant qu'on est connecté au serveur
Console.WriteLine("[CLIENT] reception d'un paquet")
Dim i = Sock.Receive(buffer) ' on met dans i le nombre d'octets recu
Dim data(i - 1) As Byte ' on créé un tableau de bytes du nombre de bytes recus
Array.Copy(buffer, data, i) ' on copie buffer dans data
Reader.parser(data)
End While
End Sub
Public Shared Function _send_to_serveur(ByVal data() As Byte)
Dim Envoi As Integer = Sock.Send(data)
Console.WriteLine("[CLIENT] " & Envoi & " bytes envoyés au serveur dofus2")
Return True
End Function
Public Shared Function _getSock() As Socket
Return Sock
End Function
Public Shared Function _close()
Sock.Close()
Return True
End Function
End Class
End Namespace
Connexion au serveur de jeu :
Imports System.Net.Sockets
Imports System.Net
Imports System.Threading
Namespace client
Public Class game
'connexion
Public _ip As String
Public _port As String
Public Shared active As Boolean = False
'socket
Public Shared Sock As Socket
Public Thread As Thread
Private buffer(8191) As Byte
Public Function _initialize_client(ByVal ip As String, ByVal port As String)
Me._ip = ip
Me._port = port
Return Me
End Function
Public Sub _initialize_socket() 'initialisation du socket CLIENT
Console.WriteLine("[SYSTEM] initialisation socket GAME")
Sock = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Dim MonEP As IPEndPoint = New IPEndPoint(IPAddress.Parse(_ip), _port)
Sock.Connect(MonEP)
active = True
Console.WriteLine("[SYSTEM] initialisation terminée " & _port)
Console.WriteLine("[GAME] connecté au serveur de JEU Dofus")
_traitement(Sock)
End Sub
Private Sub _traitement(ByVal Sock As Socket)
Console.Write("[GAME] client MITM connecté")
Try
Console.WriteLine("[GAME] démarrage du thread") 'Affichage
Thread = New Thread(AddressOf _reception)
Thread.Start()
Catch ex As Exception
Console.WriteLine("Erreur lors de la réception des données : " & ex.ToString)
End Try
End Sub
Private Sub _reception()
While (Sock.Connected) 'Tant qu'on est connecté au serveur
Dim i = Sock.Receive(buffer) ' on met dans i le nombre d'octets recu
Dim data(i - 1) As Byte ' on créé un tableau de bytes du nombre de bytes recus
Array.Copy(buffer, data, i) ' on copie buffer dans data
Console.WriteLine("[GAME] reception d'un paquet")
Reader.parser(data)
End While
End Sub
Public Shared Function _send_to_serveur(ByVal data() As Byte)
Dim Envoi As Integer = Sock.Send(data)
Console.WriteLine("[GAME] " & Envoi & " bytes envoyés au serveur dofus2")
Return True
End Function
Public Shared Function _getSock() As Socket
Return Sock
End Function
Public Shared Function _close()
Sock.Close()
Return True
End Function
End Class
End Namespace
Enfin, j'ai quelques classes copiées de celles de D.. (SelectedServerDataMessage, BooleanByteWrapper, ...)
Et le dofusStream de LeaftBot pour envoyer le packet 42 au client D.. :
Cliquez pour révéler
Cliquez pour masquer
Imports System.Net.Sockets
Namespace Utils
Public Class DofusReader
Inherits IO.BinaryReader
Sub New(ByVal input As IO.Stream)
MyBase.New(input)
End Sub
Sub New(ByVal input As IO.Stream, ByVal encoding As System.Text.Encoding)
MyBase.New(input, encoding)
End Sub
Public Function readUnsignedByte()
Return ReadByte()
End Function
Public Overrides Function ReadByte() As Byte
Return MyBase.ReadByte()
End Function
Public Function ReadUnSignedshort()
Return ReadUInt16()
End Function
Public Overrides Function ReadUInt16() As UShort
Return (CUShort(ReadByte()) << 8) + ReadByte()
End Function
Public Overrides Function ReadBoolean() As Boolean
Return ReadByte() = 1
End Function
Public Function ReadShort()
Return ReadInt16()
End Function
Public Overrides Function ReadInt16() As Short
Dim Value As UShort = ReadUInt16()
If Value > Short.MaxValue Then
Dim Value2 As Short = -(UShort.MaxValue - Value) - 1
Return Value2
End If
Return Value
End Function
Public Function ReadInt()
Return ReadInt32()
End Function
Public Overrides Function ReadInt32() As Integer
Dim Value As UInteger = ReadUInt32()
If Value > Integer.MaxValue Then
Dim Value2 As Integer = -(UInteger.MaxValue - Value) - 1
Return Value2
End If
Return Value
End Function
Public Overrides Function ReadUInt32() As UInteger
Return (CUInt(ReadByte()) << 24) + (CUInt(ReadByte()) << 16) + (CUInt(ReadByte()) << 8) + ReadByte()
End Function
Public Overrides Function ReadDouble() As Double
Dim Bytes() As Byte = ReadBytes(8)
Array.Reverse(Bytes)
Return BitConverter.ToDouble(Bytes, 0)
End Function
Public Function ReadUTF()
Return ReadString()
End Function
Public Overrides Function ReadString() As String
Dim ByteArray() As Byte = MyBase.ReadBytes(ReadUInt16())
Return System.Text.Encoding.UTF8.GetString(ByteArray)
End Function
End Class
Public Class DofusWriter
Shared m_Bytes As New List(Of Byte)
Private Shared Function ComputeStaticHeader(ByVal PacketId As Integer, ByVal MessageLenghtType As Integer) As Short
Return (PacketId << 2) Or MessageLenghtType
End Function
Private Shared Function ComputeTypeLen(ByVal MessageLenght As Integer) As Short
Select Case MessageLenght
Case Is > UShort.MaxValue
Return 3
Case Is > Byte.MaxValue
Return 2
Case Is > 0
Return 1
Case Else
Return 0
End Select
End Function
Friend Shared Function Send(ByVal PacketID As Integer, ByVal sock As Object)
Dim MessageLenghtType As Integer = ComputeTypeLen(m_Bytes.Count)
Dim Header As Short = ComputeStaticHeader(PacketID, MessageLenghtType)
Dim BytesToWrite(2 + MessageLenghtType + m_Bytes.Count - 1) As Byte
BytesToWrite(0) = Header >> 8
BytesToWrite(1) = Header - 256 * BytesToWrite(0)
Dim index As Integer
Select Case MessageLenghtType
Case 1
BytesToWrite(2) = m_Bytes.Count
index = 3
Case 2
BytesToWrite(2) = m_Bytes.Count >> 8
BytesToWrite(3) = m_Bytes.Count - 256 * BytesToWrite(2)
index = 4
Case 3
BytesToWrite(2) = m_Bytes.Count >> 16
BytesToWrite(3) = m_Bytes.Count >> 8
BytesToWrite(4) = m_Bytes.Count - 256 * BytesToWrite(3) - 256 * 256 * BytesToWrite(2)
index = 5
End Select
Dim send_debug As String = Nothing
For i As Integer = index To m_Bytes.Count + index - 1
BytesToWrite(i) = m_Bytes(i - index)
Next
m_Bytes.Clear()
'sock.Send(BytesToWrite)
Console.WriteLine("[GAME] Transformé, envoie : id -> " & PacketID & " len -> " & BytesToWrite.Count - index)
mitm.serveur.client._send_to_client(BytesToWrite)
Return (BytesToWrite)
End Function
Public Sub WriteByte(ByVal Value As Byte)
m_Bytes.Add(Value)
End Sub
Public Sub WriteBoolean(ByVal Value As Boolean)
If Value Then
WriteByte(1)
Else
WriteByte(0) ' ton cryptpasse et pas bon c'est pas le bon projet que t'a lancé
End If
End Sub
Public Sub WriteShort(ByVal Target As Integer)
Dim arr As Byte()
arr = BitConverter.GetBytes(Target)
WriteByte(arr(1))
WriteByte(arr(0))
End Sub
Public Sub WriteUShort(ByVal Value As UShort)
WriteByte(CByte(Value >> 8))
WriteByte(CByte(Value And 255))
End Sub
Public Sub WriteUInt32(ByVal Value As UInteger)
WriteByte(CByte(Value >> 24))
Value -= (Value >> 24) << 24
WriteByte(CByte(Value >> 16))
Value -= (Value >> 16) << 16
WriteByte(CByte(Value >> 8))
Value -= (Value >> 8) << 8
WriteByte(CByte(Value))
End Sub
Public Sub WriteDouble(ByVal Value As UInteger)
WriteByte(CByte(Value >> 56))
Value -= (Value >> 56) << 56
WriteByte(CByte(Value >> 48))
Value -= (Value >> 48) << 48
WriteByte(CByte(Value >> 40))
Value -= (Value >> 40) << 40
WriteByte(CByte(Value >> 32))
Value -= (Value >> 32) << 32
WriteByte(CByte(Value >> 24))
Value -= (Value >> 24) << 24
WriteByte(CByte(Value >> 16))
Value -= (Value >> 16) << 16
WriteByte(CByte(Value >> 8))
Value -= (Value >> 8) << 8
WriteByte(CByte(Value))
End Sub
Public Sub WriteString(ByVal Value As String)
WriteUTF(Value)
End Sub
Public Sub WriteUTF(ByVal Value As String)
Dim BytesToWrite() As Byte = System.Text.Encoding.UTF8.GetBytes(Value)
WriteUShort(BytesToWrite.Length)
For Each Bit As Byte In BytesToWrite
WriteByte(Bit)
Next
End Sub
Sub WriteInt(ByVal Target As Integer)
Dim arr As Byte()
arr = BitConverter.GetBytes(Target)
WriteByte(arr(3))
WriteByte(arr(2))
WriteByte(arr(1))
WriteByte(arr(0))
End Sub
Sub WriteInt32(ByVal Target As Integer)
Dim arr As Byte()
arr = BitConverter.GetBytes(Target)
WriteByte(arr(3))
WriteByte(arr(2))
WriteByte(arr(1))
WriteByte(arr(0))
End Sub
Sub WriteInt16(ByVal Target As Integer)
Dim arr As Byte()
arr = BitConverter.GetBytes(Target)
WriteByte(arr(1))
WriteByte(arr(0))
End Sub
Sub WriteSingle(ByVal Target As Double)
Dim arr As Byte()
arr = BitConverter.GetBytes(Target)
For Each bytte As Byte In arr
WriteByte(bytte)
Next
End Sub
End Class
End Namespace
Voila :)