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

Inherits:
Object
  • Object
show all
Defined in:
lib/rex/parser/net_sarang.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.



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

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.new('MD5')
  sha256 = OpenSSL::Digest.new('SHA256')
  if (self.version > 0) && (self.version < 5.1)
    self.key = (type == 'Xshell') ? md5.digest('!X@s#h$e%l^l&') : md5.digest('!X@s#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.



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

def key
  @key
end

#master_passwordObject

Returns the value of attribute master_password.



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

def master_password
  @master_password
end

#sidObject

Returns the value of attribute sid.



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

def sid
  @sid
end

#usernameObject

Returns the value of attribute username.



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

def username
  @username
end

#versionObject

Returns the value of attribute version.



7
8
9
# File 'lib/rex/parser/net_sarang.rb', line 7

def version
  @version
end

Instance Method Details

#decrypt_string(string) ⇒ String

Decrypt

Parameters:

  • string (String)

Returns:

  • (String)

    plaintext failed return nil



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

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



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

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