Advertisement
coderail

Packet Sniffer/Editor - VB.NET

Mar 5th, 2012
2,722
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.40 KB | None | 0 0
  1. Imports System
  2. Imports System.Net
  3. Imports System.Net.Sockets
  4. Imports System.Threading
  5. Imports System.Collections.Generic
  6.  
  7. 'Installation:
  8. '-----------------
  9. '1.) Navigate to Windows Control Panel-> Device Manager.
  10. '2.) Select the top most item (ex. Name-PC), right click, and Add Legacy Hardware.
  11. '3.) When prompted choose to install the hardware you select from a list (Advanced).
  12. '4.) Install Network Adapter-> Microsoft-> Microsoft Loopback Adapter.
  13.  
  14. 'Configuration:
  15. '-----------------
  16. '1.) Navigate to Windows Control Panel-> Network and Sharing Center.
  17. '2.) Find the option 'Change adapter settings', usually found on the top left.
  18. '3.) Once in Network Connections, right click the Loopback Adapater and choose Properties.
  19. '4.) (Optional) Disable everything but Internet Protocol Version 4 (TCP/IPv4).
  20. '5.) Select Internet Protocol Version 4 and click on Properties.
  21. '6.) Set the IP address to your target ip address and press OK.
  22.  
  23. 'Important:
  24. '-----------------
  25. 'Disable the adapter when not in use. Follow Configuration steps 1 to 3
  26. 'but instead of choosing Properties, click Enable/Disable instead.
  27.  
  28.  
  29. '------------------
  30. 'Creator: aeonhack
  31. 'Site: elitevs.net
  32. 'Created: 7/10/2010
  33. 'Changed: 1/15/2012
  34. 'Version: 2.0.0
  35. '------------------
  36. Class Carrier
  37.  
  38. Delegate Sub RecieveDG(ByVal connection As Interceptor, ByVal data As Byte())
  39. Delegate Sub ConnectionDG(ByVal connection As Interceptor)
  40. Delegate Sub StateChangeDG()
  41.  
  42. Public ServerToClient As RecieveDG
  43. Public ClientToServer As RecieveDG
  44. Public ConnectEvent As ConnectionDG
  45. Public DisconnectEvent As ConnectionDG
  46. Public StateChange As StateChangeDG
  47.  
  48. Private DisconnectFilter As ConnectionDG
  49.  
  50. Private Master As Socket
  51. Private LocalEndPoint As IPEndPoint
  52. Private RemoteEndPoint As IPEndPoint
  53.  
  54. Private ConnectionPool As Stack(Of Interceptor)
  55.  
  56. Private _Connections As Dictionary(Of Guid, Interceptor)
  57. ReadOnly Property Connections As Dictionary(Of Guid, Interceptor)
  58. Get
  59. Return _Connections
  60. End Get
  61. End Property
  62.  
  63. Private _Listening As Boolean
  64. ReadOnly Property Listening As Boolean
  65. Get
  66. Return _Listening
  67. End Get
  68. End Property
  69.  
  70. Sub New()
  71. _Connections = New Dictionary(Of Guid, Interceptor)
  72. ConnectionPool = New Stack(Of Interceptor)
  73.  
  74. ServerToClient = New RecieveDG(AddressOf RecieveHandler)
  75. ClientToServer = New RecieveDG(AddressOf RecieveHandler)
  76. ConnectEvent = New ConnectionDG(AddressOf ConnectionHandler)
  77. DisconnectEvent = New ConnectionDG(AddressOf ConnectionHandler)
  78. StateChange = New StateChangeDG(AddressOf StateChangeHandler)
  79.  
  80. DisconnectFilter = New ConnectionDG(AddressOf DisconnectFilterHandler)
  81. End Sub
  82.  
  83. #Region " Handlers "
  84. Private Sub RecieveHandler(ByVal connection As Interceptor, ByVal data As Byte())
  85. Return
  86. End Sub
  87. Private Sub ConnectionHandler(ByVal connection As Interceptor)
  88. Return
  89. End Sub
  90. Private Sub StateChangeHandler()
  91. Return
  92. End Sub
  93.  
  94. Private Sub DisconnectFilterHandler(ByVal connection As Interceptor)
  95. SyncLock Me
  96. _Connections.Remove(connection.GUID)
  97. ConnectionPool.Push(connection)
  98. End SyncLock
  99.  
  100. DisconnectEvent(connection)
  101. End Sub
  102.  
  103. #End Region
  104.  
  105. Private Function IPv4Addresses() As IPAddress()
  106. Dim T As New List(Of IPAddress)
  107.  
  108. For Each I As IPAddress In Dns.GetHostEntry(Dns.GetHostName).AddressList
  109. If IsPrivateIPv4(I) Then T.Add(I)
  110. Next
  111.  
  112. Return T.ToArray()
  113. End Function
  114. Private Function IsPrivateIPv4(ByVal address As IPAddress) As Boolean
  115. If Not address.AddressFamily = AddressFamily.InterNetwork Then Return False
  116.  
  117. Dim I As Byte() = address.GetAddressBytes
  118. Return I(0) = 10 OrElse (I(0) = 172 AndAlso I(1) > 15 AndAlso I(1) < 32) OrElse (I(0) = 192 AndAlso I(1) = 168)
  119. End Function
  120.  
  121. Sub Listen(ByVal remoteHost As String, ByVal remotePort As UShort)
  122. Try
  123. SyncLock Me
  124. If _Listening Then Return
  125. _Listening = True
  126.  
  127. StateChange()
  128.  
  129. LocalEndPoint = New IPEndPoint(IPv4Addresses(0), 0)
  130. RemoteEndPoint = New IPEndPoint(IPAddress.Parse(remoteHost), remotePort)
  131.  
  132. For I As Integer = 1 To 20 'Change this for more concurrent connections.
  133. ConnectionPool.Push(New Interceptor(LocalEndPoint, RemoteEndPoint, ServerToClient, ClientToServer, ConnectEvent, DisconnectFilter))
  134. Next
  135.  
  136. Master = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
  137. Master.Bind(RemoteEndPoint)
  138. Master.Listen(3)
  139.  
  140. Dim E As New SocketAsyncEventArgs
  141. AddHandler E.Completed, AddressOf AcceptAsync
  142.  
  143. If Not Master.AcceptAsync(E) Then
  144. AcceptAsync(Master, E)
  145. End If
  146. End SyncLock
  147. Catch
  148. Close()
  149. End Try
  150. End Sub
  151.  
  152. Private Sub AcceptAsync(ByVal sender As Object, ByVal e As SocketAsyncEventArgs)
  153. Try
  154. If e.LastOperation = SocketAsyncOperation.Accept AndAlso e.SocketError = SocketError.Success Then
  155. SyncLock Me
  156. Dim Interceptor As Interceptor = ConnectionPool.Pop
  157. _Connections.Add(Interceptor.GUID, Interceptor)
  158. Interceptor.Initialize(e.AcceptSocket)
  159. End SyncLock
  160.  
  161. e.AcceptSocket = Nothing
  162. If Not Master.AcceptAsync(e) Then
  163. AcceptAsync(Master, e)
  164. End If
  165. Else
  166. Close()
  167. End If
  168. Catch
  169. Close()
  170. End Try
  171. End Sub
  172.  
  173. Sub Close()
  174. SyncLock Me
  175. If Not _Listening Then Return
  176. _Listening = False
  177.  
  178. Try
  179. Master.Close()
  180. Catch
  181. 'Do Nothing
  182. End Try
  183.  
  184. Dim E As Dictionary(Of Guid, Interceptor).Enumerator = _Connections.GetEnumerator
  185. While E.MoveNext
  186. E.Current.Value.Close()
  187. End While
  188.  
  189. _Connections.Clear()
  190. End SyncLock
  191. End Sub
  192.  
  193. End Class
  194.  
  195. Class Interceptor
  196.  
  197. Private Closing As Boolean
  198.  
  199. Private ClientSocket As Socket
  200. Private ServerSocket As Socket
  201.  
  202. Private LocalEndPoint As IPEndPoint
  203. Private RemoteEndPoint As IPEndPoint
  204.  
  205. Private ConnectEventArgs As SocketAsyncEventArgs
  206. Private ServerRead As SocketAsyncEventArgs
  207. Private ClientRead As SocketAsyncEventArgs
  208.  
  209. Private ServerRecvEvent As Carrier.RecieveDG
  210. Private ClientRecvEvent As Carrier.RecieveDG
  211. Private ConnectEvent As Carrier.ConnectionDG
  212. Private DisconnectEvent As Carrier.ConnectionDG
  213.  
  214. Private Const BufferSize As Integer = 65535 'Change this to increase maximum packet size.
  215.  
  216. Private _GUID As Guid
  217. ReadOnly Property GUID As Guid
  218. Get
  219. Return _GUID
  220. End Get
  221. End Property
  222.  
  223. Sub New(ByVal local As IPEndPoint, ByVal remote As IPEndPoint, _
  224. ByVal serverRecv As Carrier.RecieveDG, _
  225. ByVal clientRecv As Carrier.RecieveDG, _
  226. ByVal connect As Carrier.ConnectionDG, _
  227. ByVal disconnect As Carrier.ConnectionDG)
  228.  
  229. _GUID = GUID.NewGuid
  230.  
  231. LocalEndPoint = local
  232. RemoteEndPoint = remote
  233.  
  234. ServerRecvEvent = serverRecv
  235. ClientRecvEvent = clientRecv
  236. ConnectEvent = connect
  237. DisconnectEvent = disconnect
  238.  
  239. ConnectEventArgs = New SocketAsyncEventArgs
  240. ConnectEventArgs.RemoteEndPoint = remote
  241.  
  242. ServerRead = New SocketAsyncEventArgs
  243. ClientRead = New SocketAsyncEventArgs
  244.  
  245. ServerRead.SetBuffer(New Byte(BufferSize - 1) {}, 0, BufferSize)
  246. ClientRead.SetBuffer(New Byte(BufferSize - 1) {}, 0, BufferSize)
  247.  
  248. AddHandler ConnectEventArgs.Completed, AddressOf ConnectAsync
  249. AddHandler ServerRead.Completed, AddressOf ServerReadAsync
  250. AddHandler ClientRead.Completed, AddressOf ClientReadAsync
  251. End Sub
  252.  
  253. Sub Initialize(ByVal socket As Socket)
  254. Try
  255. Closing = False
  256. ClientSocket = socket
  257.  
  258. ServerSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
  259. ServerSocket.Bind(LocalEndPoint)
  260.  
  261. If Not ServerSocket.ConnectAsync(ConnectEventArgs) Then
  262. ConnectAsync(ServerSocket, ConnectEventArgs)
  263. End If
  264. Catch
  265. Close()
  266. End Try
  267. End Sub
  268.  
  269. Private Sub ConnectAsync(ByVal sender As Object, ByVal e As SocketAsyncEventArgs)
  270. Try
  271. If e.LastOperation = SocketAsyncOperation.Connect AndAlso e.SocketError = SocketError.Success Then
  272. ConnectEvent(Me)
  273.  
  274. Dim T As New Thread(AddressOf GuaranteeClientRead)
  275. T.IsBackground = True
  276. T.Start()
  277.  
  278. If Not ServerSocket.ReceiveAsync(ServerRead) Then
  279. ServerReadAsync(ServerSocket, ServerRead)
  280. End If
  281. Else
  282. Close()
  283. End If
  284. Catch
  285. Close()
  286. End Try
  287. End Sub
  288.  
  289. Private Sub GuaranteeClientRead()
  290. If Not ClientSocket.ReceiveAsync(ClientRead) Then
  291. ClientReadAsync(ClientSocket, ClientRead)
  292. End If
  293. End Sub
  294.  
  295. Private Sub ServerReadAsync(ByVal sender As Object, ByVal e As SocketAsyncEventArgs)
  296. Try
  297. If e.LastOperation = SocketAsyncOperation.Receive AndAlso e.SocketError = SocketError.Success AndAlso e.BytesTransferred > 0 Then
  298. Dim Data(e.BytesTransferred - 1) As Byte
  299. Buffer.BlockCopy(e.Buffer, 0, Data, 0, e.BytesTransferred)
  300. ServerRecvEvent(Me, Data)
  301.  
  302. If Not ServerSocket.ReceiveAsync(e) Then
  303. ServerReadAsync(ServerSocket, e)
  304. End If
  305. Else
  306. Close()
  307. End If
  308. Catch
  309. Close()
  310. End Try
  311. End Sub
  312.  
  313. Private Sub ClientReadAsync(ByVal sender As Object, ByVal e As SocketAsyncEventArgs)
  314. Try
  315. If e.LastOperation = SocketAsyncOperation.Receive AndAlso e.SocketError = SocketError.Success AndAlso e.BytesTransferred > 0 Then
  316. Dim Data(e.BytesTransferred - 1) As Byte
  317. Buffer.BlockCopy(e.Buffer, 0, Data, 0, e.BytesTransferred)
  318. ClientRecvEvent(Me, Data)
  319.  
  320. If Not ClientSocket.ReceiveAsync(e) Then
  321. ClientReadAsync(ClientSocket, e)
  322. End If
  323. Else
  324. Close()
  325. End If
  326. Catch
  327. Close()
  328. End Try
  329. End Sub
  330.  
  331. Private Sub SendAsync(ByVal sender As Object, ByVal e As SocketAsyncEventArgs)
  332. If Not e.LastOperation = SocketAsyncOperation.Send AndAlso e.SocketError = SocketError.Success Then
  333. Close()
  334. End If
  335. End Sub
  336.  
  337. Sub Close()
  338. SyncLock Me
  339. If Closing Then Return
  340. Closing = True
  341.  
  342. Try
  343. ServerSocket.Close()
  344. Catch
  345. 'Do nothing
  346. End Try
  347.  
  348. Try
  349. ClientSocket.Close()
  350. Catch
  351. 'Do nothing
  352. End Try
  353.  
  354. ServerSocket = Nothing
  355. ClientSocket = Nothing
  356.  
  357. DisconnectEvent(Me)
  358. End SyncLock
  359. End Sub
  360.  
  361. Sub SendToServer(ByVal data As Byte())
  362. Try
  363. Dim E As New SocketAsyncEventArgs
  364. E.SetBuffer(data, 0, data.Length)
  365. AddHandler E.Completed, AddressOf SendAsync
  366.  
  367. If Not ServerSocket.SendAsync(E) Then
  368. SendAsync(ServerSocket, E)
  369. End If
  370. Catch
  371. Close()
  372. End Try
  373. End Sub
  374.  
  375. Sub SendToClient(ByVal data As Byte())
  376. Try
  377. Dim E As New SocketAsyncEventArgs
  378. E.SetBuffer(data, 0, data.Length)
  379. AddHandler E.Completed, AddressOf SendAsync
  380.  
  381. If Not ClientSocket.SendAsync(E) Then
  382. SendAsync(ClientSocket, E)
  383. End If
  384. Catch
  385. Close()
  386. End Try
  387. End Sub
  388.  
  389. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement