Skip to content

Commit 7347e70

Browse files
authored
Merge pull request #513 from RUB-NDS/parallelExecutorShutdown
Parallel executor shutdown
2 parents d3f1659 + 93a19a8 commit 7347e70

File tree

9 files changed

+154
-7
lines changed

9 files changed

+154
-7
lines changed

Attacks/src/main/java/de/rub/nds/tlsattacker/attacks/connectivity/ConnectivityChecker.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,22 @@
88
*/
99
package de.rub.nds.tlsattacker.attacks.connectivity;
1010

11+
import de.rub.nds.tlsattacker.core.config.Config;
12+
import de.rub.nds.tlsattacker.core.protocol.message.ClientHelloMessage;
13+
import de.rub.nds.tlsattacker.core.protocol.message.ProtocolMessage;
14+
import de.rub.nds.tlsattacker.core.protocol.message.SSL2ServerHelloMessage;
15+
import de.rub.nds.tlsattacker.core.protocol.message.ServerHelloDoneMessage;
16+
import de.rub.nds.tlsattacker.core.protocol.message.ServerHelloMessage;
17+
import de.rub.nds.tlsattacker.core.record.AbstractRecord;
18+
import de.rub.nds.tlsattacker.core.record.Record;
19+
import de.rub.nds.tlsattacker.core.state.State;
20+
import de.rub.nds.tlsattacker.core.workflow.WorkflowExecutor;
21+
import de.rub.nds.tlsattacker.core.workflow.WorkflowExecutorFactory;
22+
import de.rub.nds.tlsattacker.core.workflow.WorkflowTrace;
23+
import de.rub.nds.tlsattacker.core.workflow.action.ReceiveAction;
24+
import de.rub.nds.tlsattacker.core.workflow.action.ReceiveTillAction;
25+
import de.rub.nds.tlsattacker.core.workflow.action.SendAction;
26+
import de.rub.nds.tlsattacker.core.workflow.action.executor.WorkflowExecutorType;
1127
import de.rub.nds.tlsattacker.transport.Connection;
1228
import de.rub.nds.tlsattacker.transport.TransportHandler;
1329
import de.rub.nds.tlsattacker.transport.TransportHandlerFactory;
@@ -63,4 +79,29 @@ public boolean isConnectable() {
6379
}
6480
}
6581

82+
public boolean speaksTls(Config config) {
83+
config.setHttpsParsingEnabled(Boolean.TRUE);
84+
WorkflowTrace trace = new WorkflowTrace();
85+
trace.addTlsAction(new SendAction(new ClientHelloMessage(config)));
86+
ReceiveTillAction receiveTillAction = new ReceiveTillAction(new ServerHelloDoneMessage());
87+
trace.addTlsAction(receiveTillAction);
88+
State state = new State(config, trace);
89+
WorkflowExecutor executor = WorkflowExecutorFactory.createWorkflowExecutor(WorkflowExecutorType.DEFAULT, state);
90+
executor.executeWorkflow();
91+
if (receiveTillAction.getRecords().size() > 0) {
92+
if (receiveTillAction.getRecords().get(0) instanceof Record) {
93+
return true;
94+
} else {
95+
for (ProtocolMessage message : receiveTillAction.getReceivedMessages()) {
96+
if (message instanceof ServerHelloMessage || message instanceof ServerHelloDoneMessage
97+
|| message instanceof SSL2ServerHelloMessage) {
98+
return true;
99+
}
100+
}
101+
return false;
102+
}
103+
} else {
104+
return false;
105+
}
106+
}
66107
}

TLS-Core/src/main/java/de/rub/nds/tlsattacker/core/config/Config.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,16 @@ public class Config implements Serializable {
8686
*/
8787
private static final String DEFAULT_CONFIG_FILE = "/default_config.xml";
8888

89+
private static final ConfigCache DEFAULT_CONFIG_CACHE;
90+
91+
static {
92+
DEFAULT_CONFIG_CACHE = new ConfigCache(createConfig());
93+
}
94+
8995
public static Config createConfig() {
96+
if (DEFAULT_CONFIG_CACHE != null) {
97+
return DEFAULT_CONFIG_CACHE.getCachedCopy();
98+
}
9099
InputStream stream = Config.class.getResourceAsStream(DEFAULT_CONFIG_FILE);
91100
return ConfigIO.read(stream);
92101

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* TLS-Attacker - A Modular Penetration Testing Framework for TLS
3+
*
4+
* Copyright 2014-2017 Ruhr University Bochum / Hackmanit GmbH
5+
*
6+
* Licensed under Apache License 2.0
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*/
9+
package de.rub.nds.tlsattacker.core.config;
10+
11+
import org.apache.commons.lang3.SerializationUtils;
12+
13+
/**
14+
*
15+
* @author robert
16+
*/
17+
public class ConfigCache {
18+
19+
private final Config cachedConfig;
20+
21+
public ConfigCache(Config cachedConfig) {
22+
this.cachedConfig = cachedConfig;
23+
}
24+
25+
public Config getCachedCopy() {
26+
return SerializationUtils.clone(cachedConfig);
27+
}
28+
29+
}

TLS-Core/src/main/java/de/rub/nds/tlsattacker/core/connection/AliasedConnection.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,14 @@
1212
import de.rub.nds.tlsattacker.transport.Connection;
1313
import de.rub.nds.tlsattacker.transport.ConnectionEndType;
1414
import de.rub.nds.tlsattacker.transport.TransportHandlerType;
15-
import java.io.Serializable;
1615
import java.util.Collection;
1716
import java.util.HashSet;
1817
import java.util.Objects;
1918
import java.util.Set;
2019
import javax.xml.bind.annotation.XmlType;
2120

2221
@XmlType(propOrder = { "alias", "port", "hostname", "timeout", "transportHandlerType" })
23-
public abstract class AliasedConnection extends Connection implements Aliasable, Serializable {
22+
public abstract class AliasedConnection extends Connection implements Aliasable {
2423

2524
public static final String DEFAULT_CONNECTION_ALIAS = "defaultConnection";
2625
public static final TransportHandlerType DEFAULT_TRANSPORT_HANDLER_TYPE = TransportHandlerType.TCP;

TLS-Core/src/main/java/de/rub/nds/tlsattacker/core/protocol/message/extension/KS/KeyShareStoreEntry.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import de.rub.nds.modifiablevariable.util.ByteArrayAdapter;
1212
import de.rub.nds.tlsattacker.core.constants.NamedGroup;
13+
import java.io.Serializable;
1314
import java.util.Arrays;
1415
import javax.xml.bind.annotation.XmlAccessType;
1516
import javax.xml.bind.annotation.XmlAccessorType;
@@ -18,7 +19,7 @@
1819

1920
@XmlRootElement
2021
@XmlAccessorType(XmlAccessType.FIELD)
21-
public class KeyShareStoreEntry {
22+
public class KeyShareStoreEntry implements Serializable {
2223

2324
private NamedGroup group;
2425

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* TLS-Attacker - A Modular Penetration Testing Framework for TLS
3+
*
4+
* Copyright 2014-2017 Ruhr University Bochum / Hackmanit GmbH
5+
*
6+
* Licensed under Apache License 2.0
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*/
9+
package de.rub.nds.tlsattacker.core.workflow;
10+
11+
import java.util.concurrent.Executors;
12+
import java.util.concurrent.ThreadFactory;
13+
14+
/**
15+
*
16+
* @author robert
17+
*/
18+
public class NamedThreadFactory implements ThreadFactory {
19+
20+
private int number;
21+
22+
private final String prefix;
23+
24+
public NamedThreadFactory(String prefix) {
25+
this.number = 1;
26+
this.prefix = prefix;
27+
}
28+
29+
@Override
30+
public Thread newThread(Runnable r) {
31+
Thread newThread = Executors.defaultThreadFactory().newThread(r);
32+
newThread.setName(prefix + "-" + number);
33+
number++;
34+
return newThread;
35+
}
36+
37+
}

TLS-Core/src/main/java/de/rub/nds/tlsattacker/core/workflow/ParallelExecutor.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.concurrent.ExecutorService;
1818
import java.util.concurrent.Future;
1919
import java.util.concurrent.LinkedBlockingDeque;
20+
import java.util.concurrent.ThreadFactory;
2021
import java.util.concurrent.ThreadPoolExecutor;
2122
import java.util.concurrent.TimeUnit;
2223
import org.apache.logging.log4j.LogManager;
@@ -45,12 +46,28 @@ public ParallelExecutor(int size, int reexecutions) {
4546
}
4647
}
4748

49+
public ParallelExecutor(int size, int reexecutions, ThreadFactory factory) {
50+
executorService = new ThreadPoolExecutor(size, size, 10, TimeUnit.DAYS, new LinkedBlockingDeque<Runnable>(),
51+
factory);
52+
this.reexecutions = reexecutions;
53+
this.size = size;
54+
if (reexecutions < 0) {
55+
throw new IllegalArgumentException("Reexecutions is below zero");
56+
}
57+
}
58+
4859
public Future addTask(State state) {
60+
if (executorService.isShutdown()) {
61+
throw new RuntimeException("Cannot add Tasks to already shutdown executor");
62+
}
4963
Future<?> submit = executorService.submit(new StateThreadExecutor(state, reexecutions));
5064
return submit;
5165
}
5266

5367
public void bulkExecute(List<State> stateList) {
68+
if (executorService.isShutdown()) {
69+
throw new RuntimeException("Cannot add Tasks to already shutdown executor");
70+
}
5471
List<Future> futureList = new LinkedList<>();
5572
for (State state : stateList) {
5673
futureList.add(addTask(state));
@@ -73,6 +90,10 @@ public int getSize() {
7390
return size;
7491
}
7592

93+
public void shutdown() {
94+
executorService.shutdown();
95+
}
96+
7697
private class StateThreadExecutor implements Runnable {
7798

7899
private final State state;

Transport/src/main/java/de/rub/nds/tlsattacker/transport/Connection.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
*/
99
package de.rub.nds.tlsattacker.transport;
1010

11+
import java.io.Serializable;
1112
import java.util.Objects;
1213
import javax.xml.bind.annotation.XmlAccessType;
1314
import javax.xml.bind.annotation.XmlAccessorType;
1415
import javax.xml.bind.annotation.XmlTransient;
1516

1617
@XmlTransient
1718
@XmlAccessorType(XmlAccessType.FIELD)
18-
public abstract class Connection {
19+
public abstract class Connection implements Serializable {
1920

2021
protected Integer port = null;
2122
protected String hostname = null;

Transport/src/main/java/de/rub/nds/tlsattacker/transport/TransportHandler.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,19 @@ public byte[] fetchData() throws IOException {
4242
ByteArrayOutputStream stream = new ByteArrayOutputStream();
4343
long minTimeMillies = System.currentTimeMillis() + timeout;
4444
while ((System.currentTimeMillis() < minTimeMillies) && (stream.toByteArray().length == 0)) {
45-
while (inStream.available() != 0) {
46-
int read = inStream.read();
47-
stream.write(read);
45+
if (inStream.available() != 0) {
46+
while (inStream.available() != 0) {
47+
int read = inStream.read();
48+
stream.write(read);
49+
}
50+
} else {
51+
try {
52+
Thread.sleep(1);
53+
} catch (InterruptedException ex) {
54+
throw new RuntimeException("Got Interrupted while waiting for Data");
55+
}
4856
}
57+
4958
}
5059
return stream.toByteArray();
5160
}

0 commit comments

Comments
 (0)