Introduction

X.509 certificates are used to secure communications online by encrypting data and verifying identities, such as in HTTPS or VPNs. During penetration tests, it’s important to check if systems correctly handle cloned certificates. Cloned certificates are fake copies that look like real ones but aren’t properly signed by a trusted CA.

I wrote a simple python script called dollyx509 that creates clones of X.509 certificates.

Script and Usage

The script can be found on GitHub: dollyx509.

The usage is quite simple:

u d C A o s o l u p a l o t t - - - - g l n h i h i c k - e y e o o , i o : x r n i o o n u 5 X : s n u u f t d 0 . : - p t t o f o 9 5 E h u p p r o l 0 m e t u u m r l 9 a l c t t m y n p e c k { x c u r e e d { 5 e e t r y e d 0 r l , t , r e 9 t , , r i D p , [ f u - - e p - i s c - k m e h c s e c e } m ] a r e y } t t r o - e i t u i s n o t s C u O O I O i b h i e t u o u n u n y o n r t u t p t p w p t o p t p u p u r u i u u p u t u t e t t f t t u t t c a h c i p t f e d i e c u f k f o f r i s r a t i e i r o t n t t c l y l m r g h e e e e a m - e r t a c a l t t o o t p o f f o o c f o u e m c c g f t r e l l e c p t s o o n e c u i s n n e r e t f a e e r t r c i g d a i t e c e t f i r a c e i f t t a e d c i e n r a c - d t p t a k a i r e t n e f i e o d x i v / u i c a k t g t a t e p e t e y u n e t e k k r e e a y y t i [ n - g - i n n e f w o r k m e y { d m e a r t , e p r e i m a } l ] . [ - - o u t f o r m { d e r , p e m } ]

How it Works

The script is quite small and the following excerpt shows the magic:

class Certificate:
    def __init__(self, file, format='pem'):
        self.import_certificate(file, format)

    # [...]

    def import_certificate(self, file, format='pem'):
        with open(file, "r") as f:
            self.cert = OpenSSL.crypto.load_certificate(self.__get_filetype(format), f.read())

    # [...]

    def clone(self):
        self.key = OpenSSL.crypto.PKey()
        self.key.generate_key(self.cert.get_pubkey().type(), self.cert.get_pubkey().bits())
        self.cert.set_pubkey(self.key)
        self.cert.sign(self.key,'sha256')

This code reads the certificate from the provided file, creates new key material and self-signs the certificate.

Example Usage

We have the following certificate:

$ C e o r p t e i D S S n f a i i s i t g g s c a n n l a : V S S I V S S X a a 0 [ 3 t e e i s a u u 5 t t 8 . 5 x e r r g s l b b 0 u u : . : 5 : s i n u i j j 9 r r 8 . 9 0 i a 0 a e d N N e e P v X X X X X A X X C e e b ] 6 9 o l 3 t r i o o c c u 3 5 5 5 5 5 u 5 5 T : : n : u : t t t t t b 0 0 0 0 0 t 0 0 A V 6 4 - : N d r y : l e 9 9 9 9 9 h 9 9 P l a 8 4 i u 9 e C B A P i P M E x v D v T v C v 0 v 1 o O C v D v P r S S g l : : n 3 m : = e f C u c u o x t 3 i 3 L 3 A 3 9 3 4 r C A 3 N 3 o e i i o u 2 1 b e A U f t N b b d p e g S : : : i S S l c g g r e 9 c o ( e f l S o e = l K l u o n K i E B F S 6 A 2 t P I S : C i e n n i : : r 0 r : g , r r e i e i l 0 c [ 6 8 n s e t x W a A u F u E y s u * e c r e V L T E S e V L T E S t d i x : 8 o e m c y c u 0 f . 0 8 e i y a t e s L b : t : - s b . r y t d e o i x i d e o i x i h 4 g 2 c r O : : a - s : : . : : n o l e b i S j 4 h B I u j e t : i r g m t g r g m t g m : i ) : i = n K A K : b 3 . 9 a t n U n c E e 5 o 3 n U e e m i f C s e e n C s e e n : 5 n 0 t L A N u e l e d d ] 3 2 : s s S d S c : r : f R r c a f 2 i e i I s n a e i I s n a 9 a b h e u o e y g y : : : : : a i e e C t 8 i 1 o I s t n i . c r o D t s t r o D t s t s : l : m t g v l o : c a b 4 6 g g d r o A t 7 r : u c 2 a t n a i u t n a i u h b . 3 : ' d I r b 7 c 3 5 e n v n K : y : m h - A e a 3 t i m o r i m o r a 4 p 7 s 2 2 u n i ( : : : 5 : a K e s e 0 B a t l l t . e f p n e f p n e 2 : e : s 2 0 s f t 4 b b 8 3 t e r t y 0 K 7 t t U t d e 1 i s i s 5 5 m d h E s o h 0 1 2 b 7 c u y r : e : i p R e u 4 S c : : : : : c : : : : : 6 8 d a n 1 1 . : m 9 : : : r r A a I A y 5 o : I r s P 0 C a a W : - : 2 c 7 7 c : 6 0 9 8 ( i e U u i d 0 8 n / : n s o . T t v B 5 A n e 3 [ 3 t v A B A n e 3 [ E i d n 2 5 r : : h e a 4 0 t , s t n e : I : / h a . l 1 s e 1 7 D u o c 0 . 2 e 1 D 4 u o c 0 . 6 t 8 o 0 6 y 3 3 r b : : : x i a h t n 1 d 5 A r t t c i . : : : g n d : . : : : g n d : . : h : o : W p 1 1 s i 1 0 d 1 c K g e s t 8 e 6 c 3 t i h c 2 T ( 3 F e s 4 . 4 T ( F 6 e s 4 . 8 R 3 u 3 i t : : a t 3 1 7 0 a e e n : i : n : c . p v , i . i 0 E C 2 a 6 ] 1 i 0 7 7 2 a 5 ] B S 8 t 7 t , 4 4 E ) : : : 0 l y : t f D t C e o : e e 1 m x : : 2 - : : m x : : 2 - : : A : : h 7 6 n 0 0 0 0 i c i B i B s . / D s e 0 F 4 w 0 9 e 0 B 2 w 0 C E 4 - 1 R C c 8 4 7 1 E c r e : f : s l / N N : s ) B 2 1 i 2 7 s ) E 9 1 i 2 A n 5 t 7 S N 2 2 r : : : ) n a i r C i A : e r a S t : : 8 t : : t : : 8 t : : c : e : A = 0 0 y 1 2 f c t t : B e E n 3 m : a 2 C : h 2 A a F 5 : h 2 E r 1 x 6 E R 2 2 p c c c i i i : r : c . e e m 4 F 3 - 1 5 m A D 3 - 0 E y 2 t 3 n 3 3 3 t : : : p o c 9 : 5 r i : m p : : 1 S : : p : : 1 S : : p : : c i f c d h n a A 0 . . a : D 7 : H 0 6 : 7 C : H 7 7 t 9 6 r G G o 0 b e e , l : : o l n F A 4 A 0 D C F 4 A 7 C i b a y M M n : : : r 3 0 r e u : : 7 2 : : : : 7 2 : : o : : p T T 3 4 4 m T 8 9 g n e 9 9 . 5 B 8 F B . 5 B B n 6 5 t 7 3 5 e L : : c l C F 4 6 1 F F 1 5 6 F 6 7 f i : : : n S 0 4 r d : : 8 : : : : 5 : : : : o f f 0 t 0 0 . u 4 3 3 4 5 1 0 5 3 6 f 7 n 8 c 8 W : : o s D 5 5 E 0 C 5 5 f 0 : : : e 8 E r s : : 2 : : : : 2 : : : 3 6 e b 1 6 g . B C 0 7 2 C 2 0 D c 6 1 0 2 : : / c A 4 2 5 F 8 4 2 3 a 2 : : : C B 1 h : : 3 : : : 3 : : : 9 6 9 l 5 F 7 9 0 8 C A 9 6 5 c d i : : 5 E G 3 B A G 3 f 9 : : : e 8 A : : M : : : M : : : f c 3 n F F F 1 T 8 9 8 T B 9 f 9 d 7 t : : 2 D 1 D 5 7 6 5 : : : 0 9 : : : : : : : : 3 8 5 A 8 D 3 0 F 3 8 B 2 3 1 0 u : : 9 9 3 D 6 B c : : : t B 8 : : : : : : h 6 B C 8 D 9 3 6 e : : 5 1 0 C 4 E n A 1 : : : : : : t F 4 B 2 2 1 E 8 i : : A 5 3 E B 5 c A C : : : : : : a 6 2 5 E E 3 D 3 t : : 8 D 9 E C 9 i 2 C : : : : : : o F 6 F B 1 1 8 6 n 4 4 3 8 2 0 : : : : : : 6 9 B 6 8 A C 9 2 A A 4 : : : :

Note:

  • The subject is CN=emanuelduss.ch
  • The certificate was issued on Aug 22 17:31:47 2023 GMT
  • The modulus of the public key start with 00:bd:cb:b1
  • The signature starts with 08:8b:68:29

Cloning the certificate using dollyx509:

$ d o l l y x 5 0 9 - i o r i g i n a l . p e m - c c l o n e d . p e m - k c l o n e d . k e y

The following files were created:

$ c c l l f o o i n n l e e e d d . . c k p l e e o y m n : : e d O P . p E k e M e n y S c S e c H r l t o p i n r f e i i d v c . a a p t t e e e m k e y ( n o p a s s w o r d )

The cloned certificate looks like this:

$ C e o r p t e i D S S n f a i i s i t g g s c a n n l a : V S S I V S S X a a 7 8 [ c 6 t e e i s a u u 5 t t e c . 6 1 x e r r g s l b b 0 u u : : . : : 5 : s i n u i j j 9 r r 1 5 . 1 1 0 i a 0 a e d N N e e P v X X X X X A X X C e e f 4 ] 0 8 9 o l 3 t r i o o c c u 3 5 5 5 5 5 u 5 5 T : : : : n : u : t t t t t b 0 0 0 0 0 t 0 0 A V 2 4 8 4 - : N d r y : l e 9 9 9 9 9 h 9 9 P l a 6 6 8 2 i u 9 e C B A P i P M E x v D v T v C v 0 v 1 o O C v D v P r S S g l : : : : n 3 m : = e f C u c u o x t 3 i 3 L 3 A 3 9 3 4 r C A 3 N 3 o e i i o u b f c 8 b e A U f t N b b d p e g S : : : i S S l c g g r e 4 2 3 8 c ( e f l S o e = l K l u o n K i E B F S 6 A 2 t P I S : C i e n n i : : : : : l 0 r : g , r r e i e i l 0 4 [ 6 c n s e t x W a A u F u E y s u * e c r e V L T E S e V L T E S t a e e 8 o x : 8 o e m c y c u 0 7 . 6 1 e i y a t e s L b : t : - s b . r y t d e o i x i d e o i x i h f 0 5 d n 2 c r O : : a - s : : . : : n o l e b i S j 4 h B I u j e t : i r g m t g r g m t g m : : : : e ) : i = n K A K : a c . 2 2 t n U n c E e 5 o 3 n U e e m i f C s e e n C s e e n : b 3 9 b d 0 t L A N u e l e 9 0 ] 9 1 : s s S d S c : r : f R r c a f 2 i e i I s n a e i I s n a 9 f d f . b h e u o e y g y : : : : : a i e e C t 8 i 1 o I s t n i . c r o D t s t r o D t s t s : : : : p : m t g v l o : 3 a c 9 6 g g d r o A t 7 r : u c 2 a t n a i u t n a i u h a b 5 6 e 3 : ' d I r 1 0 e d 5 e n v n K : y : m h - A e a 3 t i m o r i m o r a 4 5 6 b m 7 s 2 2 u n i ( : : : 5 : a K e s e 0 B a t l l t . e f p n e f p n e 2 : : : : : s 2 0 s f t 4 0 6 a 3 t e r t y 0 K 7 t t U t d e 1 i s i s 5 4 9 0 9 - d h E s o h 0 5 4 6 7 c u y r : e : i p R e u 4 S c : : : : : c : : : : : 6 5 7 3 0 n d a n 1 1 . : m 9 : : : r r A a I A y 5 o : I r s P 0 C a a W : : : o : 2 c 7 7 c : 6 5 3 a ( i e U u i d 0 8 n / : n s o . T t v B 5 A n e 3 [ 3 t v A B A n e 3 [ E i a c 0 o 2 5 r : : h 7 f 0 0 t , s t n e : I : / h a . l 1 s e 1 7 D u o c 0 . 2 e 1 D 4 u o c 0 . 6 t 1 f 6 u 0 6 y 3 3 r b : : : x i a h t n 1 d 5 A r t t c i . : : : g n d : . : : : g n d : . : h : : : t : W p 1 1 s i c f 7 1 c K g e s t 8 e 6 c 3 t i h c 2 T ( 3 F e s 4 . 4 T ( F 6 e s 4 ] 8 R 1 6 8 3 i t : : a t c 5 1 0 a e e n : i : n : c . p v , i . i 0 E C 2 a 6 ] 1 i 0 7 7 2 a 5 B S c 6 5 - 7 t , 4 4 E ) : : : 0 l y : t f D t C e o : e e 1 m x : : 2 - : : m x : : 2 - : : A : : : t : h 7 6 n 9 e c 0 i c i B i B s . / D s e 0 F 4 w 0 9 e 0 B 2 w 0 C E 4 c 1 e 1 R C c 5 d 3 1 E c r e : f : s l / N N : s ) B 2 1 i 2 7 s ) E 9 1 i 2 A n 3 c 3 x 7 S N 2 2 r : : : ) n a i r C i A : e r a S t : : 8 t : : t : : 8 t : : c : : : t : A = 0 0 y 5 a 1 c t t : B e E n 3 m : a 2 C : h 2 A a F 5 : h 2 E r 0 4 1 6 E R 2 2 p d 0 2 i i i : r : c . e e m 4 F 3 - 1 5 m A D 3 - 0 E y f e 6 3 n 3 3 3 t : : : p o c 9 : 5 r i : m p : : 1 S : : p : : 1 S : : p : : : : c i 4 d 8 h n a A 0 . . a : D 7 : H 0 6 : 7 C : H 7 7 t 4 6 0 6 r G G o 8 e f e , l : : o l n F A 4 A 0 D C F 4 A 7 C i d 2 4 a y M M n : : : r 3 0 r e u : : 7 2 : : : : 7 2 : : o : : : : p T T 1 8 c m T 8 9 g n e 9 9 . 5 B 8 F B . 5 B B n e e 4 5 t 5 f 3 e L : : c l C F 4 6 1 F F 1 5 6 F 6 8 1 a f i : : : n S 0 4 r d : : 8 : : : : 5 : : : : : : o f 3 d t 0 0 . u 4 3 3 4 5 1 0 5 3 6 2 7 a 7 n 1 7 d W : : o s D 5 5 E 0 C 5 5 8 7 d 0 : : : e 8 E r s : : 2 : : : : 2 : : : : : c d e b 1 6 g . B C 0 7 2 C 2 0 D a e 4 6 f 2 c : : / c A 4 2 5 F 8 4 2 3 4 2 e 2 : : : C B 1 h : : 3 : : : 3 : : : : : 0 d 6 l 5 F 7 9 0 8 C A 3 4 4 6 f 1 3 i : : 5 E G 3 B A G 3 c 5 9 9 : : : e 8 A : : M : : : M : : : : : 1 0 2 n F F F 1 T 8 9 8 T B 6 6 2 f 2 f 6 t : : 2 D 1 D 5 7 5 8 3 5 : : : 0 9 : : : : : : : : : : 2 1 3 A 8 D 3 0 F 3 8 B 2 d a 1 u : : 9 9 3 D 6 B c : : : t B 8 : : : : : : h 6 B C 8 D 9 3 6 e : : 5 1 0 C 4 E n A 1 : : : : : : t F 4 B 2 2 1 E 8 i : : A 5 3 E B 5 c A C : : : : : : a 6 2 5 E E 3 D 3 t : : 8 D 9 E C 9 i 2 C : : : : : : o F 6 F B 1 1 8 6 n 4 4 3 8 2 0 : : : : : : 6 9 B 6 8 A C 9 2 A A 4 : : : :

Note:

  • The subject is still the same: CN=emanuelduss.ch
  • The certificate issued date is still the same: Aug 22 17:31:47 2023 GMT
  • The modulus of the public key has changed and starts now with 00:a9:31:05
  • The signature value has changed and starts now with 7e:1f:26:b4

So, basically everything is exactly the same except the public key and signature.

This can also be seen when the two certificates are compared, e.g. using vimdiff:

vimdiff <(openssl x509 -in original.pem -noout -text) <(openssl x509 -in cloned.pem -noout -text)

Only the public key and signature has changed:

These cloned certificates can now be used to test if the involved systems correctly verify the signature.

Alternatives

There is another nice bash script which can also be used to clone certificates: clone-cert.

The Burp Suite extension SAML Raider has also the ability to clone certificates. In addition, it can also be used to change the properties of the certificates to other values: SAML Raider.

References