Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
JPEG を壊す話 / About JPEG corruption
Shimpei Makimoto
August 18, 2018
Technology
0
170
JPEG を壊す話 / About JPEG corruption
Kyoto.なんか #4
Shimpei Makimoto
August 18, 2018
Tweet
Share
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.2k
Training for New Graduates in Cookpad (Shitei Todan 2015)
makimoto
0
5.9k
How to read code
makimoto
0
1k
Glitch, OSS activities, and so on (Oedo RubyKaigi 04 #oedo04)
makimoto
5
920
The Pragmatic Animated GIF in Ruby (a RubyConf 2013 lightning talk)
makimoto
0
450
グリッチについて話をします。 #wedding_s
makimoto
3
520
The Pragmatic Glitch (#rubykaigi 2013 LT)
makimoto
4
2.7k
chiyahoya.me
makimoto
0
290
Recipes of Recipe Search
makimoto
5
1.2k
Other Decks in Technology
See All in Technology
hey BOOK
heyinc
26
290k
DMMプラットフォーム ゼロから始めるKubernetes運用 課題と改善
pospome
0
400
Istioを活用したセキュアなマイクロサービスの実現/Secure Microservices with Istio
ido_kara_deru
3
420
聊聊 Cgo 的二三事
david74chou
0
330
塩漬けにしているMySQL 8.0.xxをバージョンアップしたくなる、ここ数年でのMySQL 8.0の改善点 / MySQL Update 202208
yoshiakiyamasaki
1
670
Amazon Comprehendで始める感情分析
46ta
0
190
ECS on EC2 で Auto Scaling やってみる!
sayjoy
1
210
セキュアなTerraformの使い方 ~ 機密情報をコードに含めず環境構築するにはどうしたらいいの?
harukasakihara
9
1.5k
JAWS-UG 朝会 #36 登壇資料
takakuni
1
550
cobra は便利になっている
nwiizo
0
140
2022 COSCUP - GKE Backend Cluster 除雷分享
brentchang
0
120
Red Hat Partner Training Portal のご紹介 / Red Hat Partner Training Portal Introduction
rhpej
0
110
Featured
See All Featured
Mobile First: as difficult as doing things right
swwweet
213
7.6k
Embracing the Ebb and Flow
colly
73
3.4k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
15
980
Designing on Purpose - Digital PM Summit 2013
jponch
106
5.7k
Making the Leap to Tech Lead
cromwellryan
113
7.4k
It's Worth the Effort
3n
172
26k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
21
1.4k
Side Projects
sachag
450
37k
Web Components: a chance to create the future
zenorocha
303
40k
Fantastic passwords and where to find them - at NoRuKo
philnash
27
1.6k
Pencils Down: Stop Designing & Start Developing
hursman
113
9.8k
For a Future-Friendly Web
brad_frost
166
7.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)