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
SECCON Beginners 2017 東京 Binary編
Search
Chihiro Hasegawa
December 01, 2017
Programming
1
43
SECCON Beginners 2017 東京 Binary編
SECCON Beginners 2017 東京で行ったBinary編のスライドです。
Chihiro Hasegawa
December 01, 2017
Tweet
Share
More Decks by Chihiro Hasegawa
See All by Chihiro Hasegawa
Sysdig Secure/Falcoの活用術! ~Kubernetes基盤の脅威モデリングとランタイムセキュリティの強化~
owlinux1000
0
1.3k
mipsシェルコード入門
owlinux1000
0
880
Other Decks in Programming
See All in Programming
C++でシェーダを書く
fadis
6
4.1k
型付き API リクエストを実現するいくつかの手法とその選択 / Typed API Request
euxn23
7
2.1k
EventSourcingの理想と現実
wenas
6
2.3k
シールドクラスをはじめよう / Getting Started with Sealed Classes
mackey0225
4
640
Amazon Qを使ってIaCを触ろう!
maruto
0
390
Tuning GraphQL on Rails
pyama86
2
1.2k
JavaでLチカしたい! / JJUG CCC 2024 Fall LT
nhayato
0
140
Why Jakarta EE Matters to Spring - and Vice Versa
ivargrimstad
0
920
Boost Performance and Developer Productivity with Jakarta EE 11
ivargrimstad
0
1.8k
【Kaigi on Rails 2024】YOUTRUST スポンサーLT
krpk1900
1
330
距離関数を極める! / SESSIONS 2024
gam0022
0
250
Click-free releases & the making of a CLI app
oheyadam
2
110
Featured
See All Featured
A Tale of Four Properties
chriscoyier
156
23k
Raft: Consensus for Rubyists
vanstee
136
6.6k
What's in a price? How to price your products and services
michaelherold
243
12k
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
Adopting Sorbet at Scale
ufuk
73
9.1k
Intergalactic Javascript Robots from Outer Space
tanoku
269
27k
StorybookのUI Testing Handbookを読んだ
zakiyama
26
5.2k
Optimizing for Happiness
mojombo
376
70k
How STYLIGHT went responsive
nonsquared
95
5.2k
Typedesign – Prime Four
hannesfritz
40
2.4k
Building Better People: How to give real-time feedback that sticks.
wjessup
364
19k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Transcript
4&$$0/#FHJOOFST౦ژ #JOBSZฤ © SECCON2017 All Rights Reserved
४උ ԋशϑΝΠϧΛμϯϩʔυ͍ͯͩ͘͠͞ IUUQGJMFTCJOBSZCJOBSZ[JQ λʔϛφϧΛ։͖μϯϩʔυͨ͠[JQΛղౚ͍ͯͩ͘͠͞ DEd%PXOMPBET VO[JQCJOBSZ[JQ DECJOBSZ ©
SECCON2017 All Rights Reserved 2 仮想環境内で⾏って下さい
ࣗݾհ جຊใ ୩ ઍ ͪͻΖ େֶ ˠ ࣾձਓ $5'νʔϜ 5PLZP8FTUFSOT
͖ $ݴޠɼ3VCZɼ&NBDT © SECCON2017 All Rights Reserved 3
࣍ ͡Ίʹ dόΠφϦղੳͱd ϓϩάϥϜ࣮ߦͷΈ ΞηϯϒϦݴޠೖ Y
͓ΘΓʹ d$5'ʹ͚ͯd © SECCON2017 All Rights Reserved 4
5 © SECCON2017 All Rights Reserved ͡Ίʹ
͡Ίʹ ຊߨٛͷඪ ˘όΠφϦղੳͱԿ͔ΛΔ ˘ΞηϯϒϦݴޠ Y ΛಡΊΔΑ͏ʹͳΔ ˘͜ͷߨٛͷޙʹ͋Δ$5'Ͱ3FWFSTJOHΛղ͘ʂ © SECCON2017 All
Rights Reserved 6
͡Ίʹ όΠφϦͱ • ʮʯͱʮʯͰදݱ͞ΕΔσʔλܗࣜͷ͜ͱ όΠφϦղੳͱ • ओʹ࣮ߦϑΝΠϧΛղੳͯ͠ಈ࡞ݪཧΛղ໌͢Δ • $5'ͷδϟϯϧͰʮ3FWʯʮ3FWFSTJOHʯ͕૬ ©
SECCON2017 All Rights Reserved 7
͡Ίʹ $5'ʹ͓͚Δ3FWFSTJOHͷྫ • -JOVYͷ࣮ߦϑΝΠϧͷղੳ • 8JOEPXTͷ࣮ߦϑΝΠϧͷղੳ • "OESPJEΞϓϦͷղੳ • ͦͷଞ༷ʑͳόΠτίʔυͷղੳ
© SECCON2017 All Rights Reserved 8
͡Ίʹ $5'ʹ͓͚Δ3FWFSTJOHͷྫ • -JOVYͷ࣮ߦϑΝΠϧͷղੳ • 8JOEPXTͷ࣮ߦϑΝΠϧͷղੳ • "OESPJEΞϓϦͷղੳ • ͦͷଞ༷ʑͳόΠτίʔυͷղੳ
© SECCON2017 All Rights Reserved 9
͡Ίʹ &-'όΠφϦ &-'&YFDVUBCMF-JOLBCMF'PSNBU • ଟ͘ͷ-JOVYܥɼ#4%ܥͷ04Ͱ࣮ߦϑΝΠϧܗࣜͱͯ͠ ࠾༻͞Ε͍ͯΔ © SECCON2017 All Rights
Reserved 10 コンピュータはどのようにして プログラムを実⾏しているのか?
ϓϩάϥϜ࣮ߦͷΈ 11 © SECCON2017 All Rights Reserved
ϓϩάϥϜ࣮ߦͷΈ $16 $FOUSBM1SPDFTTJOH6OJU • ػցޠΛಡΈɼ໋ྩ࣮ͣͭߦ͢Δ Ϩδελ • $16ʹ͋Δ࡞ۀ༻ྖҬ • ૣ͍ɼখ༰ྔ
ϝϞϦ • σʔλΛهԱ͢Δஔ • ͍ɼେ༰ྔ © SECCON2017 All Rights Reserved 12 アドレス 値 0x804860 0xdeadbeef 0x804864 Rev! 0x804868 0x90909090 メモリのイメージ図
ϓϩάϥϜ࣮ߦͷΈ © SECCON2017 All Rights Reserved 13 ίϯύΠϧ 55 89
e5 51 83 ec 04 83 ec 0c 68 c0 84 04 08 ・・・・ 機械語 ソースコード(C⾔語) ࣮ߦϑΝΠϧ
ϓϩάϥϜ࣮ߦͷΈ ػցޠਓؒʹಡΉ͜ͱ͕ग़དྷͳ͍ Ͱɼ׳Εͯ͘Δͱɾɾɾʁ © SECCON2017 All Rights Reserved 14 55
89 e5 51 83 ec 04 83 ec 0c 68 c0 84 04 08 ・・・・ 機械語
ΞηϯϒϦݴޠೖ Y 15 © SECCON2017 All Rights Reserved
ΞηϯϒϦݴޠೖ Y ΞηϯϒϦݴޠ • ػցޠͱରͰରԠͨ͠ݴޠ • ਓ͕ؒಡΈ͍͢ • ໋ྩ Φϖίʔυ
Ҿ Φϖϥϯυ ه๏ • *OUFMه๏ɼ"55ه๏ © SECCON2017 All Rights Reserved 16 b8 0a 00 00 00 mov eax, 0xa 機械語 アセンブリ⾔語 *例外あり
ΞηϯϒϦݴޠೖ Y *OUFMه๏ ᶃసૹઌσΟεςΟωʔγϣϯΦϖϥϯυ ᶄసૹݩιʔεΦϖϥϯυ © SECCON2017 All Rights Reserved
17 mov eax ,0xa オペコード オペランド ① ② → 付録A 10/16進数, ASCIIコード早⾒表
ΞηϯϒϦݴޠೖ Y ٯΞηϯϒϧ • ػցޠΛΞηϯϒϦݴޠʹม͢Δ͜ͱ © SECCON2017 All Rights Reserved
18 b8 0a 00 00 00 mov eax, 0xa 機械語 アセンブリ⾔語
ΞηϯϒϦݴޠೖ Y ԋश όΠφϦΛ࣮ߦͯ͠ΈΔ DINPE V Y PCKEVNQ@TBNQMF PCKEVNQ@TBNQMF )FMMP8PSME
© SECCON2017 All Rights Reserved 19 ˡ࣮ߦݖݶΛ༩͢Δ
ΞηϯϒϦݴޠೖ Y ԋश PCKEVNQίϚϯυΛͬͯٯΞηϯϒϧͯ͠ΈΔ PCKEVNQ EPCKEVNQ@TBNQMF .JOUFM © SECCON2017 All
Rights Reserved 20
ΞηϯϒϦݴޠೖ Y *%"EFNP Ծڥͷը໘ࠨԼͷΞΠίϯΛબ ʮͦͷଞʯ͔Β*%"EFNPΛىಈͯ͠OFXΛબ ղੳ͢Δ࣮ߦϑΝΠϧΛબ ʮ&-'GPSJOUFMʯΛ֬ೝͯ͠0, © SECCON2017 All
Rights Reserved 21
ΞηϯϒϦݴޠೖ Y Ϩδελ • ܭࢉ݁ՌͳͲͷҰ࣌อଘʹར༻ • ൚༻Ϩδελ FBY FCY FDY
FEY FTJ FEJ ࣗ༝ʹͬͯྑ͍Ϩδελ • ಛघϨδελ FCQ FTQ FJQ ಛघతͷͨΊͷϨδελ © SECCON2017 All Rights Reserved 22
ΞηϯϒϦݴޠೖ Y Ϩδελ • ܭࢉ݁ՌͳͲͷҰ࣌อଘʹར༻ • ൚༻Ϩδελ FBY FCY FDY
FEY FTJ FEJ ࣗ༝ʹͬͯྑ͍Ϩδελ • ಛघϨδελ FCQ FTQ FJQ ಛघతͷͨΊͷϨδελ © SECCON2017 All Rights Reserved 23
ΞηϯϒϦݴޠೖ Y ൚༻Ϩδελ FBY • ܭࢉؔͷΓʹར༻ FCY • ܭࢉʹར༻ FDY
• ܭࢉϧʔϓॲཧͷΧϯλʹར༻ FEY • ܭࢉ ಛʹࢉআࢉͰසൟ ʹར༻ © SECCON2017 All Rights Reserved 24 ಛघϨδελ FTQ • ݱࡏελοΫͷ্ͷΞυϨεΛࢦ͢ FCQ • ؔͰελοΫͷఈͷΞυϨεΛࢦ͢ FJQ • ࣍ʹ࣮ߦ͢Δ໋ྩͷΞυϨεΛࢦ͢ ※必ずしもこれらの意味を準拠するとは限らない
ΞηϯϒϦݴޠೖ Y ࠓճѻ͏໋ྩҰཡ • NPW • MFB • BEETVC •
JNVMJEJW • BOEPSYPSOPU • JODEFD © SECCON2017 All Rights Reserved 25 • DNQ • KNQܥ • QVTI • QPQ • DBMM • MFBWF • SFU 3.1 代⼊命令 3.2 算術命令 3.3 条件分岐命令 3.4 スタック操作命令 3.5 関数呼び出しの命令
ೖ໋ྩ © SECCON2017 All Rights Reserved 26
ΞηϯϒϦݴޠೖ Y NPW໋ྩ .07F • ͷҠಈΛߦ͏໋ྩ • సૹݩͷมԽ͠ͳ͍ సૹݩͷΛసૹઌʹίϐʔ •
ϝϞϦʹ֨ೲ͞ΕͨʹΞΫηε͢Δʹɼ<>Λར༻ NPW FBY FCY FCYͷΛFBYʹҠಈ NPW FBY %803%153<FCY> FCYͷΛΞυϨεͱͯ͠ߟ͑ɼͦ͜ʹ֨ೲ͞Ε͍ͯΔΛFBYʹҠಈ NPW %803%153<FBY > FCY FBY ͷΛΞυϨεͱͯ͠ߟ͑ɼͦ͜ʹFCYͷΛҠಈ %803%%PVCMF8PSE CZUF ୯ҐͰΞΫηε͢Δ͜ͱΛද͢ © SECCON2017 All Rights Reserved 27
ΞηϯϒϦݴޠೖ Y MFB໋ྩ -PBE &GGFDUJWF"EESFTT • సૹݩͷΞυϨεΛܭࢉͯ͠ɼసૹઌʹ֨ೲ • ΞυϨεܭࢉͷ໋ྩ͕ͩɼࢉज़ԋࢉʹར༻͞ΕΔ߹ •
NPW໋ྩͱҟͳΓܭࢉͨ͠ΞυϨεʹ֨ೲ͞Εͨ ΞΫηε͢ΔͷͰͳ͍ MFBFBY <FCY > FCYͷʹΛͨ͠ΛFBYʹ֨ೲ͢Δ ˞FCYͷࣗมԽ͠ͳ͍ © SECCON2017 All Rights Reserved 28
ΞηϯϒϦݴޠೖ Y ԋश ࠷ޙʹ֨ೲ͞Ε͍ͯΔFBYͷΛٻΊΑ͏ʂ © SECCON2017 All Rights Reserved 29
プログラム eax ebx mov eax, 0x1 lea ebx, [eax + 0x5] mov eax, ebx ?
ΞηϯϒϦݴޠೖ Y ղ ࠷ޙʹ֨ೲ͞Ε͍ͯΔFBYͷΛٻΊΑ͏ʂ © SECCON2017 All Rights Reserved 30
プログラム eax ebx mov eax, 0x1 1 lea ebx, [eax + 0x5] 1 6 mov eax, ebx 6 6
ࢉज़໋ྩ © SECCON2017 All Rights Reserved 31
ΞηϯϒϦݴޠೖ Y BEETVC໋ྩ "%%JUJPO 46#USBDUJPO • Ճࢉݮࢉ BEEFBY Y FBY
ͨ͠ΛFBYʹ֨ೲ͢Δ JODEFD໋ྩ */$SFNFOU %&$SFNFOU • Λ૿͢ݮΒ͢ JOD FBY FBY ͨ͠ΛFBYʹ֨ೲ͢Δ ্هͷBEEFBY Yͱಉ͡ಇ͖ © SECCON2017 All Rights Reserved 32
ΞηϯϒϦݴޠೖ Y JNVMJEJW໋ྩ T*HOFE .VMUJQMZ %JWJEF • ࢉআࢉ JNVM FBY
FCY FBY ✕ FCYͷ݁ՌΛFBYʹ֨ೲ͢Δ ˞ܭࢉ݁ՌʹΑͬͯFEYʹ֨ೲ͞ΕΔ JEJW FCY FBY × FCYͷΛFBYɼ༨ΓΛFEYʹ֨ೲ͢Δ © SECCON2017 All Rights Reserved 33
ΞηϯϒϦݴޠೖ Y BOEPSYPSOPU໋ྩ • ཧੵཧഉଞతཧ൱ఆ YPS FBY FBY FBYͱFBYͷഉଞతཧΛFBYʹ֨ೲ͢Δ ಉ͡ಉ࢜ͷഉଞతཧʹͳΔ
ˠϨδελͷॳظԽʹར༻Մೳ © SECCON2017 All Rights Reserved 34 mov eax, 0x0 = b8 00 00 00 00 xor eax, eax = 31 c0 余談:xorによる0初期化の⽅が機械語が短い!
ΞηϯϒϦݴޠೖ Y ԋश ࠷ޙʹ֨ೲ͞Ε͍ͯΔFBYͷΛٻΊΑ͏ʂ © SECCON2017 All Rights Reserved 35
プログラム eax ebx mov eax, 0x1 mov ebx, 0x2 add eax, 0x5 dec eax imul eax, ebx ?
ΞηϯϒϦݴޠೖ Y ղ ࠷ޙʹ֨ೲ͞Ε͍ͯΔFBYͷΛٻΊΑ͏ʂ © SECCON2017 All Rights Reserved 36
プログラム eax ebx mov eax, 0x1 1 mov ebx, 0x2 1 2 add eax, 0x5 6 2 dec eax 5 2 imul eax, ebx 10 2
݅ذ໋ྩ © SECCON2017 All Rights Reserved 37
ΞηϯϒϦݴޠೖ Y ݅ذ • DNQ໋ྩͱKNQܥ໋ྩͷΈ߹ΘͤʹΑΓ࣮ݱ • ݅ذΛ͏͜ͱͰϧʔϓॲཧΛ࣮ݱՄೳ DNQ໋ྩ $P.1BSF •
Φϖϥϯυಉ࢜Λൺֱ͠ɼ݁ՌʹΑͬͯϑϥάϨδελΛߋ৽͢Δ KNQܥ໋ྩ • Φϖϥϯυ͕ࢦ͢ΞυϨεʹ੍ޚΛҠ͢ • DNQ໋ྩͷ݁ՌʹΑΓ੍ޚΛҠ͔͢Ͳ͏͔ΛذͰ͖Δ © SECCON2017 All Rights Reserved 38
ΞηϯϒϦݴޠೖ Y KNQܥ໋ྩ Ұྫ • KNQ+V.1 ݅ແ͠ • KF+VNQJG&RVBM ͍͠
• KOF +VNQJG/PU&RVBM ͘͠ͳ͍ • KMF +VNQJG-FTTPS&RVBM ҎԼ • KHF +VNQJG(SFBUFS&RVBM Ҏ্ • KM +VNQJG-FTT ະຬ • KH +VNQJG(SFBUFS ΑΓେ͖͍ • KB+VNQJG"CPWF ΑΓେ͖͍ ˞ූ߸ແ͠ͱͯ͠ߟ͑Δ • KCF +VNQJG#FMPXPG&RVBM ҎԼ ˞ූ߸ແ͠ͱͯ͠ߟ͑Δ © SECCON2017 All Rights Reserved 39
ΞηϯϒϦݴޠೖ Y ԋश ࠷ޙʹ֨ೲ͞Ε͍ͯΔFBYͷΛٻΊΑ͏ʂ © SECCON2017 All Rights Reserved 40
アドレス プログラム eax ecx 0x8048060 mov eax, 0x1 0x8048065 mov ecx, 0x0 0x804806a jmp 0x804806c 0x804806c add eax, ecx 0x804806e inc ecx 0x804806f cmp eax, 0x3 0x8048072 jl 0x804806c
ΞηϯϒϦݴޠೖ Y ղ ࠷ޙʹ֨ೲ͞Ε͍ͯΔFBYͷΛٻΊΑ͏ʂ © SECCON2017 All Rights Reserved 41
アドレス プログラム eax ecx 0x8048060 mov eax, 0x1 1 0x8048065 mov ecx, 0x0 1 0 0x804806a jmp 0x804806c 1 0 0x804806c add eax, ecx 1 0 0x804806e inc ecx 1 1 0x804806f cmp eax, 0x3 1 1 0x8048072 jl 0x804806c 1 1 ①
ΞηϯϒϦݴޠೖ Y ղ ࠷ޙʹ֨ೲ͞Ε͍ͯΔFBYͷΛٻΊΑ͏ʂ © SECCON2017 All Rights Reserved 42
アドレス プログラム eax ecx 0x8048060 mov eax, 0x1 1 0x8048065 mov ecx, 0x0 1 0 0x804806a jmp 0x804806c 1 0 0x804806c add eax, ecx 2 1 0x804806e inc ecx 2 2 0x804806f cmp eax, 0x3 2 2 0x8048072 jl 0x804806c 2 2 ②
ΞηϯϒϦݴޠೖ Y ղ ࠷ޙʹ֨ೲ͞Ε͍ͯΔFBYͷΛٻΊΑ͏ʂ © SECCON2017 All Rights Reserved 43
アドレス プログラム eax ecx 0x8048060 mov eax, 0x1 1 0x8048065 mov ecx, 0x0 1 0 0x804806a jmp 0x804806c 1 0 0x804806c add eax, ecx 4 2 0x804806e inc ecx 4 3 0x804806f cmp eax, 0x3 4 3 0x8048072 jl 0x804806c 4 3
ελοΫૢ࡞໋ྩ © SECCON2017 All Rights Reserved 44
ΞηϯϒϦݴޠೖ Y QVTIQPQ໋ྩ 164)101 • ελοΫΛૢ࡞͢Δ໋ྩ • QVTI͕ελοΫʹΛੵΉ • ࣗಈతʹFTQ͕ݮΔ
• QPQ͕ελοΫ͔ΒΛϨδελʹҠ͢ • ࣗಈతʹFTQ͕૿͑Δ © SECCON2017 All Rights Reserved 45
ελοΫ -*'0 -BTU*O'JSTU0VU ࠷ޙʹೖΕͨͷ͕࠷ॳʹग़Δ © SECCON2017 All Rights Reserved 46
10 push 0xa esp
ελοΫ -*'0 -BTU*O'JSTU0VU ࠷ޙʹೖΕͨͷ͕࠷ॳʹग़Δ © SECCON2017 All Rights Reserved 47
10 push 0xa esp 10
ελοΫ -*'0 -BTU*O'JSTU0VU ࠷ޙʹೖΕͨͷ͕࠷ॳʹग़Δ © SECCON2017 All Rights Reserved 48
ecx pop ecx 10 esp
ελοΫ -*'0 -BTU*O'JSTU0VU ࠷ޙʹೖΕͨͷ͕࠷ॳʹग़Δ © SECCON2017 All Rights Reserved 49
ecx pop ecx 10 esp = 10
ؔݺͼग़໋͠ྩ © SECCON2017 All Rights Reserved 50
ΞηϯϒϦݴޠೖ Y ؔ • ϓϩάϥϜ௨ৗɼػೳຖʹؔʹׂ͞Ε͍ͯΔ DBMM໋ྩ $"-- • ؔͷݺͼग़࣌͠ʹར༻ •
࣮ߦ࣌ʹDBMM໋ྩͷ࣍ͷ໋ྩ͕֨ೲ͞Ε͍ͯΔΞυϨεΛελοΫʹQVTI • ͔ؔΒݟΔͱ͜ͷΞυϨεΓઌͳͷͰϦλʔϯΞυϨεͱݺΕΔ • ؔͷҾ༧ΊελοΫʹੵΜͰ͓͘ SFU໋ྩ 3&5VSO • ͔ؔΒݩͷؔʹΔࡍʹར༻ • DBMM໋ྩͰελοΫʹอଘ͓͍ͯͨ͠ϦλʔϯΞυϨεʹ੍ޚΛ͢ © SECCON2017 All Rights Reserved 51
ΞηϯϒϦݴޠೖ Y ྫ ༩͑ΒΕͨ̎ͭͷB CΛͨ͠Λฦؔ͢BEE © SECCON2017 All Rights Reserved
52 main: sub esp, 0x8 mov DWORD PTR [esp], 1 mov DWORD PTR [esp+0x4], 2 call add mov ebx, eax (以降省略) add: push ebp mov ebp, esp sub esp, 16 mov edx, DWORD PTR [ebp+0x8] mov eax, DWORD PTR [ebp+0xc] add eax, edx mov DWORD PTR [ebp-0x4], eax leave ret ※ スライドの都合上DWORD PTRを以後省略
ΞηϯϒϦݴޠೖ Y © SECCON2017 All Rights Reserved 53 sub esp,
0x8 mov [esp], 1 mov [esp+0x4], 2 call add mov ebx, eax esp ebp N byte ※スライドの都合上ebpとespがすごく近くなっていますが, 実際にはもっと離れているものとして考えてください
ΞηϯϒϦݴޠೖ Y © SECCON2017 All Rights Reserved 54 esp 8byte
sub esp, 0x8 mov [esp], 1 mov [esp+0x4], 2 call add mov ebx, eax esp+0x8 ebp ҾΛελοΫʹੵΉͨΊʹFTQΛখ͘͢͞Δ
ΞηϯϒϦݴޠೖ Y © SECCON2017 All Rights Reserved 55 1 4byte
esp esp+0x4 esp+0x8 sub esp, 0x8 mov [esp], 1 mov [esp+0x4], 2 call add mov ebx, eax ebp ̍ͭͷҾΛελοΫʹੵΉ
ΞηϯϒϦݴޠೖ Y © SECCON2017 All Rights Reserved 56 1 4byte
esp 2 4byte sub esp, 0x8 mov [esp], 1 mov [esp+0x4], 2 call add mov ebx, eax esp+0x4 esp+0x8 ebp ※push 2 → push 1でも同様の処理を実現可能 ̎ͭͷҾΛελοΫʹੵΉ
ΞηϯϒϦݴޠೖ Y © SECCON2017 All Rights Reserved 57 1 4byte
esp リターンアドレス 4byte esp+0xc DBMM໋ྩɼݺͼग़͠ޙͷ໋ྩͷΞυϨεΛελοΫʹQVTIͯ͠BEEʹKNQ sub esp, 0x8 mov [esp], 1 mov [esp+0x4], 2 call add mov ebx, eax esp+0x8 esp+0x4 2 4byte ebp
ΞηϯϒϦݴޠೖ Y © SECCON2017 All Rights Reserved 58 1 4byte
esp esp+4 esp+8 リターンアドレス 4byte esp+12 ݺͼग़͠ݩͷؔͷFCQΛελοΫʹQVTIͯ͠อଘ add: push ebp mov ebp, esp sub esp, 16 mov edx, [ebp+8] mov eax, [ebp+12] add eax, edx mov [ebp-4], eax leave ret main関数のebp 4byte esp+16 2 4byte ebp
ΞηϯϒϦݴޠೖ Y © SECCON2017 All Rights Reserved 59 1 4byte
esp esp+4 esp+8 リターンアドレス 4byte esp+12 ݺͼग़͠ݩͷؔͷFCQΛελοΫʹQVTIͯ͠อଘ main関数のebp 4byte esp+16 ebp add: push ebp mov ebp, esp sub esp, 16 mov edx, [ebp+8] mov eax, [ebp+12] add eax, edx mov [ebp-4], eax leave ret 2 4byte
add: push ebp mov ebp, esp sub esp, 16 mov
edx, [ebp+8] mov eax, [ebp+12] add eax, edx mov [ebp-4], eax leave ret ΞηϯϒϦݴޠೖ Y © SECCON2017 All Rights Reserved 60 esp 1 4byte 2 4byte リターンアドレス 4byte 呼び出し元の関数のebp 4byte ebp 16byte ebp+4 ebp+8 ebp+12 BEEؔͰར༻͢Δ ྖҬ ྖҬ֬อͷͨΊFTQΛখ͘͢͞Δ
ΞηϯϒϦݴޠೖ Y © SECCON2017 All Rights Reserved 61 esp 1
4byte 2 4byte リターンアドレス 4byte main関数のebp 4byte ebp 16byte ebp+4 ebp+8 ebp+12 省略 3 4byte ebp-4 add: push ebp mov ebp, esp sub esp, 16 mov edx, [ebp+8] mov eax, [ebp+12] add eax, edx ; eax = 1 + 2 mov [ebp-4], eax leave ret
ΞηϯϒϦݴޠೖ Y © SECCON2017 All Rights Reserved 62 esp 1
4byte 2 4byte リターンアドレス 4byte main関数のebp 4byte ebp 16byte ebp+4 ebp+8 ebp+12 3 4byte ebp-4 ؔʹ͞ΕͨҾɼ <FCQ >ͰୈҰҾ <FCQ >ͰୈೋҾ ɾ ɾ ɾ ؔͷมɼ <FCQ> <FCQY>ɾɾɾ ͷΑ͏ʹΞΫηεͰ͖Δ
ΞηϯϒϦݴޠೖ Y © SECCON2017 All Rights Reserved 63 esp add:
(省略) leave ret 1 4byte 2 4byte リターンアドレス 4byte main関数のebp 4byte 16byte leave命令は以下の動き mov esp, ebp pop ebp 3 4byte ebp
ΞηϯϒϦݴޠೖ Y © SECCON2017 All Rights Reserved 64 esp add:
(省略) leave ret 1 4byte 2 4byte リターンアドレス 4byte main関数のebp 4byte 16byte leave命令は以下の動き mov esp, ebp pop ebp ebp 3 4byte
ΞηϯϒϦݴޠೖ Y © SECCON2017 All Rights Reserved 65 esp add:
(省略) leave ret 1 4byte 2 4byte リターンアドレス 4byte main関数のebp 4byte 16byte ret命令は以下のような動き pop eip ※ pop eip命令は存在しない eip 3 4byte
ΞηϯϒϦݴޠೖ Y ࠓճѻ໋ͬͨྩҰཡ • NPW • MFB • BEETVC •
JNVMJEJW • BOEPSYPSOPU • JODEFD © SECCON2017 All Rights Reserved 66 • DNQ • KNQܥ • QVTI • QPQ • DBMM • MFBWF • SFU 3.1 代⼊命令 3.2 算術命令 3.3 条件分岐命令 3.4 スタック操作命令 3.5 関数呼び出しの命令 → 付録F 参考書籍/pdf等
͓ΘΓʹ 67 © SECCON2017 All Rights Reserved
͓ΘΓʹ 3FWFSTJOHͷղ͖ํखॱ ϑΝΠϧΛμϯϩʔυ͢Δ ݖݶΛ༩͢Δ DINPE V Y ରϑΝΠϧ
࣮ߦͯ͠ڍಈΛ֬ೝ PCKEVNQ*%"ͳͲͰٯΞηϯϒϧͯ͠ղੳ © SECCON2017 All Rights Reserved 68
͓ΘΓʹ ͜Ε͔Β্ͷεςοϓʹਐΉͨΊʹ ݸਓతͳҙݟ • $$ ݴޠʹ׳ΕΔ ˠ ( • HEC
ͳ Ͳ σ ό ο Ψ τ Ϩ ʔ α ʔ Λ ༻ ͍ ͨ ಈ త ղ ੳ Λ ֶ Ϳ ඞ ਢ ˠ $ % • 3FWFSTJOH҉߸ֶɼΞϧΰϦζϜͷཁૉ͕ʹ ࠞͬͯ͘͟Δ͜ͱ͕ଟ͍ͷͰɼ෯͍ࣝΛ͚Δ • ෆඞཁͳॲཧಡΈඈ͢׳Ε © SECCON2017 All Rights Reserved 69
͓ΘΓʹ © SECCON2017 All Rights Reserved 70 ຊߨٛͷඪ ☑όΠφϦղੳͱԿ͔ΛΔ ☑
ΞηϯϒϦݴޠ Y ΛಡΊΔΑ͏ʹͳΔ ˘ ͜ͷߨٛͷޙʹ͋Δ$5'Ͱ3FWFSTJOHΛղ͘ʂ
͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ © SECCON2017 All Rights Reserved 71
© SECCON2017 All Rights Reserved
"ਐ "4$**ίʔυૣݟද © SECCON2017 All Rights Reserved 73 ⾒⽅: 10進数の65は,16進数だと0x41,ASCIIコードに変換するとAという⽂字
# 1ZUIPOʹΑΔࢉज़ԋࢉ λʔϛφϧͰʮQZUIPOʯͱೖྗͯ͠ΠϯλϓϦλΛىಈ͢Δ • ਐ 㱻 ਐม • ࢛ଇԋࢉ
• ཧԋࢉ c? ͕Մೳ • "4$**ม DISؔɼPSEؔ © SECCON2017 All Rights Reserved 74
$HEC HECQFEB ͷ͍ํ HEC • (/6ͷσόοΨ • QFEB1ZUIPOʹΑΔHECͷ֦ு ਪ σόοΨ
• ରతʹϓϩάϥϜΛղੳ͢Δπʔϧ ϒϨʔΫϙΠϯτ • σόοΨ͕ೝࣝͯ͠ϓϩάϥϜΛࢭΊͯ͘ΕΔҹ σόοΨΛ͏ϝϦοτ • ϨδελมͷΛݟΕΔ • ϒϨʔΫϙΠϯτΛுͬͯɼͱ͋Δ໋ྩ·Ͱࣗಈ࣮ߦ © SECCON2017 All Rights Reserved 75
$HEC HECQFEB ͷ͍ํ © SECCON2017 All Rights Reserved 76 レジスタ
プログラム スタック
$HEC HECQFEB ͷ͍ํ • ىಈ HEC ରϑΝΠϧ໊ • NBJOؔʹϒϨʔΫϙΠϯτΛுΔ HECQFEBCSFBLNBJO
• ΞυϨεʹϒϨʔΫϙΠϯτΛுΔ ྫYʹϒϨʔΫϙΠϯτுΔ HECQFEBCSFBL Y • ໋̍ྩ࣮ͣͭߦ ؔͷதʹೖΒͳ͍ HECQFEBOFYU • ໋ྩ࣮ͣͭߦ ؔͷதʹೖΔ HECQFEBTUFQ • ϒϨʔΫϙΠϯτ·ͰࣗಈͰଓ͚Δ HECQFEBDPOUJOVF © SECCON2017 All Rights Reserved 77 • ࣮ߦ HECQFEBSVO • NBJOؔͰࣗಈతʹࢭ·Δ࣮ߦ HECQFEBTUBSU
$HEC HECQFEB ͷ͍ํ • ϝϞϦʹ֨ೲ͞Ε͍ͯΔΛදࣔ ྫCZUFΛਐͰදࣔ HECQFEBYXY ΞυϨε • ϝϞϦʹ֨ೲ͞Ε͍ͯΔจࣈྻΛදࣔ
HECQFEBYTΞυϨε • ϨδελͷதΛม͑Δ ྫFBYΛʹॻ͖͑ HECQFEBTFUFBY • ϝϞϦͷதΛม͑Δ ྫYGGGGEΛʹॻ͖͑ HECQFEBTFU\JOU^YGGGGE • ελοΫΛݟΔ ྫߦදࣔ HECQFEBUFMFTDPQF © SECCON2017 All Rights Reserved 78
%MUSBDFTUSBDFͷ͍ํ MUSBDFίϚϯυ • ϥΠϒϥϦؔͷݺͼग़͠ΛΘ͔Γ͍͢ܗࣜͰදࣔ MUSBDF ࣮ߦ͢ΔϑΝΠϧ © SECCON2017 All Rights
Reserved 79
%MUSBDFTUSBDFͷ͍ํ TUSBDFίϚϯυ • γεςϜίʔϧͷݺͼग़͠ΛΘ͔Γ͍͢ܗࣜͰදࣔ TUSBDF ࣮ߦ͢ΔϑΝΠϧ © SECCON2017 All Rights
Reserved 80 システムコール: OSの持つ機能を 呼び出すための機構
&Yͱ • *OUFMͷͱݺΕΔCJUͷ$16͕ॳY$16 • ͷޙܧʹɼͳͲ͕͋Δ • ಛʹCJUͷ$16ͱͳ͍ͬͯͯɼࠓճͷߨٛ ͜ͷ$16ͷ͜ͱΛऔΓѻ͍ͬͯΔ • ࠓYͱݺΕΔCJU$16͕ओྲྀʹ
ͪ͜Β$5'Ͱසग़ͳͷͰֶͿ͜ͱΛਪ © SECCON2017 All Rights Reserved 81
'ࢀߟॻ੶QEG • ηΩϡϦςΟίϯςετνϟϨϯδϒοΫ • ηΩϡϦςΟίϯςετͷͨΊͷ$5'ू • σόοΨʹΑΔYϓϩάϥϜղੳೖʲYରԠ൛ʳ • ͨͷ͍͠όΠφϦͷา͖ํ •
ϦόʔεΤϯδχΞϦϯάόΠϒϧ • ࣗ࡞ΤϛϡϨʔλͰֶͿYΞʔΩςΫνϟ • #*/"3:)"$,4 • େ݂Ξηϯϒϥೖ • *"ΠϯςϧΞʔΩςΫνϟιϑτΣΞσϕϩούʔζɾϚχϡΞϧ © SECCON2017 All Rights Reserved 82
($ݴޠͷසग़ؔ QSJOUGؔ • ୈҰҾʹ༩͑ΒΕͨΞυϨεʹ֨ೲ͞ΕͨจࣈΛग़ྗ͢Δ TDBOGؔ • จࣈΛೖྗ͢ΔɽୈೋҾʹ͕֨ೲ͞ΕΔɽ TUSMFOؔ • ୈҰҾʹ༩͑ΒΕͨΞυϨεʹ֨ೲ͞Εͨจࣈྻͷ͞Λฦ͢
BUPJؔ • ୈҰҾʹ༩͑ΒΕͨΞυϨεʹ֨ೲ͞ΕͨจࣈྻΛ ʹม͢Δ © SECCON2017 All Rights Reserved 83
)ٯΞηϯϒϧΛಡΉίπ •DBMM໋ྩͷޙFBYʹண͢Δ • ؔͷΓFBYʹ֨ೲ͞ΕΔ •DBMM໋ྩͷલʹQVTI͍ͯ͠Δʹண͢Δ • ؔͷҾελοΫʹੵΉ •όΠφϦதͷ"4$**จࣈʹண͢Δ • Y
֯εϖʔε dYF d ҹࣈՄೳͳจࣈ © SECCON2017 All Rights Reserved 84