c7decrypt 0.1.10 → 0.1.13

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/c7decrypt.rb +1 -181
  2. metadata +6 -4
data/lib/c7decrypt.rb CHANGED
@@ -1,181 +1 @@
1
- require_relative 'exceptions'
2
-
3
- class C7Decrypt
4
-
5
- # Vigenere translation table (these are our key values for decryption)
6
- VT_TABLE = [
7
- 0x64, 0x73, 0x66, 0x64, 0x3b, 0x6b, 0x66, 0x6f, 0x41, 0x2c, 0x2e,
8
- 0x69, 0x79, 0x65, 0x77, 0x72, 0x6b, 0x6c, 0x64, 0x4a, 0x4b, 0x44,
9
- 0x48, 0x53, 0x55, 0x42, 0x73, 0x67, 0x76, 0x63, 0x61, 0x36, 0x39,
10
- 0x38, 0x33, 0x34, 0x6e, 0x63, 0x78, 0x76, 0x39, 0x38, 0x37, 0x33,
11
- 0x32, 0x35, 0x34, 0x6b, 0x3b, 0x66, 0x67, 0x38, 0x37
12
- ]
13
-
14
- # Regexes for extracting hashes from configs
15
- TYPE_7_REGEXES = [
16
- /enable password 7 ([A-Z0-9]+)/,
17
- /username [A-Z0-9]+ password 7 ([A-Z0-9]+)/,
18
- /password 7 ([A-Z0-9]+)/
19
- ]
20
-
21
- # The Decryption Method for Cisco Type-7 Encrypted Strings
22
- # @param [String] the Cisco Type-7 Encrypted String
23
- # @raise [InvalidFirstCharacter,
24
- # InvalidCharacter,
25
- # OddNumberOfCharacters]
26
- # @return [String] the Decrypted String
27
- def decrypt(e_text)
28
- check_type_7_errors(e_text)
29
-
30
- d_text = ""
31
- seed = nil
32
-
33
- e_text.scan(/../).each_with_index do |char,i|
34
- if i == 0
35
- seed = char.to_i - 1
36
- else
37
- d_text += decrypt_char(char, i, seed)
38
- end
39
- end
40
-
41
- return d_text
42
- end
43
-
44
- # The Encryption Method for Cisco Type-7 Encrypted Strings
45
- # @param [String] the plaintext password
46
- # @param [String] the seed for the encryption used
47
- # @raise [InvalidEncryptionSeed,
48
- # InvalidFirstCharacter,
49
- # InvalidCharacter,
50
- # OddNumberOfCharacters]
51
- # @return [String] the encrypted password
52
- def encrypt(d_text, seed = 2)
53
- check_seed(seed)
54
-
55
- e_text = sprintf("%02d", seed)
56
-
57
- d_text.each_char.each_with_index do |d_char,i|
58
- e_text += encrypt_char(d_char, i, seed)
59
- end
60
-
61
- check_type_7_errors(e_text)
62
-
63
- return e_text
64
- end
65
-
66
- # The method for encrypting a single character
67
- # @param [String] the plain text char
68
- # @param [FixNum] the index of the char in plaintext string
69
- # @param [FixNum] the seed used in the encryption process
70
- # @return [String] the string of the encrypted char
71
- def encrypt_char(char, i, seed)
72
- sprintf("%02X", char.unpack('C')[0] ^ VT_TABLE[(i + seed) % 53])
73
- end
74
-
75
- # The method for decrypting a single character
76
- # @param [String] the encrypted char
77
- # @param [Integer] the index of the char pair in encrypted string
78
- # @param [Integer] the seed used in the decryption process
79
- # @return [String] the string of the decrypted char
80
- def decrypt_char(char, i, seed)
81
- (char.hex^VT_TABLE[(i + seed) % 53]).chr
82
- end
83
-
84
- # A helper method to decrypt an arracy of Cisco Type-7 Encrypted Strings
85
- # @param [Array>String] an array of Cisco Type-7 Encrypted Strings
86
- # @raise [InvalidFirstCharacter,
87
- # InvalidCharacter,
88
- # OddNumberOfCharacters]
89
- # @return [Array>String] an array of Decrypted Strings
90
- def decrypt_array(pw_array)
91
- pw_array.collect {|pw| decrypt(pw)}
92
- end
93
-
94
- # A helper method to encrypt an arracy of passwords
95
- # @param [Array>String] an array of plain-text passwords
96
- # @raise [InvalidEncryptionSeed,
97
- # InvalidFirstCharacter,
98
- # InvalidCharacter,
99
- # OddNumberOfCharacters]
100
- # @return [Array>String] an array of encrypted passwords
101
- def encrypt_array(pt_array, seed = 2)
102
- pt_array.collect {|pw| encrypt(pw, seed)}
103
- end
104
-
105
- # This method scans a raw config file for type 7 passwords and
106
- # decrypts them
107
- # @param [String] a string of the config file path that contains
108
- # Cisco Type-7 Encrypted Strings
109
- # @raise [InvalidFirstCharacter,
110
- # InvalidCharacter,
111
- # OddNumberOfCharacters]
112
- # @return [Array>String] an array of Decrypted Strings
113
- def decrypt_config(file)
114
- f = File.open(file, 'r').to_a
115
- decrypt_array(f.collect {|line| type_7_matches(line)}.flatten)
116
- end
117
-
118
- # This method scans a config line for encrypted type-7 passwords and
119
- # returns an array of results
120
- # @param [String] a line with potential encrypted type-7 passwords
121
- # @return [Array>String] an array of Cisco type-7 encrypted Strings
122
- def type_7_matches(string)
123
- TYPE_7_REGEXES.collect {|regex| string.scan(regex)}.flatten.uniq
124
- end
125
-
126
- # This method determines if an encrypted hash is corrupted/invalid
127
- # and throw a specific exeception
128
- # @param [String] the Cisco Type-7 Encrypted String
129
- # @raise [InvalidFirstCharacter, InvalidCharacter, OddNumberOfCharacters]
130
- # @return [Nil]
131
- def check_type_7_errors(e_text)
132
-
133
- valid_first_chars = (0..15).to_a.collect {|c| sprintf("%02d", c)}
134
- first_char = e_text[0,2]
135
-
136
- # Check for an invalid first character in the has
137
- unless valid_first_chars.include? first_char
138
- raise InvalidFirstCharacter,
139
- "'#{e_text}' hash contains an invalid first chracter (only '00' - '15' allowed)"
140
- end
141
-
142
- # Check for an invalid character in the hash
143
- unless e_text.match(/^[A-Z0-9]+$/)
144
- raise InvalidCharacter,
145
- "'#{e_text}' hash contains an invalid character (only upper-alpha numeric allowed)"
146
- end
147
-
148
- # Check for an odd number of characters in the hash
149
- unless e_text.size % 2 == 0
150
- raise OddNumberOfCharacters,
151
- "'#{e_text}' hash contains odd length of chars (only even number of chars allowed)"
152
- end
153
-
154
- return nil
155
-
156
- end
157
-
158
- # This method determines if an encryption seed is valid or not
159
- # and throw a specific exeception
160
- # @param [FixNum] the seed used in the encryption process
161
- # @raise [InvalidEncryptionSeed]
162
- # @return [Nil]
163
- def check_seed(seed)
164
- if seed < 0 ||
165
- seed > 15
166
-
167
- raise InvalidEncryptionSeed,
168
- "'#{seed.to_s}' seed is not a valid seed (only 0 - 15 allowed)"
169
- end
170
-
171
- return nil
172
- end
173
-
174
- #Definition of short-hand methods for the lazy
175
- alias :d :decrypt
176
- alias :e :encrypt
177
- alias :d_a :decrypt_array
178
- alias :e_a :encrypt_array
179
- alias :d_c :decrypt_config
180
-
181
- end
1
+ require 'c7decrypt/c7decrypt'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: c7decrypt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.10
4
+ version: 0.1.13
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-22 00:00:00.000000000 Z
12
+ date: 2013-05-05 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A library for decoding Cisco Type 7 passwords
15
15
  email: claudijd@yahoo.com
@@ -30,6 +30,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
30
30
  - - ! '>='
31
31
  - !ruby/object:Gem::Version
32
32
  version: '0'
33
+ segments:
34
+ - 0
35
+ hash: 3872791078896625045
33
36
  required_rubygems_version: !ruby/object:Gem::Requirement
34
37
  none: false
35
38
  requirements:
@@ -38,9 +41,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
38
41
  version: '0'
39
42
  requirements: []
40
43
  rubyforge_project:
41
- rubygems_version: 1.8.23
44
+ rubygems_version: 1.8.25
42
45
  signing_key:
43
46
  specification_version: 3
44
47
  summary: Ruby based Cisco Type 7 Password Decryptor
45
48
  test_files: []
46
- has_rdoc: