CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/rex/proto/proxy/socks5/packet.rb
Views: 11765
1
# -*- coding: binary -*-
2
3
require 'bindata'
4
require 'rex/socket'
5
6
module Rex
7
module Proto
8
module Proxy
9
10
module Socks5
11
SOCKS_VERSION = 5
12
13
#
14
# Mixin for socks5 packets to include an address field.
15
#
16
module Address
17
ADDRESS_TYPE_IPV4 = 1
18
ADDRESS_TYPE_DOMAINNAME = 3
19
ADDRESS_TYPE_IPV6 = 4
20
21
def address
22
addr = address_array.to_ary.pack('C*')
23
if address_type == ADDRESS_TYPE_IPV4 || address_type == ADDRESS_TYPE_IPV6
24
addr = Rex::Socket.addr_ntoa(addr)
25
end
26
addr
27
end
28
29
def address=(value)
30
if Rex::Socket.is_ipv4?(value)
31
address_type.assign(ADDRESS_TYPE_IPV4)
32
domainname_length.assign(0)
33
value = Rex::Socket.addr_aton(value)
34
elsif Rex::Socket.is_ipv6?(value)
35
address_type.assign(ADDRESS_TYPE_IPV6)
36
domainname_length.assign(0)
37
value = Rex::Socket.addr_aton(value)
38
else
39
address_type.assign(ADDRESS_TYPE_DOMAINNAME)
40
domainname_length.assign(value.length)
41
end
42
address_array.assign(value.unpack('C*'))
43
end
44
45
def address_length
46
case address_type
47
when ADDRESS_TYPE_IPV4
48
4
49
when ADDRESS_TYPE_DOMAINNAME
50
domainname_length
51
when ADDRESS_TYPE_IPV6
52
16
53
else
54
0
55
end
56
end
57
end
58
59
class AuthRequestPacket < BinData::Record
60
endian :big
61
62
uint8 :version, :initial_value => SOCKS_VERSION
63
uint8 :supported_methods_length
64
array :supported_methods, :type => :uint8, :initial_length => :supported_methods_length
65
end
66
67
class AuthResponsePacket < BinData::Record
68
endian :big
69
70
uint8 :version, :initial_value => SOCKS_VERSION
71
uint8 :chosen_method
72
end
73
74
class Packet < BinData::Record
75
include Address
76
endian :big
77
hide :reserved, :domainname_length
78
79
uint8 :version, :initial_value => SOCKS_VERSION
80
uint8 :command
81
uint8 :reserved
82
uint8 :address_type
83
uint8 :domainname_length, :onlyif => lambda { address_type == ADDRESS_TYPE_DOMAINNAME }
84
array :address_array, :type => :uint8, :initial_length => lambda { address_length }
85
uint16 :port
86
end
87
88
class RequestPacket < Packet
89
end
90
91
class ResponsePacket < Packet
92
end
93
94
class UdpPacket < BinData::Record
95
include Address
96
endian :big
97
hide :reserved, :domainname_length
98
99
uint16 :reserved
100
uint8 :frag
101
uint8 :address_type
102
uint8 :domainname_length, :onlyif => lambda { address_type == ADDRESS_TYPE_DOMAINNAME }
103
array :address_array, :type => :uint8, :initial_length => lambda { address_length }
104
uint16 :port
105
end
106
end
107
end
108
end
109
end
110
111