Created
November 29, 2018 05:50
-
-
Save pich4ya/7e0d66c926b0cc7bf973962aa19a512c to your computer and use it in GitHub Desktop.
Ruby on Rails 5.1.6 Session Decryptor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# @author longcat (Pichaya Morimoto) | |
# Tested on ruby 2.5.3 on rails 5.1.6 | |
# modded from https://gist.github.com/erose/36a514bc5ac9c5f18552369265b4d449 | |
# $ gem install application_config | |
# $ gem install activesupport | |
require 'cgi' | |
require 'json' | |
require 'active_support' | |
require 'application_config' | |
# $ rails runner 'Rails.application.config.action_dispatch.tap { |c| p encrypted_cookie_salt: c.encrypted_cookie_salt, encrypted_signed_cookie_salt: c.encrypted_signed_cookie_salt }' | |
# {:encrypted_cookie_salt=>"encrypted cookie", :encrypted_signed_cookie_salt=>"signed encrypted cookie"} | |
# $ rails runner 'p Rails.application.secrets.secret_key_base' | |
# "2f1d90a26236c3245d96f5606c201a780dc9ca687e5ed82b45e211bb5dc84c1870f61ca9e002dad5dd8a149c9792d8f07f31a9575065cca064bd6af44f8750e4" | |
def verify_and_decrypt_session_cookie(cookie, secret_key_base) | |
cookie = CGI::unescape(cookie) | |
# salt = ApplicationConfig.get.action_dispatch.encrypted_cookie_salt | |
# signed_salt = ApplicationConfig.get.action_dispatch.encrypted_signed_cookie_salt | |
salt = "encrypted cookie" | |
signed_salt = "signed encrypted cookie" | |
key_generator = ActiveSupport::KeyGenerator.new(secret_key_base, iterations: 1000) | |
secret = key_generator.generate_key(salt)[0, 32] | |
sign_secret = key_generator.generate_key(signed_salt) | |
# encryptor = ActiveSupport::MessageEncryptor.new(secret, sign_secret, serializer: JSON) | |
encryptor = ActiveSupport::MessageEncryptor.new(secret, sign_secret, serializer: Marshal) | |
encryptor.decrypt_and_verify(cookie) | |
end | |
puts verify_and_decrypt_session_cookie('K2x3aUNMc2h6Zk1McjRQKzMwZ0QwZnd0Y2JyazJqcDByWUhHL2JTb1hrSzRCWTYzV0piRjJ5YmYwWDQyQlEwWnVuNFBMSEpTang2dDdSVVJNZzJRZG5CczBtYXRUdXB1N3pOaVhQMk9SMUxYbE1kamFVVUpOdmZNNkR3M0p4VThoU3RuQit0MkdINlNpVkdPQk55NUh2R3NsbTFqT0NSSm03cDdCWFFEZHdxWklnd1B6MmxLenJFV1N6ZXplMHhocVhNRm1rUGRJQlpnclhSUWtzOTNUUT09LS1tSHROSmxsd3B6Nk1aMVpKb09ZcVdRPT0=--d468d168e3220510b0e21f16f67357c19c15677e','2f1d90a26236c3245d96f5606c201a780dc9ca687e5ed82b45e211bb5dc84c1870f61ca9e002dad5dd8a149c9792d8f07f31a9575065cca064bd6af44f8750e4') | |
# $ ruby rails_session_decryptor-5.rb | |
# {"session_id"=>"ebed7e1a3f559b8fc5d7df8ac71a782e", "_csrf_token"=>"OJpeV8IQToGcTf5KBTGqemrqSzeqTEyeUNQY5oFwWCk=", "user_id"=>8} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment