Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report CA, ADCS Template and Password along with Pkcs12 in the database #19736

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

cdelafuente-r7
Copy link
Contributor

@cdelafuente-r7 cdelafuente-r7 commented Dec 16, 2024

This adds support to the new Pkcs12 data format added in rapid7/metasploit-credential#183. Now, the CA and ADCS template can be added to the Pkcs12 as metadata in the database.

Also, it is now possible to store a Pkcs12 password as a metadata in the database. If the Pkcs12 is encrypted, the password can (and must) be added to the metadata field. It will be used to decrypt the Pkcs12. The creds command has been updated to accept a new option pkcs12-password. The validation will fail if the Pkcs12 we want to add with creds is encrypted and the password is wrong or empty.

This PR needs the metasploit-credentials counterpart be landed first. I have updated the Gemfile to point to the feature branch to be able to test it. This will need to be reverted before landing.

Verification

Testing auxiliary/admin/dcerpc/icpr_cert

Follow the instructions here to set up an AD CS server for testing purposes.

  • Start msfconsole
  • use auxiliary/admin/dcerpc/icpr_cert
  • run verbose=true CA=<CA name> RHOSTS=<remote host> username=<username> password=<user password> CERT_TEMPLATE=User
  • Verify creds returns the generated Pkcs12
  • Check with irb in msfconsole if the Pkcs12 model contains the metadata filed with the expected values.
msf6 auxiliary(admin/dcerpc/icpr_cert) > run verbose=true CA=myca-CA RHOSTS=10.100.54.12 username=muser password=vagrant CERT_TEMPLATE=User
[*] Running module against 10.100.54.12
[*] 10.100.54.12:445 - Connecting to ICertPassage (ICPR) Remote Protocol
[*] 10.100.54.12:445 - Binding to \cert...
[+] 10.100.54.12:445 - Bound to \cert
[*] 10.100.54.12:445 - Requesting a certificate for user muser - digest algorithm: SHA256 - template: User
[+] 10.100.54.12:445 - The requested certificate was issued.
[*] 10.100.54.12:445 - Certificate UPN: [email protected]
[*] 10.100.54.12:445 - Certificate stored at: /home/n00tmeg/.msf4/loot/20241216151952_default_10.100.54.12_windows.ad.cs_020208.pfx
[*] Auxiliary module execution completed
msf6 auxiliary(admin/dcerpc/icpr_cert) > creds
Credentials
===========

host          origin        service        public              private                                                                                   realm  private_type  JtR Format  cracked_password
----          ------        -------        ------              -------                                                                                   -----  ------------  ----------  ----------------
10.100.54.12  10.100.54.12  445/tcp (smb)  [email protected]  subject:/DC=local/DC=pro/DC=ad/OU=UK/OU=Support/CN=muser,issuer:/DC=local/DC (TRUNCATED)         Pkcs12 (pfx)

msf6 auxiliary(admin/dcerpc/icpr_cert) > irb
[*] Starting IRB shell...
[*] You are in auxiliary/admin/dcerpc/icpr_cert

>> Metasploit::Credential::Pkcs12.all
=>
[#<Metasploit::Credential::Pkcs12:0x00007f3f4d255dd0
  id: 1,
  type: "Metasploit::Credential::Pkcs12",
  data: "<REDACTED>",
  created_at: 2025-02-06 13:25:11.731165 UTC,
  updated_at: 2025-02-06 13:25:11.731165 UTC,
  jtr_format: nil,
  metadata: {"ca"=>"myca-CA", "adcs_template"=>"User"}>]

Testing creds command

  • Start msfconsole
  • Add a certificate manually with creds add user:testuser pkcs12:<pkcs12 filepath> ca:myca adcs-template:OtherTemplate
  • Verify creds returns the generated Pkcs12
  • Check with irb in msfconsole if the Pkcs12 model contains the metadata filed with the expected values.

Testing creds command with an encrypted Pkcs12 and a password

First we need to get an password protected Pkcs12. We can use openssl command with an already retrieved pkcs12, with the auxiliary/admin/dcerpc/icpr_cert module for example, and set a password. Check the certificate files with the loot command to get the filepath.

  1. Extract client certificate:
openssl pkcs12 -in /home/n00tmeg/.msf4/loot/20250206142511_default_10.232.45.111_windows.ad.cs_641319.pfx -out existingpkcs12_clcert.pem -nokeys -clcerts

Hit Enter when asked for the Import Password (there is no password).
2. Extract client certificate's private key:

openssl pkcs12 -in /home/n00tmeg/.msf4/loot/20250206142511_default_10.232.45.111_windows.ad.cs_641319.pfx -out existingpkcs12_key.pem -nocerts -des3

Hit Enter when asked for the Import Password (there is no password).
Enter a password for the private key export (e.g. password)
3. Re-create the PKCS#12

openssl pkcs12 -export -in existingpkcs12_clcert.pem -inkey existingpkcs12_key.pem -keypbe PBE-SHA1-3DES -certpbe PBE-SHA1-3DES -out newpkcs12.p12

Enter the previous password set when asked for the pass phrase for existingpkcs12_key.pem (e.g. password)
Enter Export Password: 123456

  • Start msfconsole
  • Add a certificate manually with creds add user:testuser pkcs12:newpkcs12.p12 ca:myca adcs-template:OtherTemplate pkcs12-password:123456
  • Verify creds returns the generated Pkcs12
  • Check with irb in msfconsole if the Pkcs12 model contains the metadata filed with the expected values.
  • Try to add it again with a wrong password: creds add user:testuser pkcs12:newpkcs12.p12 ca:myca adcs-template:OtherTemplate pkcs12-password:wrongpasswd
  • Verify you get a PKCS12 Data ArgumentError

@cdelafuente-r7 cdelafuente-r7 added the blocked Blocked by one or more additional tasks label Dec 16, 2024
@@ -414,11 +420,13 @@ def creds_search(*args)
when 'password'
Metasploit::Credential::Password
when 'hash'
Metasploit::Credential::PasswordHash
Metasploit::Credential::NonreplayableHash
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently hash corresponds to a Nonreplayable hash instead of a PasswordHash. This change were required otherwise the specs were failing.

# realm: nil,
# workspace: framework.db.workspace)
# end
let!(:ntlm_core) do
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests are now working properly. I've added them back even if it is not related to the changes in this PR.

Comment on lines +6 to +7
gem 'metasploit-credential', git: 'https://github.com/cdelafuente-r7/metasploit-credential', branch: 'enh/MS-9710/add_pkcs12_metadata'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will need to be reverted before landing.

Gemfile.lock Outdated
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file will need to be updated to point to the new metasploit-credential gem is available instead of the this feature branch.

…base

- Update the `creds` command to add Pkcs12 private credentials with
  metadata.
- Update `ms_icpr` module to store metadata.
@cdelafuente-r7 cdelafuente-r7 force-pushed the enh/pkcs12/add_metadata branch from c44cad3 to 3581724 Compare February 6, 2025 13:00
@cdelafuente-r7 cdelafuente-r7 changed the title Report CA and ADCS Template along with Pkcs12 in the database Report CA, ADCS Template and Password along with Pkcs12 in the database Feb 6, 2025
@cdelafuente-r7 cdelafuente-r7 force-pushed the enh/pkcs12/add_metadata branch 4 times, most recently from fddd218 to 91ff4ce Compare February 6, 2025 18:28
…a model

- a separate field is now used for metadata (`private_metadata`) when
  creating a new Pkcs12
- the `creds` command now support adding an encrypted Pkcs12 with a password
@cdelafuente-r7 cdelafuente-r7 force-pushed the enh/pkcs12/add_metadata branch from 91ff4ce to 7df6dbc Compare February 10, 2025 21:20
@cdelafuente-r7 cdelafuente-r7 marked this pull request as ready for review February 10, 2025 23:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked Blocked by one or more additional tasks enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant