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/ldap/dn_binary.rb
Views: 11704
1
module Rex
2
module Proto
3
module LDAP
4
5
# Handle converting objects into the DN-Binary syntax
6
# See: https://learn.microsoft.com/en-us/windows/win32/adschema/s-object-dn-binary
7
class DnBinary
8
def initialize(dn, data)
9
self.dn = dn
10
self.data = data
11
end
12
13
# Turn a DN-Binary string into a structured object containing data and a DN
14
# @param str [String] A DN-Binary-formatted string
15
def self.decode(str)
16
groups = str.match(/B:(\d+):(([a-fA-F0-9]{2})*):(.*)/)
17
raise ArgumentError.new('Invalid DN Binary string') if groups.nil?
18
length = groups[1].to_i
19
raise ArgumentError.new('Invalid DN Binary string length') if groups[2].length != length
20
data = [groups[2]].pack('H*')
21
22
DnBinary.new(groups[4], data)
23
end
24
25
# Turn this structured object containing data and a DN into a DN-Binary string
26
# @return [String] The DN-Binary-formatted string
27
def encode
28
data_hex = self.data.unpack('H*')[0]
29
30
"B:#{data_hex.length}:#{data_hex}:#{self.dn}"
31
end
32
33
attr_accessor :data # Raw bytes
34
attr_accessor :dn # LDAP Distinguished name
35
end
36
end
37
end
38
end
39