Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
iptables を使って、 2回に1回 ping が落ちる サーバを作る / Use i...
Search
do-su-0805
September 25, 2019
Programming
1.5k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
iptables を使って、 2回に1回 ping が落ちる サーバを作る / Use iptables to create a server that pings once every two times
ref :
https://do-su-dairyquestions.hatenablog.com/entry/2018/12/18/080000
do-su-0805
September 25, 2019
More Decks by do-su-0805
See All by do-su-0805
元SREのCREが伝えたい、Mackerelをもっと活用するための実践Tips集 / Mackerel Drink Up #11 do-su-0805
dosu0805
0
350
とある高専生の進路例/Example of a certain college student
dosu0805
1
1.3k
AWS IAM で MFA 有効でないユーザを awscli + Mackerel で監視する / Monitoring users who are not MFA enabled with AWS IAM with awscli + Mackerel
dosu0805
0
340
Webアプリケーション初心者が ISUCON 4 予選を通じてパフォーマンスの測定改善をした話 / newcomers of web application improvements in performance measurement through ISUCON 4 qualifying
dosu0805
0
1.4k
Other Decks in Programming
See All in Programming
手動確認はもう限界 〜XCUITestでCustom URL Schemeの遷移を起動種別ごとに自動テストする〜 / Testing Custom URL Schemes with XCUITest
otouto
0
310
Kiroで創り、AgentCoreで繋ぐ!AWSで実践する「AI-DLC」から「AIエージェント統合」までの最新地図
licux
4
700
GKE で Pod の見方を変えたら、スケールアウト時の挙動を真に捉えられた話
stkk
0
130
そのリトライ、死んだコネクションを使い回していませんか ── GoのHTTPクライアントとHTTP/2を実プロダクト障害から学び直す
myus4a
0
150
Everything will be SERVERLESS — 信じて運用した10年の経験値 / Everything Will be Serverless — Lessons Learned from 10 Years of Operational Experience
seike460
PRO
1
290
標準パッケージに uuid が追加された 背景から見る Go らしい意思決定 / go_127_uuid_decision
convto
5
7.5k
20260914 AIエージェント時代のPlatform Engineering LLM基盤とプロダクトの責務境界線
kanfab1
7
2.1k
技術的負債の返済は、AI時代の複利で効く投資 — 経営としての意思決定とその遂行
curekoshimizu
0
1.5k
すこし踏み込む CancellationToken
htkym
2
590
Vibes Containers 〜AIで変わるコンテナ設計と運用〜
tkikuc
3
550
速習iPhone Duo対応
yuukiw00w
1
660
Vue Fes Japan 2026 タイムテーブル徹底解説
448jp
1
250
Featured
See All Featured
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
120
120k
Mobile First: as difficult as doing things right
swwweet
225
10k
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
1.1k
[RailsConf 2023] Rails as a piece of cake
palkan
59
7k
A Soul's Torment
seathinner
8
3.6k
Building the Perfect Custom Keyboard
takai
2
880
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
330
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
420
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
870
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.8k
Ruling the World: When Life Gets Gamed
codingconduct
0
340
Transcript
で 回に 回 が落ちる サーバを作る 株式会社はてな サービスシステム開発本部 システムプラットフォーム部 SRE do-su-0805
今日話すこと • ヘッダ ヘッダと について話をします • 別例などは以下で紹介 ◦ https://do-su-dairyquestions.hatenablog.com/entry/2018/12/18/080000
アジェンダ • について • が届かない範囲 • オプションの紹介 • 実例 パケットが
回に 回落ちるルール • 実演
について 簡単に言うと、こんな感じのことができる • パケットのフィルタリング/ロギング ◦ tcp / port 80 あての通信は
192.168.0.0/16 からのみ ◦ udp / port 53 あての通信をログに残す • NAT
について iptablesは、Linuxに実装されたパケットフィルタリングおよびネット ワークアドレス変換 (NAT) 機能であるNetfilter(複数のNetfilterモ ジュールとして実装されている)の設定を操作するコマンドのこと。 Netfilterは、いわゆるファイアウォールやルータとしての役割を果 たす。 https://ja.wikipedia.org/wiki/Iptables より
で使えるルール対象 • proto ◦ tcp / udp / icmp •
src / dist IP • port ◦ 80 (http) / 22 (ssh) • etc… IP ヘッダの情報 (+ tcp / ttl などの拡張) が使える
のルール指定で届かない範囲 • レイヤーのパケットヘッダ情報(一部除く) ◦ ヘッダで言うならシーケンス番号や確認応答番号 ◦ ヘッダで言うならコードやエコー要求・応答時のシー ケンスナンバー ◦ ヘッダで言うなら
セグメント長 ◦
オプションの紹介 • 自分でルールを書いて、パケット解析を行いルール適用 ◦ iptables -m u32 --u32 "6&0xFF=0x1 &&
0>>22&0x3C@0>>24=0x08 && 0>>22&0x3C@6>>16&0x01=0x01" -A INPUT -j DROP` ▪ ”6& … =0x01” までのルールに該当する INPUT パケットを DROP • ICMP echo 要求で seq が 奇数の時だけ DROP する ▪ $ ping 192.168.1.7 PING 192.168.1.7 (192.168.1.7) 56(84) bytes of data. 64 bytes from 192.168.1.7: icmp_seq=2 ttl=64 time=0.329 ms 64 bytes from 192.168.1.7: icmp_seq=4 ttl=64 time=0.325 ms 64 bytes from 192.168.1.7: icmp_seq=6 ttl=64 time=0.311 ms 64 bytes from 192.168.1.7: icmp_seq=8 ttl=64 time=0.320 ms
実例 パケットが 回に 回落ちるルール • iptables -m u32 --u32 "6&0xFF=0x1
&& 0>>22&0x3C@0>>24=0x08 && 0>>22&0x3C@6>>16&0x01=0x01" -A INPUT -j DROP を読み解きます
実例 パケットが 回に 回落ちるルール • iptables -m u32 --u32 "6&0xFF=0x1
&& まで ◦ iptables -m u32 --u32 : u32 拡張を呼び出す ◦ 6 : IPヘッダ先頭から 7 ~ 10byte までの 4 byte を呼び出す ▪ この 4 byte 列 = 1 オクテット ▪ "0 なら 1byte 目 ~ 4 byte 目までの 1 オクテットが呼び出される ◦ &0xFF=0x1 : 呼び出した 1オクテットに対して、マスクをかけて比較 ▪ つまり最後の 1byte のみ = 10 byte 目だけにします。ここは プロトコル番号 ▪ 抜き出した結果が 0x1 (ICMP) であれば真となる比較式 ◦ && : 左辺が真であれば継続してルール解釈
実例 パケットが 回に 回落ちるルール ネットワークエンジニアとして 「TCP/IP - IPとは」https://www.infraexpert.com/study/tcpip1.html
実例 パケットが 回に 回落ちるルール • 6 : IPヘッダ先頭から 7 ~
10byte までの 4 byte を呼び出す ◦ 「フラグ」から「プロトコル」までが取得される 資料中段図 : ネットワークエンジニアとして 「TCP/IP - IPとは」https://www.infraexpert.com/study/tcpip1.html より
実例 パケットが 回に 回落ちるルール • &0xFF=0x1 : 呼び出した 1オクテットに対して、
マスクをかけて比較 ◦ 最後の 1byte のみ = 10 byte 目 = プロトコル番号 ◦ 抜き出した結果が 0x1 (ICMP) であれば真となる比較式 ▪ ここが 0d6 だと TCP. 0d41 だと UDP だとわかる。
実例 パケットが 回に 回落ちるルール • 0>>22&0x3C@0>>24=0x08 &&まで ◦ 0>>22&0x3C :
1 ~ 4byte を呼び出し、22bit シフトして AND ▪ バージョン/ヘッダ長/サービス種別の先頭 2bit が取得できる ▪ その結果に 0x3C を AND することで、「ヘッダ長の 4 倍」が手に入る • 「32bit 列がいくつ分か」が入っているので、 4 倍すると byte になる ◦ @0>>24=0x08 : 上記結果を 0 とし 4byte 取得しシフト & 比較 ▪ ヘッダ長@ すると IP ヘッダを抜け出し、 ICMP ヘッダ先頭から 4byte 取得 ▪ >>24 し、先頭 1byte だけ(ICMPヘッダのtype) にし、0x08(エコー要求)か比較
実例 パケットが 回に 回落ちるルール • 0>>22&0x3C :1 ~ 4byte を呼び出し
22bit シフト して AND 資料中段図 : ネットワークエンジニアとして 「TCP/IP - IPとは」https://www.infraexpert.com/study/tcpip1.html より
実例 パケットが 回に 回落ちるルール • 0>>22 すると • &0x3C すると、ヘッダ長
* 4 された値 (5*4 = 20)
実例 パケットが 回に 回落ちるルール • @0>>24=0x08 : 20 byte 目を
0 とし 4byte 取得 しシフト & 比較 ◦ IP ヘッダ長を抜けた = ICMP ヘッダの先頭から 4byte ◦ ◦ ◦ 24 bit シフトすると
実例 パケットが 回に 回落ちるルール • 0>>22&0x3C@6>>16&0x01=0x01まで ◦ 0>>22&0x3C までは先ほどと同様のため省略 •
@6>>16&0x01=0x01 ◦ @6 : ICMP ヘッダの 7byte ~ 10byte 目を取得 ◦ >>16 :16bit シフトしシーケンス番号を取得。最後に奇偶判定
実例 パケットが 回に 回落ちるルール • 今までの説明をまとめると、こんな感じ ◦ 6&0xFF=0x1 && :
ICMP パケットかつ ◦ 0>>22&0x3C@0>>24=0x08 && : タイプがエコー要求かつ ◦ 0>>22&0x3C@6>>16&0x01=0x01 : シーケンス番号が奇数なら • 最後に -A INPUT -j DROP ◦ 上記のようなパケットをサーバが「受けたら」、「DROP する」
まとめ • の 拡張を紹介した • 複雑に見えるルールを一つ一つ追ってみた ◦ 6&0xFF=0x1 && :
ICMP パケットかつ ◦ 0>>22&0x3C@0>>24=0x08 && : タイプがエコー要求かつ ◦ 0>>22&0x3C@6>>16&0x01=0x01 : シーケンス番号が奇数なら ◦ -A INPUT -j DROP : そんなパケットをサーバが受けたら廃棄する