Skip to content

Instantly share code, notes, and snippets.

@crmaxx
Last active August 29, 2015 14:20
Show Gist options
  • Save crmaxx/5f5d30a18ac6cec9b735 to your computer and use it in GitHub Desktop.
Save crmaxx/5f5d30a18ac6cec9b735 to your computer and use it in GitHub Desktop.
Ruby version of password crypt for SoftPerfect Network Scanner (port from C)
#!/usr/bin/env ruby
# c-prog
# $ ./ns32 {43fa330c-4ec6-4440-a924-bd21b406b0ea} Test654321
# WDOfQ7VONESfJIghgAaD6j4zy0M=
#
# ruby-prog
# => "WDOfQ7VONESfJIghgAaD6j4zy0M="
require 'base64'
require 'securerandom'
pass = "Test654321"
raw_key = SecureRandom.random_bytes(16)
ary = raw_key.unpack("NnnnnN")
ary[2] = (ary[2] & 0x0fff) | 0x4000
ary[3] = (ary[3] & 0x3fff) | 0x8000
uuid = "{%08x-%04x-%04x-%04x-%04x%08x}" % ary
key = ary.pack("NnnnnN")
reordered_key = [3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11, 12, 13, 14, 15].map { |idx| key.chars[idx] }
pass_bytes = pass.chars.map { |char| [char.unpack('c'), 0] }.flatten
key_bytes = reordered_key.map { |char| char.unpack('c') }.flatten
crypt = pass_bytes.map.with_index { |byte, idx| byte ^ key_bytes[idx % key_bytes.size] }
puts [format("{%08x-%04x-%04x-%04x-%04x%08x}", *ary), Base64.encode64(crypt.pack('c*')).strip].inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment