Benvenuto! Per postare e godere delle funzionalità del forum registrati, occorrono pochi secondi ed è gratis!

[VB.NET] Bloccare un sito con VB.NET senza file hosts
(Questo messaggio è stato modificato l'ultima volta il: 13/08/2014, 10:20 da Admin.)

Amministratore
Messaggi: 2,316
Discussioni: 231
Registrato: 03-2011
Reputazione:
78

Mix: 2,547
Con questa guida sarete in grado di bloccare un determinato sito per evitare che qualcuno ci navighi. Se un sito ha più indirizzi IP, vengono bloccati tutti.
Non sono necessarie dichiarazioni aggiuntive, tutto quello che dovete fare oltre al copia incolla (e magari un po' di studio del codice Asd) è quello di aggiungere la classe nel codice appunto, e la linea di codice che attiverà il blocco dei siti.

    VBNET Programming
  1. Imports System.Net
  2. Imports System.Net.NetworkInformation
  3. Imports System.Runtime.InteropServices
  4. Public Class CyBlock
  5. Shared hostAddresses As String() = Nothing
  6. Shared conThread As Boolean = True
  7.  
  8. Public Overloads Shared Sub BlockWebsite(ByVal hostAddr As String)
  9. ReDim hostAddresses(0)
  10. hostAddresses(0) = hostAddr
  11. Dim x As New Threading.Thread(AddressOf Main) With {.IsBackground = True} : x.SetApartmentState(Threading.ApartmentState.STA)
  12. x.Start()
  13. End Sub
  14.  
  15. Public Overloads Shared Sub BlockWebsite(ByVal hostAddrs As String())
  16. ReDim hostAddresses(hostAddrs.Length - 1)
  17. hostAddresses = hostAddrs
  18. Dim x As New Threading.Thread(AddressOf Main) With {.IsBackground = True} : x.SetApartmentState(Threading.ApartmentState.STA)
  19. x.Start()
  20. End Sub
  21.  
  22. Public Overloads Shared Sub Main()
  23. Dim iBlocks As New Collection
  24. While conThread
  25. For Each hostAddress As String In hostAddresses
  26. If hostAddress.Split("."c).Length < 4 Then
  27. For Each ipAdr As IPAddress In Dns.GetHostAddresses(hostAddress)
  28. iBlocks.Add(ipAdr.ToString)
  29. Next
  30. Else
  31. iBlocks.Add(hostAddress)
  32. End If
  33. Dim locIP, remIP, locPort, remPort As String
  34. Dim iProps As IPGlobalProperties = IPGlobalProperties.GetIPGlobalProperties
  35. For Each tcpCnx As TcpConnectionInformation In iProps.GetActiveTcpConnections
  36. For Each ipAdr As String In iBlocks
  37. If tcpCnx.RemoteEndPoint.Address.ToString = ipAdr Then
  38. locIP = tcpCnx.LocalEndPoint.Address.ToString
  39. locPort = tcpCnx.LocalEndPoint.Port
  40. remIP = tcpCnx.RemoteEndPoint.Address.ToString
  41. remPort = tcpCnx.RemoteEndPoint.Port
  42. BlockWebsite(locIP, locPort, remIP, remPort)
  43. Exit For
  44. End If
  45. Next
  46. Next
  47. Next
  48. End While
  49. End Sub
  50.  
  51. Private Overloads Shared Sub BlockWebsite(ByVal locIP As String, :
  52. ByVal locPort As String, ByVal remIP As String, ByVal remPort As String)
  53. Try
  54. Dim locAdr() As String = locIP.Split("."c)
  55. Dim remAdr() As String = remIP.Split("."c)
  56. Dim bLocAddr() As Byte = {Byte.Parse(locAdr(0)), Byte.Parse(locAdr(1)), Byte.Parse(locAdr(2)), Byte.Parse(locAdr(3))}
  57. Dim bRemAddr() As Byte = {Byte.Parse(remAdr(0)), Byte.Parse(remAdr(1)), Byte.Parse(remAdr(2)), Byte.Parse(remAdr(3))}
  58. Dim row As New TcpInfo() With {.cnxLocAdr = BitConverter.ToInt32(bLocAddr, 0), :
  59. .cnxLocPort = htons(Integer.Parse(locPort)), .cnxRemAdr = BitConverter.ToInt32(bRemAddr, 0), :
  60. .cnxRemPort = htons(Integer.Parse(remPort)), .cnxState = 12}
  61. Dim tPtr As IntPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(row))
  62. Marshal.StructureToPtr(row, tPtr, False)
  63. Dim ptr As IntPtr = tPtr
  64. Dim ret As Integer = SetTcpEntry(ptr)
  65. Catch ex As Exception
  66. End Try
  67. End Sub
  68.  
  69. Public Shared Sub UnblockWebsite()
  70. conThread = False
  71.  
  72. End Sub
  73. Private Structure TcpInfo
  74. Public cnxState As Integer
  75. Public cnxLocAdr As Integer
  76. Public cnxLocPort As Integer
  77. Public cnxRemAdr As Integer
  78. Public cnxRemPort As Integer
  79. End Structure
  80. <DllImport("iphlpapi.dll")> Private Shared Function SetTcpEntry(ByVal pTcprow As IntPtr) As Integer
  81. End Function
  82. <DllImport("wsock32.dll")> Private Shared Function htons(ByVal netshort As Integer) As Integer
  83. End Function
  84. End Class



Linea per il bloccaggio
    VBNET Programming
  1. CyBlock.BlockWebsite(New String() {"www.google.com", "www.hackforums.net", "www.buymyproduct.net"})
  2. 'Oppure
  3. CyBlock.BlockWebsite("www.google.com")
  4. 'Oppure
  5. CyBlock.BlockWebsite(New String() {"192.168.1.1", "159.185.47.1", "235.187.57.10"})
  6. 'Oppure
  7. CyBlock.BlockWebsite("192.168.1.254")



Linea per lo sbloccaggio
    VBNET Programming
  1. CyBlock.UnblockWebsite()



Crediti: cysto


[Per vedere i link devi REGISTRARTI.]

Prima di aprire ogni contenuto scaricato su siti esterni ad HackMix, scansionatelo su VirusTotal.
13/08/2014, 10:19
#1
Cerca




Utenti che stanno guardando questa discussione: 1 Ospite(i)