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
知られざるSerialize
Search
Yuichi Sugiyama
PRO
July 31, 2019
Technology
1
440
知られざるSerialize
Yuichi Sugiyama
PRO
July 31, 2019
Tweet
Share
More Decks by Yuichi Sugiyama
See All by Yuichi Sugiyama
サイボウズ と Garoon と The PHP Foundation と 私 / Cybozu and Garoon and The PHP Foundation and me
oogfranz
PRO
1
450
可能な限り確実にmkdirを成功させるには / Make mkdir
oogfranz
PRO
0
450
サイボウズ #Garoon 開発チームの 「 完成度低いの歓迎LT大会 」 PHPerKaigi出張版 / Low quality LT in PHPerKaigi 2023
oogfranz
PRO
0
510
20年ものの巨大プロダクトをKubernetesに移行している話 後日談/Garoon on Kubernetes after talk
oogfranz
PRO
0
510
20年ものの巨大プロダクトをKubernetesに移行している話/Garoon on Kubernetes
oogfranz
PRO
0
390
PHPアプリケーションだってモニタリングしたい / Monitoring PHP application
oogfranz
PRO
1
570
効果的な静的解析の CI導入パターンを求めて / Great static analysis with CI
oogfranz
PRO
3
3.5k
Dev-meets-Ops
oogfranz
PRO
1
920
GitHub力の低い僕でも、 OSSコントリビュートできたワケ / GitHub Power
oogfranz
PRO
1
520
Other Decks in Technology
See All in Technology
第1回 国土交通省 データコンペ参加者向け勉強会③- Snowflake x estie編 -
estie
0
130
データプロダクトの定義からはじめる、データコントラクト駆動なデータ基盤
chanyou0311
2
310
Making your applications cross-environment - OSCG 2024 NA
salaboy
0
190
OCI Network Firewall 概要
oracle4engineer
PRO
0
4.1k
ドメインの本質を掴む / Get the essence of the domain
sinsoku
2
150
The Role of Developer Relations in AI Product Success.
giftojabu1
0
120
B2B SaaSから見た最近のC#/.NETの進化
sansantech
PRO
0
810
Application Development WG Intro at AppDeveloperCon
salaboy
0
190
AGIについてChatGPTに聞いてみた
blueb
0
130
Taming you application's environments
salaboy
0
190
CysharpのOSS群から見るModern C#の現在地
neuecc
2
3.3k
ノーコードデータ分析ツールで体験する時系列データ分析超入門
negi111111
0
410
Featured
See All Featured
Designing for humans not robots
tammielis
250
25k
Agile that works and the tools we love
rasmusluckow
327
21k
What's new in Ruby 2.0
geeforr
343
31k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2.1k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
Happy Clients
brianwarren
98
6.7k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
31
2.7k
Statistics for Hackers
jakevdp
796
220k
Designing for Performance
lara
604
68k
Designing on Purpose - Digital PM Summit 2013
jponch
115
7k
We Have a Design System, Now What?
morganepeng
50
7.2k
Transcript
ΒΕ͟ΔSerialize @PHP Study Cybozu Inc, Yuichi Sugiyama
Who am I • ਿࢁ ༞Ұ @oogFranz • αΠϘζ5ΤϯδχΞ •
େاۀ͚άϧʔϓΣΞ Λ࡞ͬͯΔʢPHP 7.2) • ෳۀδϟζϛϡʔδγϟϯ@MASHݭָஂ
serialize(), unserialize()ͬͯ·͔͢ʁ
serialize() όϦϕϯϦ $a = ["hoge", 3, true]; $a[] = &$a;
echo serialize($a); # a:4:{i:0;s:4:"hoge";i:1;i:3;i:2;b:1;i:3;a:4: {i:0;s:4:"hoge";i:1;i:3;i:2;b:1;i:3;R:5;}} • PHPͷΛ෮ݩՄೳͳจࣈྻʹม͢Δɻ • ΫϥεͷΠϯελϯεͰ͋ͬͯมՄೳʂ • DBʹอଘͰ͖ΔʂͬͨͶʂ
serialize() όϦϕϯϦ $a = ["hoge", 3, true]; $a[] = &$a;
echo serialize($a); # a:4:{i:0;s:4:"hoge";i:1;i:3;i:2;b:1;i:3;a:4: {i:0;s:4:"hoge";i:1;i:3;i:2;b:1;i:3;R:5;}} • PHPͷΛ෮ݩՄೳͳจࣈྻʹม͢Δɻ • ΫϥεͷΠϯελϯεͰ͋ͬͯมՄೳʂ • DBʹอଘͰ͖ΔʂͬͨͶʂ
Կ͕ʁ • serialize() ʹ࣮ͦΜͳʹͳ͍ • ٯؔͷunserialize()ʹ͍͔ͭ͘ͷ੬ऑੑ͕͋Δ
PHP Object Injection ʢPOIʣ • ҎԼͷ͕݅ຬͨ͞Ε͍ͯΔ߹ɺ։ൃऀ͕ҙਤ͍ͯ͠ͳ͍ ίʔυΛ߈ܸऀ͕࣮ߦͰ͖Δɻ • __wakeup(), __destruct()ͳͲͷϚδοΫϝιουʹ
෭࡞༻ͷ͋Δίʔυ͕ॻ͔Ε͍ͯΔ • unserialize()ͷҾʹϢʔβʔ͔Βͷೖྗ͕͞ΕΔ
ϚδοΫϝιου • ಛఆͷ݅ͰࣗಈͰൃಈ͢Δຐ๏ • serialize()ͷ࣮ߦ࣌ʹϚδοΫϝιουͷ __sleep() ͕ ͋ͬͨΒࣗಈͰ࣮ߦ͞ΕΔ • unserialize()࣌ʹɺϚδοΫϝιουͷ
__wakeup() ͕ ͋ͬͨΒࣗಈͰ࣮ߦ͞ΕΔ
αϯϓϧ class POIExample{ private $filename; public function __destruct() { unlink($this->filename);
} } unserialize('O:10:"POIExample":1:{s: 20:"POIExamplefilename";s:8:"test.php";}')
αϯϓϧ class POIExample{ private $filename; public function __destruct() { unlink($this->filename);
} } unserialize('O:10:"POIExample":1:{s: 20:"POIExamplefilename";s:8:"test.php";}') ߈ܸऀ͕ҙͷϑΝΠϧͷআ͕Մೳʂ
POIͷ͕͜͜ා͍ • ࣮ࡍʹunserializeʹ͞ΕΔ͜ͱ͕ͳ͍ΫϥεͰ͋ͬͯɺ auto_loadͷΈ͕͋Δͱ࣮ߦ͞Εͯ͠·͏ɻ • privateͷϝϯόมʹҙͷΛೖՄೳ setterΛհ͞ͳ͍ͷͰɺߟྀ͕͍͠ɻ • ԾʹsetterͰσΟϨΫτϦτϥόʔαϧΛ͍Ͱ͍ͯແҙຯ
• json_encode(), json_decode()Λ͏ɻ ˠΦϒδΣΫτΛγϦΞϥΠζɾσγϦΞϥΠζ͢ΔͷͰͳ͚Ε ेɻ • allowed_classesΦϓγϣϯΛ͏ ˠࢦఆ͞ΕͨΫϥεͷΦϒδΣΫτʹ͔͠ ɹม͞Εͳ͍͜ͱ͕อূ͞ΕΔ POIΛ͙ʹʁ
allowed_classes Φϓγϣϯ $serialized_str = 'O:10:"POIExample":1:{s:20:"POIExamplefilename";s: 8:"test.php";}'; $result = unserialize($serialized_str, ['allowed_classes'
=> false]); var_dump($result); /* object(__PHP_Incomplete_Class)#2 (2) { ["__PHP_Incomplete_Class_Name"]=> string(10) "POIExample" ["filename":"POIExample":private]=> string(8) "test.php" } */ echo serialize($result); // O:10:"POIExample":1:{s:20:"POIExamplefilename";s:8:"test.php";}
allowed_classesͰकΕΔͷ • ڐՄ͞Ε͍ͯͳ͍ΫϥεΛunserialize()͢Δͱɺ__PHP_Incomplete_Class ʹม͞ΕΔ • ͦͷΦϒδΣΫτΛ͏Ұserialize͢Δͱ ʢͳ͔ͥʣݩʹΔɻ • σϑΥϧτ true
(ΨόΨό)ͳͷͰ໌ࣔతʹࢦఆ͢Δඞཁ͕͋Δ • falseΛࢦఆ͢Εɺ͋ΒΏΔΫϥεΛڋઈ͢Δ
__PHP_Incomplete_ClassʹͳͬͨΒΤϥʔʹ͍ͨ͠ • ةͳ͍ͷ͕͖͍ͬͯͯΔͷͰΤϥʔʹ͍ͨ͠ • get_class Λͯ͠ɺ__PHP_Incomplete_Class ͔ΛௐΔ • unserialize_callback_func ݺͼग़͞Εͳ͍ͷͰҙɻ
allowed_classes͕͋Εunserializeͬ ͯྑ͍ʁ https://twitter.com/nikita_ppv/status/895571304325062656
allowed_classes͕͋Εunserializeͬ ͯྑ͍ʁ https://www.php.net/manual/ja/function.unserialize.php
unserializeͷ੬ऑੑ੬ऑੑͱΈͳ͞Εͳ͍ • ϝϞϦपΓͳͲͷෆ۩߹Ͱ͋ͬͯ੬ऑੑͱΈͳ͞Εͳ͍ Մೳੑ͕͋Δ • ΧδϡΞϧʹϝϞϦपΓͷෆ۩߹͕ใࠂ͞Ε͍ͯΔ
serializeͨ͠ΛDBʹೖΕΔ͜ͱʹ͍ͭͯ • SQL Ξϯνύλʔϯͷ 5ষ EAVʢΤϯςΟςΟɾΞτϦϏϡʔτɾόϦϡʔʣ ʹͳͬͯΔՄೳੑ͕ߴ͍ɻ
MySQLͱserialize • MySQLͷtextʹγϦΞϥΠζσʔλΛೖΕ͍ͯΔ߹ɺ จࣈ͕Φʔόʔ͢Δͱɺ్தͰΒΕͯ͠·͍ɺ ෮ݩͰ͖ͳ͘ͳΔɻʢStrict SQL Modeʹґଘʣ • MySQL 5.7͔ΒJSONܕ͕͑ΔΑ͏ʹͳͬͨͷͰɺ
ಈతεΩʔϚ͕Ͳ͏ͯ͠ඞཁͳ߹JSONܕΛߟ͑ͨํ͕ Αͦ͞͏ɻ
·ͱΊ • serialize(), unserialize()Λ͍ͬͯͳ͍ͷͳΒɺͦΕ͕Ұ൪ • ͍ͬͯ͠ΔͷͳΒɺPOIΛ͙ͨΊɺϢʔβʔೖྗσʔλ͕ೖͬͯ͜ͳ͍͜ͳ͍͜ͱΛ͔֬Ί Α͏ • ՄೳͰ͋Εjson_encode, json_decodeʹஔ͖͔͑Α͏
• ͦΕແཧͳΒɺallowed_classesΦϓγϣϯͰPOI͠ͷ͝͏ • DBʹอଘ͠ͳ͍Α͏ʹ͠·͠ΐ͏ • ϝϞϦपΓͷෆ۩߹͋ΔͷͰஔ͖͑Λݕ౼͠·͠ΐ͏ɻ