Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
JPEG を壊す話 / About JPEG corruption
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Shimpei Makimoto
August 18, 2018
Technology
370
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
JPEG を壊す話 / About JPEG corruption
Kyoto.なんか #4
Shimpei Makimoto
August 18, 2018
More Decks by Shimpei Makimoto
See All by Shimpei Makimoto
How to Work with Legacy Ruby on Rails Applications in Treasure Data
makimoto
1
1.5k
Training for New Graduates in Cookpad (Shitei Todan 2015)
makimoto
0
6.2k
How to read code
makimoto
0
1.7k
Glitch, OSS activities, and so on (Oedo RubyKaigi 04 #oedo04)
makimoto
5
1.4k
The Pragmatic Animated GIF in Ruby (a RubyConf 2013 lightning talk)
makimoto
0
590
グリッチについて話をします。 #wedding_s
makimoto
3
590
The Pragmatic Glitch (#rubykaigi 2013 LT)
makimoto
4
3.4k
chiyahoya.me
makimoto
0
360
Recipes of Recipe Search
makimoto
5
1.6k
Other Decks in Technology
See All in Technology
もう一度考える SRE チームの作り方・育て方 / Rethinking SRE #1: Building and Growing SRE Teams
rrreeeyyy
6
1k
ガバメントクラウドでのランサムウェア対策
techniczna
2
1k
GitHub CopilotのFinOps- AI CreditのObservabilityと価値を生むためのエージェント設計
yuriemori
0
140
Master Dataグループ紹介資料
sansan33
PRO
1
4.8k
サイバー捜査員研修(前半)
nomizone
1
1.9k
新しい SLO が良い感じにハマっている話
z63d
5
2.2k
QAエンジニア起点で進める、SmartHRにおける信頼性向上について
kaomi_wombat
1
120
Invisible to AI? Making TYPO3 Sites Quotable by AI Search Systems
wolfgangwagner
0
170
Data Hubグループ 紹介資料
sansan33
PRO
0
3.2k
取引先から届く 「セキュリティチェックシート」の読み解き方
kamadamakoto
0
140
Eight Engineering Unit 紹介資料
sansan33
PRO
3
8.1k
Flutterをカメラで動かしたかった話
sony
1
130
Featured
See All Featured
Practical Orchestrator
shlominoach
191
12k
What's in a price? How to price your products and services
michaelherold
247
13k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.9k
New Earth Scene 8
popppiees
3
2.5k
Embracing the Ebb and Flow
colly
88
5.1k
Thoughts on Productivity
jonyablonski
76
5.3k
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
1
460
エンジニアに許された特別な時間の終わり
watany
108
250k
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.5k
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
930
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.3k
Transcript
JPEG を壊す話 Shimpei Makimoto (@makimoto) Kyoto.なんか #4 (18th Aug 2018)
誰? - Shimpei Makimoto - Software Engineer in Tokyo -
自分でも厳密な所属がわからない - https://twitter.com/makimoto - https://github.com/makimoto https://twitter.com/makimoto/status/1027473102437904386
JPEG - Joint Photographic Experts Group https://en.wikipedia.org/wiki/JPEG - 厳密には JPEG
は画像圧縮手法のことを指す - 人間たちが JPEG と呼んでいるのは大体のケースで JPEG を用いて画像をファイ ルに保存するための形式である JFIF (JPEG File Interchange Format) のことを指 す - グリッチの文脈ではもっともロバストな画像フォーマットの1つ
いろいろな画像形式でグリッチを試すデモ - iTerm2 と Docker Hub の makimoto/glitch-playground イメージを使う -
docker run -v /path/to/data:/data \ -i -t makimoto/glitch-playground
何これ? - https://hub.docker.com/r/makimoto/glitch-playground/ - https://github.com/makimoto/glitch-playground - ImageMagick と各種画像処理ライブラリをインストールした Docker コンテナの中
で画像を変換して sed して表示させるスクリプトを動かしている - iTerm2 で動く imgcat を使って表示させている https://www.iterm2.com/documentation-images.html
ロバスト #とは - JPEG 形式の画像は割と壊れない - PNG とか結構すぐ壊れる - GIF
とか WebP とかは割と良い
何故壊れるのか - 壊してはいけない場所を壊す - 雑なヒューリスティックをする手もある https://github.com/makimoto/glitch/blob/9b5b9b3c59fbcddae15e40e2e4cd522964b8749e/commands.go#L98-L102 - (割とちゃんと動くんだけど) 雑すぎ -
ちゃんと画像フォーマットを理解した上で壊せば壊れない
JPEG の仕様とか - https://www.w3.org/Graphics/JPEG/ - https://www.w3.org/Graphics/JPEG/itu-t81.pdf - 画像圧縮手法としての JPEG の仕様
- https://www.w3.org/Graphics/JPEG/jfif3.pdf - 画像フォーマット (JFIF) の仕様 - 読むのが辛い場合は Go のライブラリのコードを追うと良いかも - https://golang.org/pkg/image/jpeg/
xxd -g1 lena.jpg 00000000: ff d8 ff e0 00 10
4a 46 49 46 00 01 01 00 00 01 ......JFIF...... 00000010: 00 01 00 00 ff db 00 43 00 03 02 02 02 02 02 03 .......C........ 00000020: 02 02 02 03 03 03 03 04 06 04 04 04 04 04 08 06 ................ 00000030: 06 05 06 09 08 0a 0a 09 08 09 09 0a 0c 0f 0c 0a ................ 00000040: 0b 0e 0b 09 09 0d 11 0d 0e 0f 10 10 11 10 0a 0c ................ 00000050: 12 13 12 10 13 0f 10 10 10 ff db 00 43 01 03 03 ............C... 00000060: 03 04 03 04 08 04 04 08 10 0b 09 0b 10 10 10 10 ................ 00000070: 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 ................ 00000080: (snip)
xxd -g1 lena.jpg 00000000: ff d8 ff e0 00 10
4a 46 49 46 00 01 01 00 00 01 ......JFIF...... 00000010: 00 01 00 00 ff db 00 43 00 03 02 02 02 02 02 03 .......C........ 00000020: 02 02 02 03 03 03 03 04 06 04 04 04 04 04 08 06 ................ 00000030: 06 05 06 09 08 0a 0a 09 08 09 09 0a 0c 0f 0c 0a ................ 00000040: 0b 0e 0b 09 09 0d 11 0d 0e 0f 10 10 11 10 0a 0c ................ 00000050: 12 13 12 10 13 0f 10 10 10 ff db 00 43 01 03 03 ............C... 00000060: 03 04 03 04 08 04 04 08 10 0b 09 0b 10 10 10 10 ................ 00000070: 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 ................ 00000080: (snip) SOI APP0 DQT DQT
JPEG - JPEG (JFIF) 内で \xff から始まる値はマーカーとして扱われる - \xff\xd8 -
SOI (Start of Image) - \xff\xe0 - APP0 (Reserved for application segments) - \xff\xdb - DQT (Define quantization table(s))
https://www.w3.org/Graphics/JPEG/itu-t81.pdf
マーカーのプレフィックスを壊すと即死 - ruby -e 'print ARGF.read.b.gsub("\xff".b, "\xfe".b)' lena.jpg > out.jpg
マーカーの後の2バイトの値を壊しても即死 - ruby -e 'print ARGF.read.b.gsub(/(\xff.)[^\xff]./n, "\\1\x00\x00".b)' lena.jpg > out.jpg
- マーカーに続く2バイトは大体各セクションの長さを示すので破壊されると画像が開 けなくなる https://www.w3.org/Graphics/JPEG/itu-t81.pdf
jpegsan.gem - https://rubygems.org/gems/jpegsan - https://github.com/makimoto/jpegsan - ~/misc に転がっていた jpeg.rb を整えてさっき
push した
使用例 [3] pry(main)> jpeg.data[4] => #<struct Jpegsan::Jpeg::JpegMarker code_suffix="\xC0", symbol="SOF0", description="non-differential,
Huffman coding - Baseline DCT", data_sequence="\x00\x11\b\x02\x00\x02\x00\x03\x01\x11\x00\x02\x11\x01\x03\ x11\x01">
使用例 [4] pry(main)> jpeg.data[4].sequence_in_hex => "00 11 08 02 00
02 00 03 01 11 00 02 11 01 03 11 01"
使用例
None
他の例 - に の量子化テーブルを適用
他の例 - に のハフマン符号テーブルを適用
まとめ - 画像フォーマットを考慮して画像を破壊すると安全に壊せるし、逆に完全に壊さな い感じにもできる - フォーマットを意識してやるのはそれなりにコストがかかるのでできるだけ自動化し たいですね
About JPEG corruption Shimpei Makimoto (@makimoto) Kyoto.なんか #4 (18th Aug
2018)