Skip to content

Instantly share code, notes, and snippets.

@ta1kt0me
Last active November 22, 2016 17:01
Show Gist options
  • Select an option

  • Save ta1kt0me/1c4e436ba614693e2c46ec397c6dfcd6 to your computer and use it in GitHub Desktop.

Select an option

Save ta1kt0me/1c4e436ba614693e2c46ec397c6dfcd6 to your computer and use it in GitHub Desktop.
encoded clone create encoded clone
# http://pam-ya.com/blog/archives/2006/04/post-238.html
$code = <<-EOS
require 'openssl'
require 'base64'
class Clone
def generate
file_name = 'sample_' + Time.now.strftime('%F-%H%M%S%L') + '.rb'
File.open(file_name, 'w') do |f|
header = '$code = <<-EOS'
footer = 'EOS'
f.puts 'require "openssl"'
f.puts 'require "base64"'
f.puts header
f.puts encrypt(header + "\n" + $code + footer + "\neval $code")
f.puts footer
f.puts "def decrypt(str)"
f.puts " decode = Base64.strict_decode64(str.chomp)"
f.puts " cipher = OpenSSL::Cipher.new('" + mode + "')"
f.puts " cipher.decrypt"
f.puts " key_iv = OpenSSL::PKCS5.pbkdf2_hmac_sha1('" + pass + "', '" + salt + "', " + iter.to_s + ", cipher.key_len + cipher.iv_len)"
f.puts " key = key_iv[0, cipher.key_len]"
f.puts " iv = key_iv[cipher.key_len, cipher.iv_len]"
f.puts " cipher.key = key"
f.puts " cipher.iv = iv"
f.puts " decrypted_data = ''"
f.puts " decrypted_data << cipher.update(decode)"
f.puts " decrypted_data << cipher.final"
f.puts " decrypted_data"
f.puts "end"
f.puts "eval decrypt($code)"
end
end
def salt
@_salt ||= ('A'..'z').to_a.sample(8).join
end
def pass
@_pass ||= ('A'..'z').to_a.sample(10).join
end
def iter
2000
end
def mode
'AES-256-CBC'
end
def encrypt(str)
cipher = OpenSSL::Cipher.new(mode)
cipher.encrypt
key_iv = OpenSSL::PKCS5.pbkdf2_hmac_sha1(pass, salt, iter, cipher.key_len + cipher.iv_len)
key = key_iv[0, cipher.key_len]
iv = key_iv[cipher.key_len, cipher.iv_len]
cipher.key = key
cipher.iv = iv
cipherrypted_data = ''
cipherrypted_data << cipher.update(str)
cipherrypted_data << cipher.final
Base64.strict_encode64(cipherrypted_data)
end
end
Clone.new.generate
EOS
eval $code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment