01-02-18, 07:44 PM
الجزء الاول /// Class
كود :
Imports System.IO
Imports System.Net.Sockets
Imports System.Text
Friend Class MK
Private connection As Stream
Private con As TcpClient
Public Sub New(ByVal ip As String)
con = New TcpClient()
con.Connect(ip, 8728)
connection = CType(con.GetStream(), Stream)
End Sub
Public Sub Close()
connection.Close()
con.Close()
End Sub
Public Function Login(ByVal username As String, ByVal password As String) As Boolean
Send("/login", True)
Dim hash As String = Read()(0).Split(New String() {"ret="}, StringSplitOptions.None)(1)
Send("/login")
Send("=name=" & username)
Send("=response=00" & EncodePassword(password, hash), True)
If Read()(0) = "!done" Then
Return True
Else
Return False
End If
End Function
Public Sub Send(ByVal co As String)
Dim bajty() As Byte = Encoding.ASCII.GetBytes(co.ToCharArray())
Dim velikost() As Byte = EncodeLength(bajty.Length)
connection.Write(velikost, 0, velikost.Length)
connection.Write(bajty, 0, bajty.Length)
End Sub
Public Sub Send(ByVal co As String, ByVal endsentence As Boolean)
Dim bajty() As Byte = Encoding.ASCII.GetBytes(co.ToCharArray())
Dim velikost() As Byte = EncodeLength(bajty.Length)
connection.Write(velikost, 0, velikost.Length)
connection.Write(bajty, 0, bajty.Length)
connection.WriteByte(0)
End Sub
Public Function Read() As List(Of String)
Dim output As New List(Of String)()
Dim o As String = ""
Dim tmp(3) As Byte
Dim count As Long
Do
tmp(3) = CByte(connection.ReadByte())
'if(tmp[3] == 220) tmp[3] = (byte)connection.ReadByte(); it sometimes happend to me that
'mikrotik send 220 as some kind of "bonus" between words, this fixed things, not sure about it though
If tmp(3) = 0 Then
output.Add(o)
If o.Substring(0, 5) = "!done" Then
Exit Do
Else
o = ""
Continue Do
End If
Else
If tmp(3) < &H80 Then
count = tmp(3)
Else
If tmp(3) < &HC0 Then
Dim tmpi As Integer = BitConverter.ToInt32(New Byte() {CByte(connection.ReadByte()), tmp(3), 0, 0}, 0)
count = tmpi Xor &H8000
Else
If tmp(3) < &HE0 Then
tmp(2) = CByte(connection.ReadByte())
Dim tmpi As Integer = BitConverter.ToInt32(New Byte() {CByte(connection.ReadByte()), tmp(2), tmp(3), 0}, 0)
count = tmpi Xor &HC00000
Else
If tmp(3) < &HF0 Then
tmp(2) = CByte(connection.ReadByte())
tmp(1) = CByte(connection.ReadByte())
Dim tmpi As Integer = BitConverter.ToInt32(New Byte() {CByte(connection.ReadByte()), tmp(1), tmp(2), tmp(3)}, 0)
count = tmpi Xor &HE0000000UI
Else
If tmp(3) = &HF0 Then
tmp(3) = CByte(connection.ReadByte())
tmp(2) = CByte(connection.ReadByte())
tmp(1) = CByte(connection.ReadByte())
tmp(0) = CByte(connection.ReadByte())
count = BitConverter.ToInt32(tmp, 0)
Else
'Error in packet reception, unknown length
Exit Do
End If
End If
End If
End If
End If
End If
For i As Integer = 0 To count - 1
o &= ChrW(connection.ReadByte())
Next i
Loop
Return output
End Function
Private Function EncodeLength(ByVal delka As Integer) As Byte()
If delka < &H80 Then
Dim tmp() As Byte = BitConverter.GetBytes(delka)
Return New Byte(0) {tmp(0)}
End If
If delka < &H4000 Then
Dim tmp() As Byte = BitConverter.GetBytes(delka Or &H8000)
Return New Byte(1) {tmp(1), tmp(0)}
End If
If delka < &H200000 Then
Dim tmp() As Byte = BitConverter.GetBytes(delka Or &HC00000)
Return New Byte(2) {tmp(2), tmp(1), tmp(0)}
End If
If delka < &H10000000 Then
Dim tmp() As Byte = BitConverter.GetBytes(delka Or &HE0000000UI)
Return New Byte(3) {tmp(3), tmp(2), tmp(1), tmp(0)}
Else
Dim tmp() As Byte = BitConverter.GetBytes(delka)
Return New Byte(4) {&HF0, tmp(3), tmp(2), tmp(1), tmp(0)}
End If
End Function
Public Function EncodePassword(ByVal Password As String, ByVal hash As String) As String
Dim hash_byte((hash.Length \ 2) - 1) As Byte
For i As Integer = 0 To hash.Length - 2 Step 2
hash_byte(i \ 2) = Byte.Parse(hash.Substring(i, 2), System.Globalization.NumberStyles.HexNumber)
Next i
Dim heslo((1 + Password.Length + hash_byte.Length) - 1) As Byte
heslo(0) = 0
Encoding.ASCII.GetBytes(Password.ToCharArray()).CopyTo(heslo, 1)
hash_byte.CopyTo(heslo, 1 + Password.Length)
Dim hotovo() As Byte
Dim md5 As System.Security.Cryptography.MD5
md5 = New System.Security.Cryptography.MD5CryptoServiceProvider()
hotovo = md5.ComputeHash(heslo)
'Convert encoded bytes back to a 'readable' string
Dim navrat As String = ""
For Each h As Byte In hotovo
navrat &= h.ToString("x2")
Next h
Return navrat
End Function
End Class
الجزء الثانى : Example
كود :
Imports System.IO
Imports System.Net.Sockets
Public Class Form1
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim mikrotik As New MK("your ip here")
If Not mikrotik.Login("username", "password") Then
Console.WriteLine("Could not log in")
mikrotik.Close()
Return
End If
mikrotik.Send("/system/identity/getall")
mikrotik.Send(".tag=sss", True)
For Each h As String In mikrotik.Read()
Console.WriteLine(h)
Next h
Console.ReadKey()
End Sub
End Class
End Class
الجزء الثالث : Example 2
كود :
Imports System.IO
Imports System.Net.Sockets
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim ip As String = args(0)
Dim mikrotik As New MK("your ip here")
If mikrotik.Login("admin", "P@ssW0rd") Then
mikrotik.Send("/ip/firewall/filter/add")
mikrotik.Send("=action=drop")
mikrotik.Send("=chain=forward")
mikrotik.Send("=dst-port=25")
mikrotik.Send("=protocol=tcp")
mikrotik.Send("=protocol=tcp")
mikrotik.Send(String.Format("=src-address={0}",ip))
mikrotik.Send(".tag=firewall", True)
For Each h As String In mikrotik.Read()
Console.WriteLine(h)
Next h
End If
End Sub
End Class
اعملو ازاى ؟