Skip to content

SWE-agent takes a GitHub issue and tries to automatically fix it, using your LM of choice. It can also be employed for offensive cybersecurity or competitive coding challenges. [NeurIPS 2024]

added connection to server interactive command

Talor Abramovichea8f0801ffc2
diff --git a/config/commands/connect.py b/config/commands/_connect.py
similarity index 65%
rename from config/commands/connect.py
rename to config/commands/_connect.py
index db11337f..fbe79f68 100755
--- a/config/commands/connect.py
+++ b/config/commands/_connect.py
@@ -1,8 +1,15 @@
#!/usr/bin/env python3
+
+"""This helper command is used for interacting with a remote server.
+It opens a shell-like connection to a server, which allows sending and retrieving content from the server.
+
+Usage:
+ python _connect.py
+"""
+
import ast
import cmd
-import argparse
from pwn import *
import setproctitle
@@ -11,37 +18,18 @@ setproctitle.setproctitle("connect")
BUFFER_SIZE = 1024
SERVER_TIMEOUT = 10
-def parse_arguments():
- parser = argparse.ArgumentParser(description="Connect to a remote server")
-
- # Add arguments for IP and port
- parser.add_argument("server_address", type=str, help="Server address")
- parser.add_argument("port", type=int, help="Port number")
-
- # Parse the arguments
- args = parser.parse_args()
-
- return args
-
class NetcatShell(cmd.Cmd):
intro = "Welcome to the netcat shell. Type help or ? to list commands.\n"
server = None
- def __init__(self, server_address: str, port: int, *args, **kwargs) -> None:
- super().__init__(*args, **kwargs)
- self.ip_address = server_address
- self.port = port
-
- def preloop(self):
- self.do_connect()
-
prompt = "(nc) "
def do_connect(self, arg=""):
"""
Connecting to the server
"""
- self.server = remote(self.ip_address, self.port)
+ args = arg.split()
+ self.server = remote(args[0], args[1])
data_received_from_server = self._recv()
if data_received_from_server:
print("\n-------SERVER RESPONSE-------\n\n", data_received_from_server, "\n\n-------END OF RESPONSE-------\n")
@@ -74,5 +62,4 @@ class NetcatShell(cmd.Cmd):
return True
if __name__ == "__main__":
- args = parse_arguments()
- NetcatShell(args.server_address, args.port).cmdloop()
\ No newline at end of file
+ NetcatShell().cmdloop()
\ No newline at end of file
diff --git a/config/commands/server_connection.sh b/config/commands/server_connection.sh
new file mode 100644
index 00000000..18afaf56
--- /dev/null
+++ b/config/commands/server_connection.sh
@@ -0,0 +1,68 @@
+_connect_command() {
+ echo "<<INTERACTIVE||$@||INTERACTIVE>>"
+}
+
+# @yaml
+# signature: connect_start <server_address> <port>
+# docstring: Starts a
+# arguments:
+# server_address:
+# type: string
+# description: the server address to initiate connection to
+# required: true
+# port:
+# type: int
+# description: desired port for connection
+# required: true
+connect_start() {
+ if [ -z "$1" ] || [ -z "$2" ]
+ then
+ echo "Usage: connect_start <server_address> <port>"
+ return
+ fi
+ _connect_command "SESSION=connect"
+ _connect_command "START"
+ _connect_command "connect $1 $2"
+ export INTERACTIVE_SESSION="connect $@"
+}
+
+# @yaml
+# signature: connect_sendline [<line>]
+# docstring: Sends a line with unicode/hexadecimal values to the connection. Sending hexadecimal bytes should be done using \x<hh> where hh stands for the sepecific byte you want to send.
+# arguments:
+# line:
+# type: string
+# description: The line to send to the connection
+# required: false
+connect_sendline() {
+ _connect_command "SESSION=connect"
+ _connect_command 'sendline '$@
+}
+
+# @yaml
+# signature: connect_exec <command>
+# docstring: Executes arbitrary connect command in connect session.
+# arguments:
+# command:
+# type: string
+# description: command to execute (wrap in single quotes to avoid shell escaping and substitution)
+# required: true
+connect_exec() {
+ if [ -z "$1" ]
+ then
+ echo "Usage: connect_exec <command>"
+ return
+ fi
+ _connect_command "SESSION=connect"
+ _connect_command "$@"
+}
+
+# @yaml
+# signature: connect_stop
+# docstring: Stops the current connect session.
+connect_stop() {
+ _connect_command "SESSION=connect"
+ _connect_command "quit"
+ _connect_command "STOP"
+ unset INTERACTIVE_SESSION
+}
diff --git a/config/default_ctf.yaml b/config/default_ctf.yaml
index df733942..060910f5 100644
--- a/config/default_ctf.yaml
+++ b/config/default_ctf.yaml
@@ -113,5 +113,7 @@ command_files:
- config/commands/decompile.py
- config/commands/disassemble.py
- config/commands/debug.sh
+- config/commands/_connect.py
+- config/commands/server_connection.sh
parse_command: ParseCommandDetailed
history_processor: Last5Observations
diff --git a/docker/ctf_requirements.txt b/docker/ctf_requirements.txt
index 57c063d6..c6ff0b51 100644
--- a/docker/ctf_requirements.txt
+++ b/docker/ctf_requirements.txt
@@ -4,4 +4,5 @@ gmpy2
pycryptodome
mpmath
sympy
-ruff
\ No newline at end of file
+ruff
+setproctitle
\ No newline at end of file
diff --git a/docker/swe-ctf.Dockerfile b/docker/swe-ctf.Dockerfile
index ad563f87..ca9e9aaf 100644
--- a/docker/swe-ctf.Dockerfile
+++ b/docker/swe-ctf.Dockerfile
@@ -63,6 +63,7 @@ RUN pip install -r /root/requirements.txt && rm -f /root/requirements.txt
RUN cd /opt && git clone https://github.com/RsaCtfTool/RsaCtfTool.git && apt-get install -y libgmp3-dev libmpc-dev && cd RsaCtfTool && pip3 install -r "requirements.txt"
ENV PATH=$PATH:/opt/RsaCtfTool
+ENV PWNLIB_NOTERM=1
WORKDIR /
diff --git a/sweagent/agent/interactive_commands.py b/sweagent/agent/interactive_commands.py
index bc6b60bc..4488f010 100644
--- a/sweagent/agent/interactive_commands.py
+++ b/sweagent/agent/interactive_commands.py
@@ -8,6 +8,7 @@ from simple_parsing.helpers.serialization.serializable import FrozenSerializable
@dataclass(frozen=True)
class InteractiveSessionConfig(FrozenSerializable):
+ cmdline: str
terminal_prompt_pattern: str
start_command: str
exit_command: str
@@ -24,9 +25,17 @@ class InteractiveSession:
INTERACTIVE_SESSIONS_CONFIG = {
"gdb": InteractiveSessionConfig(
+ cmdline="gdb",
terminal_prompt_pattern="(gdb) ",
start_command="debug_start",
exit_command="debug_stop",
quit_commands_in_session=["quit"],
),
+ "connect": InteractiveSessionConfig(
+ cmdline="/root/commands/_connect",
+ terminal_prompt_pattern="(nc) ",
+ start_command="connect_start",
+ exit_command="connect_stop",
+ quit_commands_in_session=["quit"],
+ ),
}
diff --git a/sweagent/environment/swe_env.py b/sweagent/environment/swe_env.py
index 1f95ac95..4abcd61b 100644
--- a/sweagent/environment/swe_env.py
+++ b/sweagent/environment/swe_env.py
@@ -497,7 +497,10 @@ class SWEEnv(gym.Env):
if self.interactive_session is not None:
return self._get_only_one_interactive_error_message_observation()
self.interactive_session = get_interactive_session(
- self.container_name, "/" + self._repo_name, session_name
+ self.container_name,
+ "/" + self._repo_name,
+ session_name,
+ self.args.interactive_sessions_config[session_name].cmdline,
)
elif command == "STOP":
if self.interactive_session.session_process.poll() is None:
diff --git a/sweagent/environment/utils.py b/sweagent/environment/utils.py
index 71b1d66f..ed6c41e6 100644
--- a/sweagent/environment/utils.py
+++ b/sweagent/environment/utils.py
@@ -407,12 +407,12 @@ def get_docker_compose(docker_compose_path: Path) -> Path:
return docker_compose_path
-def get_interactive_session(ctr_name: str, cwd: str, session_name: str, *args) -> InteractiveSession:
+def get_interactive_session(ctr_name: str, cwd: str, session_name: str, cmdline: str, *args) -> InteractiveSession:
"""
Starts a new interacitve session on the given container name.
Returns a subprocess.Popen object that is available for further read/writes for submitting commands and reading output.
"""
- startup_cmd = ["docker", "exec", "-i", "-w", cwd, ctr_name, session_name, *args]
+ startup_cmd = ["docker", "exec", "-i", "-w", cwd, ctr_name, cmdline, *args]
logger.debug(f"Starting interactive session {session_name} with command: {shlex.join(startup_cmd)}")
session = subprocess.Popen(startup_cmd, stdin=PIPE, stdout=PIPE, stderr=STDOUT, text=True, bufsize=1)
time.sleep(DOCKER_START_UP_DELAY)