1111import de .rub .nds .modifiablevariable .bytearray .ByteArrayModificationFactory ;
1212import de .rub .nds .modifiablevariable .bytearray .ModifiableByteArray ;
1313import de .rub .nds .tlsattacker .core .config .Config ;
14+ import de .rub .nds .tlsattacker .core .constants .HandshakeMessageType ;
1415import de .rub .nds .tlsattacker .core .exceptions .WorkflowExecutionException ;
15- import de .rub .nds .tlsattacker .core .protocol .message .AlertMessage ;
16- import de .rub .nds .tlsattacker .core .protocol .message .CertificateMessage ;
17- import de .rub .nds .tlsattacker .core .protocol .message .ChangeCipherSpecMessage ;
1816import de .rub .nds .tlsattacker .core .protocol .message .ProtocolMessage ;
1917import de .rub .nds .tlsattacker .core .protocol .message .RSAClientKeyExchangeMessage ;
20- import de .rub .nds .tlsattacker .core .protocol .message .ServerHelloDoneMessage ;
21- import de .rub .nds .tlsattacker .core .protocol .message .ServerHelloMessage ;
2218import de .rub .nds .tlsattacker .core .state .State ;
23- import de .rub .nds .tlsattacker .core .state .TlsContext ;
2419import de .rub .nds .tlsattacker .core .workflow .WorkflowExecutor ;
2520import de .rub .nds .tlsattacker .core .workflow .WorkflowExecutorFactory ;
26- import de .rub .nds .tlsattacker .core .workflow .action . ReceiveAction ;
27- import de .rub .nds .tlsattacker .core .workflow .action . SendAction ;
21+ import de .rub .nds .tlsattacker .core .workflow .WorkflowTrace ;
22+ import de .rub .nds .tlsattacker .core .workflow .WorkflowTraceUtil ;
2823import de .rub .nds .tlsattacker .core .workflow .factory .WorkflowTraceType ;
2924import de .rub .nds .tlsattacker .util .MathHelper ;
3025import java .security .PublicKey ;
3126import java .security .interfaces .RSAPublicKey ;
32- import java .util .LinkedList ;
33- import java .util .List ;
34- import org .apache .logging .log4j .Level ;
35- import org .apache .logging .log4j .LogManager ;
36- import org .apache .logging .log4j .core .LoggerContext ;
37- import org .apache .logging .log4j .core .config .Configuration ;
38- import org .apache .logging .log4j .core .config .LoggerConfig ;
3927
4028/**
4129 *
@@ -45,65 +33,59 @@ public class RealDirectMessagePkcs1Oracle extends Pkcs1Oracle {
4533
4634 Config config ;
4735
48- public RealDirectMessagePkcs1Oracle (PublicKey pubKey , Config config ) {
36+ private final String validResponseContent ;
37+
38+ private final String invalidResponseContent ;
39+
40+ public RealDirectMessagePkcs1Oracle (PublicKey pubKey , Config config , String validResponseContent ,
41+ String invalidResponseContent ) {
4942 this .publicKey = (RSAPublicKey ) pubKey ;
5043 this .blockSize = MathHelper .intceildiv (publicKey .getModulus ().bitLength (), 8 );
5144 this .config = config ;
52- this .config .setWorkflowTraceType (WorkflowTraceType .HELLO );
53-
54- LoggerContext ctx = (LoggerContext ) LogManager .getContext (false );
55- Configuration ctxConfig = ctx .getConfiguration ();
56- LoggerConfig loggerConfig = ctxConfig .getLoggerConfig (LogManager .ROOT_LOGGER_NAME );
57- loggerConfig .setLevel (Level .INFO );
58- ctx .updateLoggers ();
45+ this .validResponseContent = validResponseContent ;
46+ this .invalidResponseContent = invalidResponseContent ;
5947 }
6048
6149 @ Override
6250 public boolean checkPKCSConformity (final byte [] msg ) {
63-
51+ // we are initializing a new connection in every loop step, since most
52+ // of the known servers close the connection after an invalid handshake
6453 State state = new State (config );
65- TlsContext tlsContext = state .getTlsContext ();
66- WorkflowExecutor workflowExecutor = WorkflowExecutorFactory .createWorkflowExecutor (
67- config .getWorkflowExecutorType (), state );
54+ state .getConfig ().setWorkflowTraceType (WorkflowTraceType .FULL );
55+ WorkflowExecutor workflowExecutor = WorkflowExecutorFactory .createWorkflowExecutor (state .getConfig ()
56+ .getWorkflowExecutorType (), state );
57+ WorkflowTrace trace = state .getWorkflowTrace ();
6858
69- List <ProtocolMessage > protocolMessages = new LinkedList <>();
70- protocolMessages .add (new ServerHelloMessage (config ));
71- protocolMessages .add (new CertificateMessage (config ));
72- protocolMessages .add (new ServerHelloDoneMessage (config ));
73- state .getWorkflowTrace ().addTlsAction (new ReceiveAction (protocolMessages ));
74- protocolMessages = new LinkedList <>();
75- RSAClientKeyExchangeMessage cke = new RSAClientKeyExchangeMessage (config );
76- protocolMessages .add (cke );
77- protocolMessages .add (new ChangeCipherSpecMessage (config ));
78- state .getWorkflowTrace ().addTlsAction (new SendAction (protocolMessages ));
59+ RSAClientKeyExchangeMessage cke = (RSAClientKeyExchangeMessage ) WorkflowTraceUtil .getFirstSendMessage (
60+ HandshakeMessageType .CLIENT_KEY_EXCHANGE , trace );
61+ ModifiableByteArray epms = new ModifiableByteArray ();
62+ epms .setModification (ByteArrayModificationFactory .explicitValue (msg ));
63+ cke .setPublicKey (epms );
7964
80- protocolMessages = new LinkedList <>();
81- protocolMessages .add (new AlertMessage (config ));
82- state .getWorkflowTrace ().addTlsAction (new ReceiveAction (protocolMessages ));
83-
84- ModifiableByteArray pms = new ModifiableByteArray ();
85- pms .setModification (ByteArrayModificationFactory .explicitValue (msg ));
86- cke .setPublicKey (pms );
87-
88- if (numberOfQueries % 100 == 0 ) {
89- LOGGER .debug ("Number of queries so far: {}" , numberOfQueries );
65+ numberOfQueries ++;
66+ if (numberOfQueries % 1000 == 0 ) {
67+ LOGGER .info ("Number of queries so far: {}" , numberOfQueries );
9068 }
9169
92- boolean valid = true ;
70+ boolean conform = false ;
9371 try {
9472 workflowExecutor .executeWorkflow ();
73+ ProtocolMessage lastMessage = WorkflowTraceUtil .getLastReceivedMessage (trace );
74+ if (lastMessage != null ) {
75+ String lastMessageLower = lastMessage .toString ().toLowerCase ();
76+ if (validResponseContent != null ) {
77+ conform = lastMessageLower .contains (validResponseContent .toLowerCase ());
78+ } else if (invalidResponseContent != null ) {
79+ conform = !lastMessageLower .contains (invalidResponseContent .toLowerCase ());
80+ }
81+ }
9582 } catch (WorkflowExecutionException e ) {
9683 // TODO implementing the orcale through caught exceptions is not
9784 // smart
98- valid = false ;
99- e .printStackTrace ();
100- } finally {
101- numberOfQueries ++;
102- }
103- if (tlsContext .isReceivedFatalAlert ()) {
104- valid = false ;
85+ conform = false ;
86+ LOGGER .info (e .getLocalizedMessage (), e );
10587 }
10688
107- return valid ;
89+ return conform ;
10890 }
10991}
0 commit comments