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/parser/net_sarang.rb
Views: 11780
1
2
module Rex
3
module Parser
4
module NetSarang
5
# @author Kali-Team
6
class NetSarangCrypto
7
attr_accessor :version
8
attr_accessor :username
9
attr_accessor :sid
10
attr_accessor :master_password
11
attr_accessor :key
12
13
# This class implements encryption and decryption of NetSarang
14
#
15
# @param type [String] only Xshell or Xftp.
16
# @param version [String] Specify version of session file. e.g.:5.3
17
# @param username [String] Specify username. This parameter will be used if version > 5.2.
18
# @param sid [String] Specify SID. This parameter will be used if version >= 5.1.
19
# @option master_password [String] Specify user's master password.
20
#
21
# @return [Rex::Parser::NetSarang::NetSarangCrypto] The NetSarangCrypto object
22
def initialize(type, version, username, sid, master_password = nil)
23
self.version = version.to_f
24
self.username = username
25
self.sid = sid
26
self.master_password = master_password
27
md5 = OpenSSL::Digest.new('MD5')
28
sha256 = OpenSSL::Digest.new('SHA256')
29
if (self.version > 0) && (self.version < 5.1)
30
self.key = (type == 'Xshell') ? md5.digest('!X@s#h$e%l^l&') : md5.digest('!X@s#c$e%l^l&')
31
elsif (self.version >= 5.1) && (self.version <= 5.2)
32
self.key = sha256.digest(self.sid)
33
elsif (self.version > 5.2)
34
if self.master_password.nil?
35
self.key = sha256.digest(self.username + self.sid)
36
else
37
self.key = sha256.digest(self.master_password)
38
end
39
else
40
raise 'Invalid argument: version'
41
end
42
end
43
44
# Encrypt
45
#
46
# @param string [String]
47
# @return [String] ciphertext
48
def encrypt_string(string)
49
cipher = Rex::Crypto.rc4(key, string)
50
if (version < 5.1)
51
return Rex::Text.encode_base64(cipher)
52
else
53
sha256 = OpenSSL::Digest.new('SHA256')
54
checksum = sha256.digest(string)
55
ciphertext = cipher
56
return Rex::Text.encode_base64(ciphertext + checksum)
57
end
58
end
59
60
# Decrypt
61
#
62
# @param string [String]
63
# @return [String] plaintext failed return nil
64
def decrypt_string(string)
65
if (version < 5.1)
66
return Rex::Crypto.rc4(key, Rex::Text.decode_base64(string))
67
else
68
data = Rex::Text.decode_base64(string)
69
ciphertext = data[0, data.length - 0x20]
70
plaintext = Rex::Crypto.rc4(key, ciphertext)
71
if plaintext.is_utf8?
72
return plaintext
73
else
74
return nil
75
end
76
end
77
end
78
end
79
80
# Parse xsh session file provided as a string.
81
#
82
# @param input [String] XSH Session file as a string
83
# @return [Array] An array containing the version, host,
84
# port, username, and password obtained from the XSH session file.
85
def parser_xsh(input)
86
ini = Rex::Parser::Ini.from_s(input)
87
version = ini['SessionInfo']['Version']
88
port = ini['CONNECTION']['Port']
89
host = ini['CONNECTION']['Host']
90
username = ini['CONNECTION:AUTHENTICATION']['UserName']
91
password = ini['CONNECTION:AUTHENTICATION']['Password'] || nil
92
[version, host, port, username, password]
93
end
94
95
# parser xfp session file
96
#
97
# @param ini [String]
98
# @return [version, host, port, username, password]
99
def parser_xfp(file)
100
ini = Rex::Parser::Ini.from_s(file)
101
version = ini['SessionInfo']['Version']
102
port = ini['Connection']['Port']
103
host = ini['Connection']['Host']
104
username = ini['Connection']['UserName']
105
password = ini['Connection']['Password']
106
[version, host, port, username, password]
107
end
108
end
109
end
110
end
111
112