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
Shimpei Makimoto
August 18, 2018
Technology
360
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
580
グリッチについて話をします。 #wedding_s
makimoto
3
590
The Pragmatic Glitch (#rubykaigi 2013 LT)
makimoto
4
3.4k
chiyahoya.me
makimoto
0
350
Recipes of Recipe Search
makimoto
5
1.6k
Other Decks in Technology
See All in Technology
チームで実践する AI-DLC 思考の軌跡を残すチェックポイント設計
belongadmin
0
3.1k
MIERUNE JCT 発表資料「宇宙から伊能忠敬ごっこ」
syuchimu
0
200
トークン数だけでは測れない — Claude Code 組織展開の効果検証から学んだこと
makikub
0
140
protovalidate-es を導入してみた
bengo4com
0
160
新規事業を牽引する技術選定 〜フルスタックTypeScript開発の実践事例〜
nullnull
3
380
エンジニアリング戦略の作り方 / Crafting Engineering Strategy
iwashi86
16
5.3k
GoとSIMDとWasmの今。
askua
3
520
AWSシリコン最前線 〜AI時代のチップ選択を読み解く〜
htokoyo
2
300
AmazonRoute 53ではじめてのドメイン取得!HTTPS化までの道のりを整理してみた
usanchuu
3
120
タクシーアプリ『GO』の実践的データ活用
mot_techtalk
3
180
あなたの AI ワークスペースに、 専門コーダーを連れてくる - Amazon Quick Desktop 最新情報
kawaji_scratch
1
120
実装は速くなった、レビューはどうする? ― 自身のレビューをAIで再現させるサーヴァントエンジニアリングのすゝめ / Implementation got faster. So what about reviews? — An invitation to Servant Engineering: Recreating your own code reviews with AI
nrslib
7
4.4k
Featured
See All Featured
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
370
A Soul's Torment
seathinner
6
2.9k
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
330
Mobile First: as difficult as doing things right
swwweet
225
10k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.8k
[SF Ruby Conf 2025] Rails X
palkan
2
1.1k
Visualization
eitanlees
152
17k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
55k
Deep Space Network (abreviated)
tonyrice
0
170
30 Presentation Tips
portentint
PRO
1
320
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
380
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.5k
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)