Created
August 3, 2013 18:42
-
-
Save chalfant/6147530 to your computer and use it in GitHub Desktop.
Ruby script to get and decrypt a Windows instance's Administrator password.
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
#!/usr/bin/env ruby | |
require 'optparse' | |
require 'ostruct' | |
require 'aws-sdk' | |
require 'base64' | |
require 'openssl' | |
class Passworder | |
def initialize(args) | |
parse_opts(args) | |
end | |
def parse_opts(args) | |
options = OpenStruct.new | |
options.private_key = "#{ENV['HOME']}/.ssh/id_rsa" | |
opts = OptionParser.new do |o| | |
o.banner = "Usage: get_windows_password.rb [options] INSTANCE_ID" | |
o.separator "" | |
o.separator "options:" | |
o.on("-p", "--private-key PRIVATEKEY", "private key used to start instance") do |pk| | |
options.private_key = pk | |
end | |
o.on_tail("-h", "--help", "show this message") do | |
puts o | |
exit | |
end | |
end | |
opts.parse! args | |
@options = options | |
@instance_id = args[0] | |
end | |
def run | |
ec2 = AWS::EC2.new | |
response = ec2.client.get_password_data(instance_id: @instance_id) | |
if response[:password_data].nil? | |
$stderr.puts "password not yet available for #{@instance_id}" | |
exit 1 | |
end | |
password_data = Base64.decode64 response[:password_data] | |
key = OpenSSL::PKey::RSA.new File.read(@options.private_key) | |
password = key.private_decrypt password_data | |
puts password | |
end | |
end | |
Passworder.new(ARGV).run | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just used this today and it worked great. Thanks for sharing.