1+ import sys
2+ import subprocess
3+ import importlib .util
4+ import os
5+
6+ def ensure_package (package ):
7+ if importlib .util .find_spec (package ) is None :
8+ print (f"Installing required package: { package } ..." )
9+ subprocess .check_call ([sys .executable , "-m" , "pip" , "install" , package ])
10+ print ("Restarting script ..." )
11+ os .execv (sys .executable , [sys .executable ] + sys .argv )
12+
13+ ensure_package ("cryptography" )
14+
15+ import datetime
16+ from cryptography import x509
17+ from cryptography .hazmat .primitives .serialization import pkcs12
18+ from cryptography .hazmat .primitives import serialization
19+
20+ RED = "\033 [31m"
21+ BLUE = "\033 [34m"
22+ BLACK = "\033 [30m"
23+ RESET = "\033 [0m"
24+
25+ ASCII_ART = (
26+ "██████╗ ███████╗██╗ ██╗ ██████╗██╗ ██╗███████╗ ██████╗██╗ ██╗\n "
27+ "██╔══██╗██╔════╝╚██╗██╔╝██╔════╝██║ ██║██╔════╝██╔════╝██║ ██╔╝\n "
28+ "██████╔╝█████╗ ╚███╔╝ ██║ ███████║█████╗ ██║ █████╔╝ \n "
29+ "██╔═══╝ ██╔══╝ ██╔██╗ ██║ ██╔══██║██╔══╝ ██║ ██╔═██╗ \n "
30+ "██║ ██║ ██╔╝ ██╗╚██████╗██║ ██║███████╗╚██████╗██║ ██╗\n "
31+ "╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝╚═╝ ╚═╝╚══════╝ ╚═════╝╚═╝ ╚═╝\n "
32+ " "
33+ )
34+
35+ TOOL_NAME = "P F X C E R T I F I C A T E A N A L Y Z E R"
36+ VERSION = "Version 1.00"
37+ AUTHOR = "By Joshua M Clatney - Ethical Pentesting Enthusiast"
38+
39+ def clear ():
40+ os .system ("cls" if os .name == "nt" else "clear" )
41+
42+ def banner ():
43+ clear ()
44+ print (f"{ RED } { ASCII_ART } { RESET } " )
45+ print (f"{ BLUE } { TOOL_NAME } { RESET } { RED } { VERSION } { RESET } " )
46+ print (f"{ BLACK } { AUTHOR } { RESET } " )
47+
48+ def load_pfx (path , password ):
49+ with open (path , "rb" ) as f :
50+ data = f .read ()
51+ key , cert , _ = pkcs12 .load_key_and_certificates (data , password .encode ())
52+ return key , cert
53+
54+ def print_cert_info (label , value ):
55+ print (f"{ label } : { value } " )
56+
57+ def analyze_certificate (cert ):
58+ print_cert_info ("Subject" , cert .subject )
59+ print_cert_info ("Issuer" , cert .issuer )
60+ print_cert_info ("Serial Number" , cert .serial_number )
61+ print_cert_info ("Valid From" , cert .not_valid_before )
62+ print_cert_info ("Valid To" , cert .not_valid_after )
63+ print_cert_info ("Version" , cert .version )
64+ print_cert_info ("Public Key Algorithm" , cert .signature_algorithm_oid ._name )
65+ print ("\n Extensions:" )
66+ for ext in cert .extensions :
67+ print (f"- { ext .oid ._name } : { ext .value } " )
68+
69+ def extend_certificate_expiry (cert , key , days = 365 ):
70+ new_expiry = cert .not_valid_after + datetime .timedelta (days = days )
71+ builder = (
72+ x509 .CertificateBuilder ()
73+ .subject_name (cert .subject )
74+ .issuer_name (cert .issuer )
75+ .public_key (cert .public_key ())
76+ .serial_number (cert .serial_number )
77+ .not_valid_before (cert .not_valid_before )
78+ .not_valid_after (new_expiry )
79+ )
80+ for ext in cert .extensions :
81+ builder = builder .add_extension (ext .value , ext .critical )
82+ return builder .sign (key , cert .signature_hash_algorithm )
83+
84+ def sanitize_path (path ):
85+ return path .strip ().strip ('"' ).strip ("'" )
86+
87+ def home ():
88+ while True :
89+ banner ()
90+ print ("\n Press Enter to start or Ctrl+C to exit." )
91+ try :
92+ inp = input ()
93+ except KeyboardInterrupt :
94+ print ("\n Exiting." )
95+ sys .exit (0 )
96+ if inp .strip () == "" :
97+ main_menu ()
98+ banner ()
99+ print ("\n Press Enter to exit." )
100+ input ()
101+ sys .exit (0 )
102+
103+ def main_menu ():
104+ banner ()
105+ raw_path = input ("PFX file path: " )
106+ pfx_path = sanitize_path (raw_path )
107+ password = input ("PFX password: " ).strip ()
108+ try :
109+ key , cert = load_pfx (pfx_path , password )
110+ except Exception as e :
111+ print (f"\n Error loading PFX: { e } " )
112+ print ("Press Enter to return to home screen." )
113+ input ()
114+ return
115+ print ("\n Choose an option:" )
116+ print ("1. Analyze certificate" )
117+ print ("2. Extend certificate expiry by 1 year" )
118+ print ("3. Analyze and extend" )
119+ choice = input ("Selection (1/2/3): " ).strip ()
120+ if choice in {"1" , "3" }:
121+ print ()
122+ analyze_certificate (cert )
123+ if choice in {"2" , "3" }:
124+ out_path = input ("\n Output path for extended certificate (PEM): " ).strip ()
125+ new_cert = extend_certificate_expiry (cert , key )
126+ with open (out_path , "wb" ) as f :
127+ f .write (new_cert .public_bytes (encoding = serialization .Encoding .PEM ))
128+ print (f"Extended certificate saved to { out_path } " )
129+ print ("\n Press Enter to return to the home screen." )
130+ input ()
131+
132+ if __name__ == "__main__" :
133+ home ()
0 commit comments