Class: Rex::Parser::NetSarang::NetSarangCrypto

Inherits:
Object
  • Object
show all
Defined in:
lib/rex/parser/netsarang.rb

Overview

Author:

  • Kali-Team

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, version, username, sid, master_password = nil) ⇒ Rex::Parser::NetSarang::NetSarangCrypto

This class implements encryption and decryption of NetSarang

Parameters:

  • type (String)

    only Xshell or Xftp.

  • version (String)

    Specify version of session file. e.g.:5.3

  • username (String)

    Specify username. This parameter will be used if version > 5.2.

  • sid (String)

    Specify SID. This parameter will be used if version >= 5.1.

  • master_password (Hash) (defaults to: nil)

    a customizable set of options

Options Hash (master_password):

  • Specify (String)

    user's master password.


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rex/parser/netsarang.rb', line 23

def initialize(type, version, username, sid, master_password = nil)
  self.version = version.to_f
  self.username = username
  self.sid = sid
  self.master_password = master_password
  md5 = OpenSSL::Digest::MD5.new
  sha256 = OpenSSL::Digest::SHA256.new
  if (self.version > 0) && (self.version < 5.1)
    self.key = (type == 'Xshell') ? md5.digest('[email protected]#h$e%l^l&') : md5.digest('[email protected]#c$e%l^l&')
  elsif (self.version >= 5.1) && (self.version <= 5.2)
    self.key = sha256.digest(self.sid)
  elsif (self.version > 5.2)
    if self.master_password.nil?
      self.key = sha256.digest(self.username + self.sid)
    else
      self.key = sha256.digest(self.master_password)
    end
  else
    raise 'Invalid argument: version'
  end
end

Instance Attribute Details

#keyObject

Returns the value of attribute key


12
13
14
# File 'lib/rex/parser/netsarang.rb', line 12

def key
  @key
end

#master_passwordObject

Returns the value of attribute master_password


11
12
13
# File 'lib/rex/parser/netsarang.rb', line 11

def master_password
  @master_password
end

#sidObject

Returns the value of attribute sid


10
11
12
# File 'lib/rex/parser/netsarang.rb', line 10

def sid
  @sid
end

#usernameObject

Returns the value of attribute username


9
10
11
# File 'lib/rex/parser/netsarang.rb', line 9

def username
  @username
end

#versionObject

Returns the value of attribute version


8
9
10
# File 'lib/rex/parser/netsarang.rb', line 8

def version
  @version
end

Instance Method Details

#decrypt_string(string) ⇒ String

Decrypt

Parameters:

  • string (String)

Returns:

  • (String)

    plaintext failed return nil


65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rex/parser/netsarang.rb', line 65

def decrypt_string(string)
  if (version < 5.1)
    return Rex::Crypto.rc4(key, Rex::Text.decode_base64(string))
  else
    data = Rex::Text.decode_base64(string)
    ciphertext = data[0, data.length - 0x20]
    plaintext = Rex::Crypto.rc4(key, ciphertext)
    if plaintext.is_utf8?
      return plaintext
    else
      return nil
    end
  end
end

#encrypt_string(string) ⇒ String

Encrypt

Parameters:

  • string (String)

Returns:

  • (String)

    ciphertext


49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rex/parser/netsarang.rb', line 49

def encrypt_string(string)
  cipher = Rex::Crypto.rc4(key, string)
  if (version < 5.1)
    return Rex::Text.encode_base64(cipher)
  else
    sha256 = OpenSSL::Digest::SHA256.new
    checksum = sha256.digest(string)
    ciphertext = cipher
    return Rex::Text.encode_base64(ciphertext + checksum)
  end
end