-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathplugin.rb
374 lines (304 loc) · 11.3 KB
/
plugin.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# name: discourse-migratepassword
# about: enable alternative password hashes
# version: 0.11.4
# authors: Communiteq
# url: https://github.com/discoursehosting/discourse-migratepassword
# Usage:
# When migrating, store a custom field with the user containing the crypted password
# for vBulletin this should be #{password}:#{salt} md5(md5(pass) + salt)
# for vBulletin5 #{token} bcrypt(md5(pass))
# for Phorum #{password} md5(pass)
# for Wordpress #{password} phpass(8).crypt(pass)
# for SMF #{username}:#{password} sha1(user+pass)
# for IPB #{salt}:#{hash} md5(md5(salt)+md5(pass))
# for WBBlite #{salt}:#{hash} sha1(salt+sha1(salt+sha1(pass)))
# for Joomla #{hash}:#{salt} md5(pass+salt)
# for Joomla 3.2 #{password} bcrypt(pass)
# for Question2Answer #{salt}:#{passcheck} sha1 (left(salt,8) + pass + right(salt,8))
# for Drupal 7 #{password} sha512(sha512(salt + pass) + pass) x iterations from salt.
#This will be applied at runtime, as authentication is attempted. It does not apply at migration time.
gem 'bcrypt', '3.1.13'
gem 'unix-crypt', '1.3.0', :require_name => 'unix_crypt'
gem 'ffi-compiler', '1.0.1', require: false
gem 'argon2', '2.2.0'
enabled_site_setting :migratepassword_enabled
require 'digest'
require 'openssl'
require "base64"
class WordpressHash
def initialize(stretch=8)
@itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
stretch = 8 unless (8..30).include?(stretch)
@stretch = stretch
@random_state = '%s%s' % [Time.now.to_f, $$]
end
def hash(pw)
rnd = ''
rnd = Phpass.random_bytes(6)
crypt(pw, gensalt(rnd))
end
def check(pw, hash)
hash.gsub! /^\$H\$/, '$P$'
return false unless hash.start_with?('$P$')
crypted = crypt(pw,hash)
crypted == hash
end
private
def gensalt(input)
out = '$P$'
out << @itoa64[[@stretch + 5, 30].min]
out << encode64(input, 6)
out
end
def crypt(pw, setting)
out = '*0'
out = '*1' if setting.start_with?(out)
iter = @itoa64.index(setting[3])
return out unless (8..30).include?(iter)
count = 1 << iter
salt = setting[4...12]
return out if salt.length != 8
hash = Digest::MD5.digest(salt + pw)
while count > 0
hash = Digest::MD5.digest(hash + pw)
count -= 1
end
setting[0,12] + encode64(hash, 16)
end
def encode64(input, count)
out = ''
cur = 0
while cur < count
value = input[cur].ord
cur += 1
out << @itoa64[value & 0x3f]
if cur < count
value |= input[cur].ord << 8
end
out << @itoa64[(value >> 6) & 0x3f]
break if cur >= count
cur += 1
if cur < count
value |= input[cur].ord << 16
end
out << @itoa64[(value >> 12) & 0x3f]
break if cur >= count
cur += 1
out << @itoa64[(value >> 18) & 0x3f]
end
out
end
end
class DrupalSHA512Hash
def initialize()
@drupal_min_hash_count = 7
@drupal_max_hash_count = 30
@drupal_hash_length = 55
@itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
@hash = Digest::SHA2.new(512)
end
def check(password, crypted_pass)
return false if password.nil? or crypted_pass.nil?
# passwords migrated from drupal 6 start with a 'U' and crypt the MD5-hashed password
if crypted_pass[0] == 'U'
crypt(Digest::MD5.hexdigest(password), crypted_pass[1..12]) == crypted_pass[1..-1]
else
# normal drupal 7 passwords
crypt(password, crypted_pass[0..11]) == crypted_pass
end
end
def crypt(password, setting)
if setting[0] != '$' or setting[1] != 'S' or setting[2] != '$'
# Wrong hash format
return false
end
count_log2 = @itoa64.index(setting[3])
if count_log2 < @drupal_min_hash_count or count_log2 > @drupal_max_hash_count
return false
end
salt = setting[4..4 + 7]
if salt.length != 8
return false
end
iterations = 2 ** count_log2
pass_hash = @hash.digest(salt + password)
1.upto(iterations) do |i|
pass_hash = @hash.digest(pass_hash.force_encoding(Encoding::UTF_8) + password)
end
hash_length = pass_hash.length
output = setting + encode64(pass_hash, hash_length)
if output.length != 98
return false
end
return output[0..(@drupal_hash_length - 1)]
end
def encode64(input, count)
output = ''
i = 0
while true
value = (input[i]).ord
i += 1
output = output + @itoa64[value & 0x3f]
if i < count
value |= (input[i].ord) << 8
end
output = output + @itoa64[(value >> 6) & 0x3f]
if i >= count
break
end
i += 1
if i < count
value |= (input[i].ord) << 16
end
output = output + @itoa64[(value >> 12) & 0x3f]
if i >= count
break
end
i += 1
output = output + @itoa64[(value >> 18) & 0x3f]
if i >= count
break
end
end
return output
end
end
after_initialize do
module ::AlternativePassword
def confirm_password?(password)
return true if super
return false unless SiteSetting.migratepassword_enabled
return false unless self.custom_fields.has_key?('import_pass')
if AlternativePassword::check_all(password, self.custom_fields['import_pass'])
self.password = password
self.custom_fields.delete('import_pass')
if SiteSetting.migratepassword_allow_insecure_passwords
return save(validate: false)
else
return save
end
end
false
end
def self.check_all(password, crypted_pass)
AlternativePassword::check_vbulletin(password, crypted_pass) ||
AlternativePassword::check_vbulletin5(password, crypted_pass) ||
AlternativePassword::check_ipb(password, crypted_pass) ||
AlternativePassword::check_smf(password, crypted_pass) ||
AlternativePassword::check_md5(password, crypted_pass) ||
AlternativePassword::check_sha1(password, crypted_pass) ||
AlternativePassword::check_bcrypt(password, crypted_pass) ||
AlternativePassword::check_sha256(password, crypted_pass) ||
AlternativePassword::check_wordpress(password, crypted_pass) ||
AlternativePassword::check_wbblite(password, crypted_pass) ||
AlternativePassword::check_unixcrypt(password, crypted_pass) ||
AlternativePassword::check_joomla_md5(password, crypted_pass) ||
AlternativePassword::check_joomla_3_2(password, crypted_pass) ||
AlternativePassword::check_q2a(password, crypted_pass) ||
AlternativePassword::check_drupal7(password, crypted_pass) ||
AlternativePassword::check_devise(password, crypted_pass) ||
AlternativePassword::check_argon(password, crypted_pass)
end
def self.check_argon(password, crypted_pass)
begin
return false unless crypted_pass[0..9] == '$argon2id$'
return Argon2::Password.verify_password(password, crypted_pass)
rescue
false
end
end
def self.check_devise(password, crypted_pass)
begin
bcrypt = BCrypt::Password.new(crypted_pass)
BCrypt::Engine.hash_secret(password, bcrypt.salt) == crypted_pass
rescue
false
end
end
def self.check_bcrypt(password, crypted_pass)
begin
# allow salt:hash as well as hash
BCrypt::Password.new(crypted_pass.rpartition(':').last) == password
rescue
false
end
end
def self.check_vbulletin(password, crypted_pass)
hash, salt = crypted_pass.split(':', 2)
!salt.nil? && hash == Digest::MD5.hexdigest(Digest::MD5.hexdigest(password) + salt)
end
def self.check_vbulletin5(password, crypted_pass)
# replace $2y$ with $2a$ see http://stackoverflow.com/a/20981781
crypted_pass.gsub! /^\$2y\$/, '$2a$'
begin
BCrypt::Password.new(crypted_pass) == Digest::MD5.hexdigest(password)
rescue
false
end
end
def self.check_md5(password, crypted_pass)
crypted_pass == Digest::MD5.hexdigest(password)
end
def self.check_sha1(password, crypted_pass)
crypted_pass == Digest::SHA1.hexdigest(password)
end
def self.check_smf(password, crypted_pass)
user, hash = crypted_pass.split(':', 2)
sha1 = Digest::SHA1.new
sha1.update user.downcase + password
hash == sha1.hexdigest
end
def self.check_ipb(password, crypted_pass)
# we can't use split since the salts may contain a colon
salt = crypted_pass.rpartition(':').first
hash = crypted_pass.rpartition(':').last
!salt.nil? && hash == Digest::MD5.hexdigest(Digest::MD5.hexdigest(salt) + Digest::MD5.hexdigest(password))
end
def self.check_wordpress(password, crypted_pass)
hasher = WordpressHash.new(8)
hasher.check(password, crypted_pass.rpartition(':').last)
end
def self.check_sha256(password, crypted_pass)
sha256 = Digest::SHA256.new
sha256.update password
crypted_pass == sha256.hexdigest
end
def self.check_wbblite(password, crypted_pass)
salt, hash = crypted_pass.split(':', 2)
sha1 = Digest::SHA1.hexdigest(salt + Digest::SHA1.hexdigest(salt + Digest::SHA1.hexdigest(password)))
hash == sha1
end
def self.check_unixcrypt(password, crypted_pass)
UnixCrypt.valid?(password, crypted_pass)
end
def self.check_joomla_md5(password, crypted_pass)
hash, salt = crypted_pass.split(':', 2)
!salt.nil? && hash == Digest::MD5.hexdigest(password + salt)
end
def self.check_q2a(password, crypted_pass)
salt, hash = crypted_pass.split(':', 2)
salt_prefix = salt[0..7] || ""
salt_postfix = salt[-8..-1] || ""
sha1 = Digest::SHA1.hexdigest(salt_prefix + password + salt_postfix)
hash == sha1
end
def self.check_joomla_3_2(password, crypted_pass)
crypted_pass.gsub! /^\$2y\$/, '$2a$'
begin
BCrypt::Password.new(crypted_pass) == password
rescue
false
end
end
def self.check_drupal7(password, crypted_pass)
begin
DrupalSHA512Hash.new.check(password, crypted_pass)
rescue
false
end
end
end
class ::User
prepend AlternativePassword
end
end