Socks5 Implementierung

  • C#
  • .NET (FX) 4.5–4.8

    Socks5 Implementierung

    Hallo, :D

    ich bin gerade dabei manuell ein Socks5 Handshake durch zu führen und bin dabei auf das Problem gestoßen das in Wiki folgendes steht

    field 5: destination address of4 bytes for IPv4 address
    1 byte of name length followed by 1–255 bytes the domain name
    16 bytes for IPv6 address


    destination address, ist das dann sowas wie google.de Oo, denn so ganz versteh ich [nicht] wie das gemeint ist, den der Socks läuft local auf Port 9050 und da bin ich schon per TCP verbunden Oo.
    Was ist mit destination address gemeint? Geklärt - Sieh Edit

    Mein Code :whistling:

    Spoiler anzeigen

    C#-Quellcode

    1. //call
    2. Socks5.Create("127.0.0.1", 9050);
    3. public class Socks5
    4. {
    5. public static void Create(string hostname, int port, CommandCodeType commandCode = CommandCodeType.TcpIpStream, SocksType type = SocksType.Socks5)
    6. {
    7. TcpClient client = new TcpClient(hostname, port);
    8. NetworkStream networkStream = client.GetStream();
    9. BinaryWriter writer = new BinaryWriter(networkStream);
    10. BinaryReader reader = new BinaryReader(networkStream);
    11. Ping ping = new Ping();
    12. PingReply reply = ping.Send(hostname);
    13. IPAddress address = reply.Address;
    14. byte[] valAdress = address.GetAddressBytes();
    15. byte[] valPort = BitConverter.GetBytes(port);
    16. writer.Write(new[] { (byte)type, (byte)0x01, (byte) AuthenticationType.NoAuthentication});
    17. byte[] response = reader.ReadBytes(2);
    18. if (response[1] != 0xFF)
    19. {
    20. //writer.Write(new[]{
    21. // (byte)0x01,
    22. // (byte)0x01,
    23. // (byte)0x01,
    24. // (byte)0x01,
    25. // (byte)0x01,
    26. //});
    27. //Console.WriteLine(string.Concat(reader.ReadBytes(2)));
    28. writer.Write(new[] {
    29. (byte)type,
    30. (byte)commandCode,
    31. (byte)0x00,
    32. (byte)TargetAddressType.Domain
    33. });
    34. writer.Write((byte) "google.de".Length);
    35. writer.Write(Encoding.Default.GetBytes("google.de"));
    36. writer.Write(new[] {
    37. (byte) (80>>8),
    38. (byte) (80 & 0xff)
    39. });
    40. //valAdress[0],
    41. //valAdress[1],
    42. //valAdress[2],
    43. //valAdress[3],
    44. Console.WriteLine(string.Concat(reader.ReadBytes(10)));
    45. }
    46. else
    47. {
    48. Console.WriteLine("Connection error");
    49. }
    50. }
    51. }
    52. public enum SocksType : byte
    53. {
    54. Socks5 = 0x5
    55. }
    56. public enum AuthenticationType : byte
    57. {
    58. NoAuthentication = 0x00,
    59. GSSAPI = 0x1,
    60. Login = 0x02,
    61. NoAcceptableMethods = 0xff
    62. }
    63. public enum CommandCodeType : byte
    64. {
    65. TcpIpStream = 0x1,
    66. TcpIpPortBinding = 0x2,
    67. UdpAssociate = 0x3
    68. }
    69. public enum TargetAddressType
    70. {
    71. IPV4 = 0x01,
    72. Domain = 0x03,
    73. IPv6 = 0x04
    74. }


    Edit:

    auf grund dieses bildes ist mir nun klar, was gemeint ist, nur weiß ich dann nicht, wieso ich ein 0x5 0x0 0x0 0x1 0x0 0x0 0x0 0x0 0x0 0x0 bekomme abwohl in der Wiki docu was anderes steht Oo

    Server response:

    field 1: SOCKS protocol version, 1 byte (0x05 for this version)
    field 2: status, 1 byte:0x00: request granted
    0x01: general failure
    0x02: connection not allowed by ruleset
    0x03: network unreachable
    0x04: host unreachable
    0x05: connection refused by destination host
    0x06: TTL expired
    0x07: command not supported / protocol error
    0x08: address type not supported

    field 3: reserved, must be 0x00, 1 byte
    field 4: address type, 1 byte:0x01: IPv4 address
    0x03: Domain name
    0x04: IPv6 address

    field 5: server bound address of4 bytes for IPv4 address
    1 byte of name length followed by 1–255 bytes the domain name
    16 bytes for IPv6 address

    field 6: server bound port number in a network byte order, 2 bytes


    *Topic verschoben*

    Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „Marcus Gräfe“ ()