Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-2709: added remote server functionality

d-millarf4228f1329e1
diff --git a/Ghidra/Debug/Debugger-agent-lldb/src/main/java/agent/lldb/lldb/DebugClient.java b/Ghidra/Debug/Debugger-agent-lldb/src/main/java/agent/lldb/lldb/DebugClient.java
index 127b97a48c..35192846ca 100644
--- a/Ghidra/Debug/Debugger-agent-lldb/src/main/java/agent/lldb/lldb/DebugClient.java
+++ b/Ghidra/Debug/Debugger-agent-lldb/src/main/java/agent/lldb/lldb/DebugClient.java
@@ -385,6 +385,8 @@ public interface DebugClient extends DebugClientReentrant {
SBProcess attachProcess(DebugServerId si, int keyType, String key, boolean wait, boolean async);
+ SBProcess connectRemote(DebugServerId localServer, String key, boolean async);
+
SBProcess createProcess(DebugServerId si, String fileName);
SBProcess createProcess(DebugServerId si, String fileName,
diff --git a/Ghidra/Debug/Debugger-agent-lldb/src/main/java/agent/lldb/lldb/DebugClientImpl.java b/Ghidra/Debug/Debugger-agent-lldb/src/main/java/agent/lldb/lldb/DebugClientImpl.java
index 5997e9b965..f3b51c2b7e 100644
--- a/Ghidra/Debug/Debugger-agent-lldb/src/main/java/agent/lldb/lldb/DebugClientImpl.java
+++ b/Ghidra/Debug/Debugger-agent-lldb/src/main/java/agent/lldb/lldb/DebugClientImpl.java
@@ -34,6 +34,7 @@ public class DebugClientImpl implements DebugClient {
private DebugOutputCallbacks ocb;
//private DebugEventCallbacks ecb;
private SBCommandInterpreter cmd;
+ private boolean sessionsAreImmutable;
public DebugClientImpl() {
}
@@ -110,6 +111,29 @@ public class DebugClientImpl implements DebugClient {
return process;
}
+ @Override
+ public SBProcess connectRemote(DebugServerId si, String key, boolean async) {
+ SBListener listener = new SBListener();
+ SBError error = new SBError();
+ session = createNullSession();
+ SBProcess process = session.ConnectRemote(listener, key, null, error);
+ if (!error.Success()) {
+ Msg.error(this, error.GetType() + " while attaching to " + key);
+ SBStream stream = new SBStream();
+ error.GetDescription(stream);
+ Msg.error(this, stream.GetData());
+ return null;
+ }
+ if (async) {
+ manager.waitForEventEx();
+ }
+ else {
+ manager.updateState(process);
+ }
+ sessionsAreImmutable = true;
+ return process;
+ }
+
@Override
public SBProcess createProcess(DebugServerId si, String fileName) {
return createProcess(si, fileName, new ArrayList<String>(), new ArrayList<String>(), "");
@@ -232,6 +256,9 @@ public class DebugClientImpl implements DebugClient {
@Override
public Map<String, SBTarget> listSessions() {
+ if (sessionsAreImmutable) {
+ return manager.getKnownSessions();
+ }
Map<String, SBTarget> map = new HashMap<>();
for (int i = 0; i < sbd.GetNumTargets(); i++) {
SBTarget target = sbd.GetTargetAtIndex(i);
diff --git a/Ghidra/Debug/Debugger-agent-lldb/src/main/java/agent/lldb/manager/LldbManager.java b/Ghidra/Debug/Debugger-agent-lldb/src/main/java/agent/lldb/manager/LldbManager.java
index 4bf324eae8..55543e5a9f 100644
--- a/Ghidra/Debug/Debugger-agent-lldb/src/main/java/agent/lldb/manager/LldbManager.java
+++ b/Ghidra/Debug/Debugger-agent-lldb/src/main/java/agent/lldb/manager/LldbManager.java
@@ -404,6 +404,8 @@ public interface LldbManager extends AutoCloseable, LldbBreakpointInsertions {
CompletableFuture<?> attach(String url, boolean wait, boolean async);
+ CompletableFuture<?> connect(String url, boolean async);
+
CompletableFuture<?> launch(String fileName, List<String> args);
CompletableFuture<?> launch(Map<String, ?> args);
diff --git a/Ghidra/Debug/Debugger-agent-lldb/src/main/java/agent/lldb/manager/cmd/LldbRemoteConnectionCommand.java b/Ghidra/Debug/Debugger-agent-lldb/src/main/java/agent/lldb/manager/cmd/LldbRemoteConnectionCommand.java
new file mode 100644
index 0000000000..47af66e6a2
--- /dev/null
+++ b/Ghidra/Debug/Debugger-agent-lldb/src/main/java/agent/lldb/manager/cmd/LldbRemoteConnectionCommand.java
@@ -0,0 +1,79 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package agent.lldb.manager.cmd;
+
+import java.util.LinkedHashSet;
+import java.util.Set;
+
+import SWIG.SBThread;
+import agent.lldb.lldb.DebugClient;
+import agent.lldb.lldb.DebugThreadInfo;
+import agent.lldb.manager.LldbEvent;
+import agent.lldb.manager.evt.*;
+import agent.lldb.manager.impl.LldbManagerImpl;
+
+/**
+ * Implementation of {@link LldbProcess#connect()}
+ */
+public class LldbRemoteConnectionCommand extends AbstractLldbCommand<Set<SBThread>> {
+
+ private LldbProcessCreatedEvent created = null;
+ private boolean completed = false;
+ private String key;
+ private boolean async = false;
+
+ public LldbRemoteConnectionCommand(LldbManagerImpl manager, String key) {
+ this(manager, key, false);
+ }
+
+ public LldbRemoteConnectionCommand(LldbManagerImpl manager, String key, boolean async) {
+ super(manager);
+ this.key = key;
+ }
+
+ @Override
+ public boolean handle(LldbEvent<?> evt, LldbPendingCommand<?> pending) {
+ if (evt instanceof AbstractLldbCompletedCommandEvent && pending.getCommand().equals(this)) {
+ completed = true;
+ }
+ else if (evt instanceof LldbProcessCreatedEvent) {
+ created = (LldbProcessCreatedEvent) evt;
+ }
+ else if (evt instanceof LldbThreadCreatedEvent) {
+ pending.claim(evt);
+ }
+ else if (evt instanceof LldbStoppedEvent) {
+ pending.claim(evt);
+ }
+ return completed && (created != null);
+ }
+
+ @Override
+ public Set<SBThread> complete(LldbPendingCommand<?> pending) {
+ Set<SBThread> threads = new LinkedHashSet<>();
+ for (LldbThreadCreatedEvent adds : pending.findAllOf(LldbThreadCreatedEvent.class)) {
+ DebugThreadInfo info = adds.getInfo();
+ threads.add(info.thread);
+ }
+ return threads;
+ }
+
+ @Override
+ public void invoke() {
+ DebugClient client = manager.getClient();
+ client.connectRemote(client.getLocalServer(), key, async);
+ }
+}
diff --git a/Ghidra/Debug/Debugger-agent-lldb/src/main/java/agent/lldb/manager/impl/LldbManagerImpl.java b/Ghidra/Debug/Debugger-agent-lldb/src/main/java/agent/lldb/manager/impl/LldbManagerImpl.java
index 0cfbbc724f..dd3c3fae76 100644
--- a/Ghidra/Debug/Debugger-agent-lldb/src/main/java/agent/lldb/manager/impl/LldbManagerImpl.java
+++ b/Ghidra/Debug/Debugger-agent-lldb/src/main/java/agent/lldb/manager/impl/LldbManagerImpl.java
@@ -1470,6 +1470,11 @@ public class LldbManagerImpl implements LldbManager {
return execute(new LldbAttachCommand(this, url, wait));
}
+ @Override
+ public CompletableFuture<?> connect(String url, boolean async) {
+ return execute(new LldbRemoteConnectionCommand(this, url, async));
+ }
+
@Override
public CompletableFuture<?> launch(String fileName, List<String> args) {
return execute(new LldbLaunchProcessCommand(this, fileName, args));
diff --git a/Ghidra/Debug/Debugger-agent-lldb/src/main/java/agent/lldb/model/impl/LldbModelTargetConnectorContainerImpl.java b/Ghidra/Debug/Debugger-agent-lldb/src/main/java/agent/lldb/model/impl/LldbModelTargetConnectorContainerImpl.java
index 9111a0e8e1..1498da977e 100644
--- a/Ghidra/Debug/Debugger-agent-lldb/src/main/java/agent/lldb/model/impl/LldbModelTargetConnectorContainerImpl.java
+++ b/Ghidra/Debug/Debugger-agent-lldb/src/main/java/agent/lldb/model/impl/LldbModelTargetConnectorContainerImpl.java
@@ -51,6 +51,11 @@ import ghidra.dbg.target.schema.TargetObjectSchemaInfo;
type = LldbModelTargetProcessAttachByPathConnectorImpl.class,
required = false,
fixed = true),
+ @TargetAttributeType(
+ name = "Connect to server",
+ type = LldbModelTargetProcessRemoteConnectorImpl.class,
+ required = false,
+ fixed = true),
@TargetAttributeType(
name = "Load trace/dump",
type = LldbModelTargetTraceOrDumpConnectorImpl.class,
@@ -75,6 +80,7 @@ public class LldbModelTargetConnectorContainerImpl extends LldbModelTargetObject
protected final LldbModelTargetProcessAttachByPidConnectorImpl processAttacherByPid;
protected final LldbModelTargetProcessAttachByNameConnectorImpl processAttacherByName;
protected final LldbModelTargetProcessAttachByPathConnectorImpl processAttacherByPath;
+ protected final LldbModelTargetProcessRemoteConnectorImpl processRemoteConnect;
protected final LldbModelTargetTraceOrDumpConnectorImpl traceLoader;
protected final LldbModelTargetKernelConnectorImpl kernelAttacher;
@@ -91,7 +97,9 @@ public class LldbModelTargetConnectorContainerImpl extends LldbModelTargetObject
this.processAttacherByName =
new LldbModelTargetProcessAttachByNameConnectorImpl(this, "Attach to process by name");
this.processAttacherByPath =
- new LldbModelTargetProcessAttachByPathConnectorImpl(this, "Attach to process by path");
+ new LldbModelTargetProcessAttachByPathConnectorImpl(this, "Attach to process by path");
+ this.processRemoteConnect =
+ new LldbModelTargetProcessRemoteConnectorImpl(this, "Connect to server");
this.traceLoader = new LldbModelTargetTraceOrDumpConnectorImpl(this, "Load trace/dump");
this.kernelAttacher = new LldbModelTargetKernelConnectorImpl(this, "Attach to kernel");
this.defaultConnector = processLauncher;
@@ -100,6 +108,7 @@ public class LldbModelTargetConnectorContainerImpl extends LldbModelTargetObject
processAttacherByPid, //
processAttacherByName, //
processAttacherByPath, //
+ processRemoteConnect, //
processLauncher, //
processLauncherEx, //
traceLoader //
diff --git a/Ghidra/Debug/Debugger-agent-lldb/src/main/java/agent/lldb/model/impl/LldbModelTargetProcessRemoteConnectorImpl.java b/Ghidra/Debug/Debugger-agent-lldb/src/main/java/agent/lldb/model/impl/LldbModelTargetProcessRemoteConnectorImpl.java
new file mode 100644
index 0000000000..cb9be28ba2
--- /dev/null
+++ b/Ghidra/Debug/Debugger-agent-lldb/src/main/java/agent/lldb/model/impl/LldbModelTargetProcessRemoteConnectorImpl.java
@@ -0,0 +1,95 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package agent.lldb.model.impl;
+
+import java.util.*;
+import java.util.concurrent.CompletableFuture;
+
+import agent.lldb.model.iface2.LldbModelTargetConnector;
+import ghidra.async.AsyncUtils;
+import ghidra.async.TypeSpec;
+import ghidra.dbg.error.DebuggerUserException;
+import ghidra.dbg.target.TargetMethod;
+import ghidra.dbg.target.TargetMethod.ParameterDescription;
+import ghidra.dbg.target.TargetMethod.TargetParameterMap;
+import ghidra.dbg.target.schema.*;
+
+@TargetObjectSchemaInfo(
+ name = "ProcessRemoteConnector",
+ elements = {
+ @TargetElementType(type = Void.class)
+ },
+ attributes = {
+ @TargetAttributeType(type = Void.class)
+ })
+public class LldbModelTargetProcessRemoteConnectorImpl extends LldbModelTargetObjectImpl
+ implements LldbModelTargetConnector {
+
+ protected final LldbModelTargetConnectorContainerImpl connectors;
+ protected final TargetParameterMap paramDescs;
+
+ public LldbModelTargetProcessRemoteConnectorImpl(
+ LldbModelTargetConnectorContainerImpl connectors,
+ String name) {
+ super(connectors.getModel(), connectors, name, name);
+ this.connectors = connectors;
+
+ changeAttributes(List.of(), List.of(), Map.of( //
+ DISPLAY_ATTRIBUTE_NAME, getDisplay(), //
+ TargetMethod.PARAMETERS_ATTRIBUTE_NAME,
+ paramDescs = TargetParameterMap.copyOf(computeParameters()) //
+ ), "Initialized");
+ }
+
+ @Override
+ public CompletableFuture<Void> setActive() {
+ connectors.setDefaultConnector(this);
+ return CompletableFuture.completedFuture(null);
+ }
+
+ protected Map<String, ParameterDescription<?>> computeParameters() {
+ HashMap<String, ParameterDescription<?>> map =
+ new LinkedHashMap<String, ParameterDescription<?>>();
+ ParameterDescription<String> p0 = ParameterDescription.create(
+ String.class, "Host", true, "localhost", "Host", "host for connection");
+ map.put("Host", p0);
+ ParameterDescription<String> p1 = ParameterDescription.create(
+ String.class, "Port", true, "12345", "Port", "port for connection");
+ map.put("Port", p1);
+ ParameterDescription<Boolean> p2 = ParameterDescription.create(
+ Boolean.class, "Async", false, true, "Async", "connect asynchronously");
+ map.put("Async", p2);
+ return map;
+ }
+
+ @Override
+ public TargetParameterMap getParameters() {
+ return TargetMethod.getParameters(this);
+ }
+
+ @Override
+ public CompletableFuture<Void> launch(Map<String, ?> args) {
+ String host = (String) args.get("Host");
+ String port = (String) args.get("Port");
+ String url = "connect://"+host+":"+port;
+ Boolean async = (Boolean) args.get("Async");
+ return AsyncUtils.sequence(TypeSpec.VOID).then(seq -> {
+ getManager().connect(url, async).handle(seq::nextIgnore);
+ }).finish().exceptionally((exc) -> {
+ throw new DebuggerUserException("Launch failed for " + args);
+ });
+ }
+}