2025年12月15日月曜日

fail2ban で ban の際に slack に通知する

fail2ban で ban の際に slack に通知する

概要

前回続きです
ban になったら slack に通知するようにしてみました

環境

  • Ubuntu 24.04
  • docker
  • linuxserver/fail2ban 1.1.0-r2-ls26
  • fail2ban 1.1.0

compose.yaml

ユーザの権限部分など少し変更しています
作成したアクションの設定ファイルもマウントするようにしています

services:
  nginx:
    image: nginx:latest
    container_name: nginx
    network_mode: host
    volumes:
      - ./nginx/log:/var/log/nginx  # このログをfail2banで監視
    restart: unless-stopped

  fail2ban:
    image: linuxserver/fail2ban:latest
    container_name: fail2ban
    network_mode: host
    cap_add:
      - NET_ADMIN # BANに必須
      - NET_RAW   # BANに必須
    volumes:
      - ./config/jail.local:/config/fail2ban/jail.d/jail.local
      - ./config/filter.d/nginx-404.conf:/config/fail2ban/filter.d/nginx-404.conf
      - ./config/action.d/slack.conf:/config/fail2ban/action.d/slack.conf
      - ./nginx/log:/remotelogs/nginx:ro  # nginxログを読み取り専用で監視
    environment:
      - PUID=1000  # 自身のユーザIDとグループIDに合わせて変更 (config/ 配下の権限がコンテナ側のユーザIDで変更されてしまうため編集ができなくなる)
      - PGID=122
      - TZ=Asia/Tokyo
      - VERBOSITY=-vv
    restart: unless-stopped

./config/jail.conf

nginx-404 サービスの action に slack を追加します

[DEFAULT]

# "bantime.increment" allows to use database for searching of previously banned ip's to increase a
# default ban time
bantime.increment = true

# "bantime.maxtime" is the max number of seconds using the ban time can reach (doesn't grow further)
bantime.maxtime = 5w

# "bantime.factor" is a coefficient to calculate exponent growing of the formula or common multiplier
bantime.factor = 24

# "bantime" is the number of seconds that a host is banned.
bantime = 1h

# A host is banned if it has generated "maxretry" during the last "findtime"
# seconds.
findtime = 24h

# "maxretry" is the number of failures before a host get banned.
maxretry = 5

# Prevents banning LAN subnets
ignoreip    = 127.0.0.1/8 ::1
              10.0.0.0/8
              172.16.0.0/12
              192.168.0.0/16

# The ban action "iptables-multiport" (default) should work for most
# The ban action "iptables-allports" can be used if multiport causes issues
banaction = iptables-multiport
banaction_allports = iptables-allports

# Read https://github.com/sebres/PoC/blob/master/FW.IDS-DROP-vs-REJECT/README.md before changing block type
# The block type "REJECT --reject-with icmp-port-unreachable" (default behavior) should respond to, but then instantly reject connection attempts
# The block type "DROP" should not respond to connection attempts, resulting in a timeout
#banaction = iptables-multiport[blocktype=DROP]

# Add additional actions
action  = %(action_)s

# [sshd]
# configuration inherits from jail.conf
# enabled = true
# chain   = INPUT
# action  = %(action_)s

[nginx-http-auth]
# configuration inherits from jail.conf
enabled = true
chain   = DOCKER-USER
action  = %(action_)s

[nginx-badbots]
# configuration inherits from jail.d/nginx-badbots.conf 
enabled = true
chain   = DOCKER-USER
action  = %(action_)s

[nginx-botsearch]
# configuration inherits from jail.conf
enabled = true
chain   = DOCKER-USER
action  = %(action_)s

[nginx-deny]
# configuration inherits from jail.d/nginx-deny.conf
enabled = true
chain   = DOCKER-USER
action  = %(action_)s

[nginx-unauthorized]
# configuration inherits from jail.d/nginx-unauthorized.conf
enabled = true
chain   = DOCKER-USER
action  = %(action_)s

[nginx-404]
# Custom filter to detect excessive 404 errors
enabled  = true
chain    = INPUT
port     = http,https
filter   = nginx-404
logpath  = /remotelogs/nginx/access.log
maxretry = 3
findtime = 10s
bantime  = 1h
action   = %(action_)s
           slack

config/filter.d/nginx-404.conf

前回と同じです

config/action.d/slack.conf

各種アクション時に通知するようにします
webhook_url は変更してください

# Fail2Ban configuration file
#
# Action to send notifications to Slack
#

[Definition]

# Option: actionstart
# Notes.: command executed once at the start of Fail2Ban.
actionstart = curl -X POST -H 'Content-type: application/json' \
              --data '{"text":"[Fail2Ban] <name> jail has started"}' \
              <slack_webhook_url>

# Option: actionstop
# Notes.: command executed once at the end of Fail2Ban
actionstop = curl -X POST -H 'Content-type: application/json' \
             --data '{"text":"[Fail2Ban] <name> jail has stopped"}' \
             <slack_webhook_url>

# Option: actioncheck
# Notes.: command executed once before each actionban command
actioncheck =

# Option: actionban
# Notes.: command executed when banning an IP. Take care that the
#         command is executed with Fail2Ban user rights.
actionban = curl -X POST -H 'Content-type: application/json' \
            --data '{"text":"[Fail2Ban] :no_entry: <name> jail banned IP: *<ip>*\nFailures: <failures>\nTime: <time>"}' \
            <slack_webhook_url>

# Option: actionunban
# Notes.: command executed when unbanning an IP. Take care that the
#         command is executed with Fail2Ban user rights.
actionunban = curl -X POST -H 'Content-type: application/json' \
              --data '{"text":"[Fail2Ban] :white_check_mark: <name> jail unbanned IP: *<ip>*"}' \
              <slack_webhook_url>

[Init]

# Slack webhook URL
slack_webhook_url = https://hooks.slack.com/services/xxx/xxx/xxx

動作確認

  • docker compose up -d

あとは localhost/test などで 404 を発生させると通知が来ます
unbanip で ban 解除しても通知が来ます

トラブルシューティング

起動時にちゃんと追加したアクションが読み込まれていることを確認しましょう
ここで読み込まれていないとうまく動作しません

  • docker compose logs | grep ‘action’
fail2ban  |  2025-12-12 10:00:26,132 79986F9C2B28 INFO  Loading configs for action.d/iptables-multiport under /etc/fail2ban
fail2ban  |  2025-12-12 10:00:26,133 79986F9C2B28 DEBUG Reading configs for action.d/iptables-multiport under /etc/fail2ban
fail2ban  |  2025-12-12 10:00:26,133 79986F9C2B28 DEBUG Reading config files: /etc/fail2ban/action.d/iptables-multiport.conf
fail2ban  |  2025-12-12 10:00:26,133 79986F9C2B28 INFO    Loading files: ['/etc/fail2ban/action.d/iptables-multiport.conf']
fail2ban  |  2025-12-12 10:00:26,133 79986F9C2B28 INFO    Loading files: ['/etc/fail2ban/action.d/iptables.conf']
fail2ban  |  2025-12-12 10:00:26,134 79986F9C2B28 INFO    Loading files: ['/etc/fail2ban/action.d/iptables.conf', '/etc/fail2ban/action.d/iptables-multiport.conf']
fail2ban  |  2025-12-12 10:00:26,147 79986F9C2B28 INFO  Loading configs for action.d/slack under /etc/fail2ban
fail2ban  |  2025-12-12 10:00:26,147 79986F9C2B28 DEBUG Reading configs for action.d/slack under /etc/fail2ban
fail2ban  |  2025-12-12 10:00:26,147 79986F9C2B28 DEBUG Reading config files: /etc/fail2ban/action.d/slack.conf
fail2ban  |  2025-12-12 10:00:26,147 79986F9C2B28 INFO    Loading files: ['/etc/fail2ban/action.d/slack.conf']
fail2ban  |  2025-12-12 10:00:26,147 79986F9C2B28 INFO    Loading files: ['/etc/fail2ban/action.d/slack.conf']

最後に

ban になったかどうかをいちいち確認する必要がなくなるので便利です
現在の ban 状況を定期的に送信してくれる機能があると更に便利かなと思います

2025年12月14日日曜日

fail2ban を docker で動かす

fail2ban を docker で動かす

概要

fail2ban をコンテナで動作させる方法を紹介します
よく紹介されているのはホストに直接インストールする方法かなと思います
今回は nginx の不正アクセスを ban する方法を紹介します

環境

  • Ubuntu 24.04
  • docker
  • linuxserver/fail2ban 1.1.0-r2-ls26
  • fail2ban 1.1.0

compose.yaml

fail2ban は linuxserver/fail2ban を使います
fail2ban は仕組み上 iptables を使うのでホストネットワークを使う必要があります

services:
  nginx:
    image: nginx:latest
    container_name: nginx
    network_mode: host
    volumes:
      - ./nginx/log:/var/log/nginx  # このログをfail2banで監視
    restart: unless-stopped

  fail2ban:
    image: linuxserver/fail2ban:latest
    container_name: fail2ban
    network_mode: host
    cap_add:
      - NET_ADMIN # BANに必須
      - NET_RAW   # BANに必須
    volumes:
      - ./config/jail.local:/config/fail2ban/jail.d/jail.local
      - ./config/filter.d/nginx-404.conf:/config/fail2ban/filter.d/nginx-404.conf
      - ./nginx/log:/remotelogs/nginx:ro  # nginxログを読み取り専用で監視
    environment:
      - TZ=Asia/Tokyo
    restart: unless-stopped

./config/jail.local

[DEFAULT]

# "bantime.increment" allows to use database for searching of previously banned ip's to increase a
# default ban time
bantime.increment = true

# "bantime.maxtime" is the max number of seconds using the ban time can reach (doesn't grow further)
bantime.maxtime = 5w

# "bantime.factor" is a coefficient to calculate exponent growing of the formula or common multiplier
bantime.factor = 24

# "bantime" is the number of seconds that a host is banned.
bantime = 1h

# A host is banned if it has generated "maxretry" during the last "findtime"
# seconds.
findtime = 24h

# "maxretry" is the number of failures before a host get banned.
maxretry = 5

# Prevents banning LAN subnets
ignoreip    = 127.0.0.1/8 ::1
              10.0.0.0/8
              172.16.0.0/12
              192.168.0.0/16

# The ban action "iptables-multiport" (default) should work for most
# The ban action "iptables-allports" can be used if multiport causes issues
banaction = iptables-multiport
banaction_allports = iptables-allports

# Read https://github.com/sebres/PoC/blob/master/FW.IDS-DROP-vs-REJECT/README.md before changing block type
# The block type "REJECT --reject-with icmp-port-unreachable" (default behavior) should respond to, but then instantly reject connection attempts
# The block type "DROP" should not respond to connection attempts, resulting in a timeout
#banaction = iptables-multiport[blocktype=DROP]

# Add additional actions
action  = %(action_)s
          apprise-api[host="127.0.0.1", tag="fail2ban"]
          cloudflare[cfuser="YOUR-EMAIL", cftoken="YOUR-TOKEN"]

# [sshd]
# configuration inherits from jail.conf
# enabled = true
# chain   = INPUT
# action  = %(action_)s

[nginx-http-auth]
# configuration inherits from jail.conf
enabled = true
chain   = DOCKER-USER
action  = %(action_)s

[nginx-badbots]
# configuration inherits from jail.d/nginx-badbots.conf
enabled = true
chain   = DOCKER-USER
action  = %(action_)s

[nginx-botsearch]
# configuration inherits from jail.conf
enabled = true
chain   = DOCKER-USER
action  = %(action_)s

[nginx-deny]
# configuration inherits from jail.d/nginx-deny.conf
enabled = true
chain   = DOCKER-USER
action  = %(action_)s

[nginx-unauthorized]
# configuration inherits from jail.d/nginx-unauthorized.conf
enabled = true
chain   = DOCKER-USER
action  = %(action_)s

[nginx-404]
# Custom filter to detect excessive 404 errors
enabled  = true
chain    = INPUT
port     = http,https
filter   = nginx-404
logpath  = /remotelogs/nginx/access.log
maxretry = 3
findtime = 10s
bantime  = 1h

banaction などは適切なものを設定しましょう

ポイントは一番下の nginx-404 です
これで 404 を連発する IP を ban にできます
それ以外も nginx の ban ルールですが認証エラーなどで発生させるのが難しいので今回 404 を追加しています

config/filter.d/nginx-404.conf

# Fail2Ban filter to detect excessive 404 errors from nginx
# Detects clients making too many requests to non-existent pages

[Definition]

failregex = ^<HOST> -.*"(GET|POST|HEAD).*HTTP.*" 404

ignoreregex =

datepattern = {^LN-BEG}%%d/%%b/%%ExY:%%H:%%M:%%S

nginx のログで 404 を正規表現でマッチさせます

起動

  • docker compsoe up -d

動作確認

今回は10秒間に3回404発生でbanなのですぐにbanになります
localhost/test など存在しないページにアクセスしましょう

そして ban になっているか確認します

  • docker compose exec fail2ban fail2ban-client status nginx-404
Status for the jail: nginx-404
|- Filter
|  |- Currently failed: 0
|  |- Total failed:     6
|  `- File list:        /remotelogs/nginx/access.log
`- Actions
   |- Currently banned: 1
   |- Total banned:     1
   `- Banned IP list:   xxx.xxx.xxx.xxx

ban されると i-Filter のお馴染みの画面が表示されるようになります
また iptables -L でホスト側を確認するとちゃんと REJECT ルールがあることが確認できると思います

sudo iptables -L f2b-nginx-404

Chain f2b-nginx-404 (1 references)
target     prot opt source               destination
REJECT     all  --  xxx.xxx.xxx.xxx      anywhere             reject-with icmp-port-unreachable
RETURN     all  --  anywhere             anywhere

ban解除

  • docker compose exec fail2ban fail2ban-client set nginx-404 unbanip xxx.xxx.xxx.xxx

テストなどですぐに解除したい場合は IP を指定して ban 解除できます
iptables にもないことが確認できます

最後に

今回使った linuxserver/fail2ban は /config/fail2ban/filter.d に大量のプリセットがあるのでログをセットするだけでも簡単に ban 監視を開始することができます
今回は 404 ルールだけなかったので手動で追加しています

fail2ban は iptables を操作するツールなので iptables を使ってアクセス制御しているケースとは親和性は高い気がします
逆にクラウドなどのファイアウォールでアクセス制御している場合は fail2ban とは少し相性が悪いので使い分けが必要かなと思います

結局 iptables なのでホストにインストールしても変わりませんが docker compose でアプリを管理していたりホストにはなるべくアプリをインストールしたくないという場合にはやはり docker で fail2ban を動かしたくなるかなと思います

参考サイト

2025年12月13日土曜日

linuxserver openssh-server にログインしたら通知する方法

linuxserver openssh-server にログインしたら通知する方法

概要

ssh したら slack に通知します
s6-overlay という機能を使います

環境

  • macOS 15.7.1
  • docker 29.1.2
  • linuxserver/openssh-server 10.0_p1-r10-ls211

compose.yaml

services:
  ssh_server:
    image: ghcr.io/linuxserver/openssh-server
    container_name: ssh_server
    ports:
      - "2222:2222"
      - "10022:10022"
    environment:
      - PUID=1000
      - PGID=1000
      - PASSWORD_ACCESS=false
      - USER_NAME=operator
      - PUBLIC_KEY_FILE=/config/ssh/authorized_keys
    volumes:
      - ./custom-cont-init.d:/custom-cont-init.d:ro
      - ./custom-services.d:/custom-services.d:ro
      - ./config/ssh:/config/ssh
    restart: unless-stopped

custom-services.d/run

#!/usr/bin/execlineb -P
with-contenv
exec bash /custom-services.d/notify_ssh/notify.sh
  • chmod +x custom-services.d/run

custom-services.d/notify_ssh/notify.sh

#!/usr/bin/env bash

LOGFILE="/config/logs/openssh/current"
WEBHOOK_URL="https://hooks.slack.com/services/xxx/xxx/xxx"

# ログファイル生成待ち
while [ ! -f "$LOGFILE" ]; do
    sleep 1
done

echo "[notify-ssh] starting log monitor"

# ログ監視
tail -Fn0 "$LOGFILE" | while read -r line; do

    # ① 接続ログ (Connection from)
    if echo "$line" | grep -q "Connection from"; then
        IP=$(echo "$line" | grep -oE "([0-9]{1,3}\.){3}[0-9]{1,3}")
        curl -s -X POST -H 'Content-Type: application/json' \
            --data "{\"text\": \"👀 SSH access from $IP\"}" \
            "$WEBHOOK_URL" >/dev/null
    fi

    # ② 成功ログ (Accepted)
    if echo "$line" | grep -q "Accepted"; then
        IP=$(echo "$line" | grep -oE "([0-9]{1,3}\.){3}[0-9]{1,3}")
        USER=$(echo "$line" | awk '{print $(NF-5)}')
        curl -s -X POST -H 'Content-Type: application/json' \
            --data "{\"text\": \"🔐 SSH login success: user=$USER, ip=$IP\"}" \
            "$WEBHOOK_URL" >/dev/null
    fi

    # ③ 失敗ログ (Failed)
    if echo "$line" | grep -q "Failed"; then
        IP=$(echo "$line" | grep -oE "([0-9]{1,3}\.){3}[0-9]{1,3}")
        curl -s -X POST -H 'Content-Type: application/json' \
            --data "{\"text\": \"⚠️ SSH failed login attempt from $IP\"}" \
            "$WEBHOOK_URL" >/dev/null
    fi

done
  • chmod +x custom-services.d/notify_ssh/notify.sh

最後に

コンテナはこの方法がいいです
systemd があれば socat なども使えます

参考サイト

2025年12月12日金曜日

Windows に metasploit をインストールする

Windows に metasploit をインストールする

概要

Windows で攻撃するということはほぼないですが一応インストールしたのでメモしておきます

環境

  • Windows11Home
  • metasploit 6.4.102
  • msfconsole 4.0.5

インストーラからインストール

https://docs.metasploit.com/docs/using-metasploit/getting-started/nightly-installers.html

ここからインストーラをダウンロードしてインストーします
インストール中に Windows defender が反応しまくりますが無視で OK です

Windows defender で metasploit を除外する

除外しないと msfconsole が起動できません

起動

Powershell でもコマンドプロンプトでも OK です

  • cd C:\metasploit-framework\bin
  • .\msfconsole.bat

起動すれば OK です

最後に

リバースシェルを張りたい場合はここを参考にmsfvenomでペイロード(exe)を作成しWindows上で実行します

基本は metasploit は攻撃側のツールなので Metasploit をインストールした Windows に攻撃するのは Windows を脆弱にするしかないです

  • use でどのマシンにどの脆弱性を使うか決める
  • set payload で脆弱性をつけたときに流し込むペイロード(シェルスクリプトや exe など)を決定
  • exploit で攻撃開始

であとは

  • meterapreter で待ち受けなどを別途する

という流れなので Metasploit をインストールしたマシンはあくまでも攻撃側になります

参考サイト

2025年12月11日木曜日

Mac の UTM でディレクトリ共有する方法

Mac の UTM でディレクトリ共有する方法

概要

紹介します

環境

  • macOS 15.7.1
  • UTM 4.7.4
  • Ubuntu 24.04

共有設定

マウント

sudo mkdir -p /mnt/share
sudo mount -t 9p -o trans=virtio share /mnt/share

最後に

特にツールのインストールは不要でした
qemu-guest-agent も不要っぽいです

参考サイト

2025年12月10日水曜日

linuxserver openssh-server にパスワードなしでログインする方法

linuxserver openssh-server にパスワードなしでログインする方法

概要

鍵を作成し登録してあげます

環境

  • macOS 15.7.1
  • docker 29.1.2
  • linuxserver/openssh-server 10.0_p1-r10-ls211

compose.yaml

services:
  ssh_server:
    image: ghcr.io/linuxserver/openssh-server
    container_name: ssh_server
    ports:
      - "2222:2222"
      - "10022:10022" # SSHリモートポートフォーワードトンネル用
    environment:
      - PUID=1000
      - PGID=1000
      - PASSWORD_ACCESS=false
      - USER_NAME=operator
      - PUBLIC_KEY_FILE=/config/ssh/authorized_keys
    volumes:
      - ./custom-cont-init.d:/custom-cont-init.d:ro
      - ./config/ssh:/config/ssh
    restart: unless-stopped

custom-cont-init.d/01-sshd-config.sh

#!/bin/bash

echo "[custom-init] modifying sshd_config..."

# 対象ファイル
SSHD_CONFIG="/config/sshd/sshd_config"

# Agent Forwarding
sed -i 's/#AllowAgentForwarding yes/AllowAgentForwarding yes/' "$SSHD_CONFIG"

# TCP Forwarding
sed -i 's/AllowTcpForwarding no/AllowTcpForwarding yes/' "$SSHD_CONFIG"

# GatewayPorts
sed -i 's/GatewayPorts no/GatewayPorts yes/' "$SSHD_CONFIG"

# X11Forwarding──
sed -i 's/X11Forwarding no/X11Forwarding yes/' "$SSHD_CONFIG"

echo "[custom-init] done."
  • chmod +x config/custom-cont-init.d/01-sshd-config.sh

鍵作成

  • ssh-keygen -t ed25519 -f operator_key
  • mkdir -p config/ssh
  • mv operator_key.pub config/ssh/authorized_keys

動作確認

  • docker compose up -d
  • chmod 600 operator_key
  • ssh -i operator_key operator@localhost -p 2222

ホストの警告が出る場合は以下で無視しましょう

  • ssh -i operator_key -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null operator@localhost -p 2222

リモートSSHトンネルは以下

  • ssh -i operator_key -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -N -R 10022:localhost:22 operator@192.168.1.100 -p 2222

最後に

ログインしたいサーバに鍵を渡しましょう

2025年12月9日火曜日

linuxserver openssh-server でリモートフォーワードを有効にする方法

linuxserver openssh-server でリモートフォーワードを有効にする方法

概要

デフォルトだと無効になっています

環境

  • macOS 15.7.1
  • docker 29.1.2
  • linuxserver/openssh-server 10.0_p1-r10-ls211

compose.yaml

services:
  ssh_server:
    image: ghcr.io/linuxserver/openssh-server
    container_name: ssh_server
    ports:
      - "2222:2222"
      - "10022:10022" # SSHリモートポートフォーワードトンネル用
    environment:
      - PUID=1000
      - PGID=1000
      - PASSWORD_ACCESS=true
      - USER_NAME=test
      - USER_PASSWORD=mypass
    volumes:
      - ./custom-cont-init.d:/custom-cont-init.d:ro
    restart: unless-stopped

custom-cont-init.d/01-sshd-config.sh

#!/bin/bash
# chmod +x config/custom-cont-init.d/01-sshd-config.sh を必ず実行

echo "[custom-init] modifying sshd_config..."

# AllowAgentForwarding
sed -i 's/#AllowAgentForwarding yes/AllowAgentForwarding yes/g' /config/sshd/sshd_config

# AllowTcpForwarding
sed -i 's/AllowTcpForwarding no/AllowTcpForwarding yes/g' /config/sshd/sshd_config

# GatewayPorts
sed -i 's/GatewayPorts no/GatewayPorts yes/g' /config/sshd/sshd_config

# X11Forwarding
sed -i 's/X11Forwarding no/X11Forwarding yes/g' /config/sshd/sshd_config

echo "[custom-init] done."
  • chmod +x config/custom-cont-init.d/01-sshd-config.sh

動作確認

  • docker compose up -d
  • docker compose exec ssh_server cat /config/sshd/sshd_config

GatewayPorts などが yes になっていることを確認します

最後に

カスタムスクリプトを配置するパスが /custom-cont-init.d でした
バージョンによって異なるのでご注意ください

参考サイト

2025年12月8日月曜日

ssh できるコンテナを気軽に立てる方法

ssh できるコンテナを気軽に立てる方法

概要

Mac などで ssh サーバを立てるのが面倒な場合に docker で簡単に構築できます

環境

  • macOS 15.7.1
  • docker 29.1.2
  • linuxserver/openssh-server 10.0_p1-r10-ls211

起動

docker run -d \
  -p 2222:2222 \
  -e PASSWORD_ACCESS=true \
  -e USER_PASSWORD=mypass \
  ghcr.io/linuxserver/openssh-server

ログイン

  • ssh linuxserver.io@192.168.1.100 -p 2222

ユーザを指定する

docker run -d \
  -p 2222:2222 \
  -e PASSWORD_ACCESS=true \
  -e USER_PASSWORD=mypass \
  -e USER_NAME=test \
  ghcr.io/linuxserver/openssh-server
  • ssh test@192.168.1.100 -p 2222

最後に

ssh の検証するときには便利です

参考サイト

2025年12月7日日曜日

ただのうpろだにcurlでファイルをアップロードする

ただのうpろだにcurlでファイルをアップロードする

概要

便利

環境

  • macOS 15.7.1
  • curl 8.17.0

アップロード

  • Basic認証のパスワードは公式のものを使うのでそのまま
  • file にはアップロードするファイルをフルパスで
  • delete_pass には削除用パスワードを設定
  • もし閲覧にも制限を設けたい場合には post_password も設定する
  • 時間制限を設けるパラメータはなさそう
curl -X POST https://tadaup.jp/wp-json/custom/v1/upload \
     -u "API:AoLU ets7 2zh3 gvqc cTEe BHfp" \
     -F "file[]=@/Users/user01/Downloads/G7gvWUFbwAAv6fq.jpeg" \
     -F "title=test with curl" \
     -F "comment=" \
     -F "delete_pass=xxx" \
     -F "post_password=" \
     -F "r18=no"

レスポンス

{"success":true,"post_id":1325317,"url":"https:\/\/tadaup.jp\/070953461325317\/","source_url":"https:\/\/tadaup.jp\/CKqGMnka.jpeg","thumbnail":"https:\/\/tadaup.jp\/CKqGMnka.jpeg"}

url がアップロードしたファイルのリンクです

削除

残念ながら API はないので手動で GUI から削除パスワードを使って削除します
今後実装されるかもです

最後に

アーカイブファイルはアップロードできないのでちゅういしてください
直リンクが発行されるのでそれにアクセスすればアーカイブファイルでもアップロードできます

参考サイト

2025年12月4日木曜日

VSCode で Remote tunnel する

VSCode で Remote tunnel する

概要

操作するファイルがリモートサーバにある場合に vscode.dev という中継サーバを介してローカルで起動している VSCode で編集することができます

環境

  • Ubuntu 24.04
    • code 1.106.3
  • Windows 11
    • VSCode 1.106.3

リモートサーバ側でトンネル作成

  • curl -Lk 'https://code.visualstudio.com/sha/download?build=stable&os=cli-alpine-x64' --output vscode_cli.tar.gz
  • tar -xf vscode_cli.tar.gz

これで code というバイナリが入手できます
あとはホームディレクトリに起動して code を起動します

  • cd
  • ./code tunnel

Github Account か Microsoft Account を選択する必要があるので Github Account を選択します

起動すると例のごとくhttps://github.com/login/device にアクセスしてトークンを取得して ./code tunnel に入力してあげます

これでトンネル用の URL が発行されるのでこれをブラウザに入力してもいいのですがそれだと code-server と変わらないので VSCode からアクセスしてみます

VSCode の Remote - Tunnels 拡張をインストールする

  • 左メニューの拡張を選択
  • Remote tunnel を入力して検索
  • 「Remote - Tunnels」拡張をインストール
  • 左メニューに Remote - Tunnels 拡張のアイコンが追加されるので選択
  • さきほど ./code tunnel で起動したサーバが一覧にあるのでそれを選択
  • 初回はブラウザで OAuth の認証が発生するので ./code tunnel と同じ Github Account でログインします
  • 接続に成功すると VSCode が再起動しサーバ上のファイルが操作できるようになります

その他

  • ローカルモードに切り替える場合は切断する必要があるので左下の接続先サーバを選択し「Close Remote Connection」を選択します
  • ./code tunnel 時にはトークンのみですが VSCode 側でトンネルと VSCode を紐づける場合にはブラウザの OAuth が必要になるので VSCode が起動する環境にブラウザがありかつそのブラウザから該当の Github Account でログインできる必要があるので注意しましょう

最後に

普通はこんな使い方しないのでどうしてもローカルで開発できない場合などに使うんだと思います

もしくは Powershell などを使いたくない場合なども使うのかなと思います

参考サイト