Home [Dreamhack] Background - Computer Science
Post
Cancel

[Dreamhack] Background - Computer Science

๐Ÿ”ธ Linux Memory Layout


[Study]Memory Layout


Quiz: Linux Memory Layout

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdlib.h>
int a = 0xa;
const char b[] = "d_str";
int c;
int foo(int arg) {
  int d = 0xd;
  return 0;
}
int main()
{
  int *e = malloc(sizeof(*e));
  return 0;
}


Q1. a๊ฐ€ ์œ„์น˜ํ•˜๋Š” ์„ธ๊ทธ๋จผํŠธ๋Š” ์–ด๋””์ธ๊ฐ€?

A. ๋ฐ์ดํ„ฐ ์„ธ๊ทธ๋จผํŠธ

โ†’ ๋ฐ์ดํ„ฐ ์„ธ๊ทธ๋จผํŠธ์—๋Š” ์ปดํŒŒ์ผ ์‹œ์ ์— ์ •ํ•ด์ง€๋Š” ์ „์—ญ๋ณ€์ˆ˜ ๋ฐ ์ „์—ญ ์ƒ์ˆ˜๊ฐ€ ์œ„์น˜. a๋Š” ์ „์—ญ๋ณ€์ˆ˜.


Q2. b๊ฐ€ ์œ„์น˜ํ•˜๋Š” ์„ธ๊ทธ๋จผํŠธ๋Š” ์–ด๋””์ธ๊ฐ€?

A. ๋ฐ์ดํ„ฐ ์„ธ๊ทธ๋จผํŠธ

โ†’ b๋Š” ์ „์—ญ ์ƒ์ˆ˜.


Q3. foo๊ฐ€ ์œ„์น˜ํ•˜๋Š” ์„ธ๊ทธ๋จผํŠธ๋Š” ์–ด๋””์ธ๊ฐ€?

A. ์ฝ”๋“œ ์„ธ๊ทธ๋จผํŠธ

โ†’ ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ํ•จ์ˆ˜์˜ ์ฝ”๋“œ ๋ฐ์ดํ„ฐ๋Š” ์ฝ”๋“œ ์„ธ๊ทธ๋จผํŠธ์— ์œ„์น˜.


Q4. โ€œd_strโ€์ด ์œ„์น˜ํ•˜๋Š” ์„ธ๊ทธ๋จผํŠธ๋Š” ์–ด๋””์ธ๊ฐ€?

A. ์ฝ๊ธฐ ์ „์šฉ ๋ฐ์ดํ„ฐ (rodata) ์„ธ๊ทธ๋จผํŠธ

โ†’ ์ „์—ญ ์ƒ์ˆ˜์™€ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋Š” ๋ฐ์ดํ„ฐ ์„ธ๊ทธ๋จผํŠธ ์ค‘์—์„œ๋„ ์“ฐ๊ธฐ ๊ถŒํ•œ์ด ๋ถ€์—ฌ๋˜์ง€ ์•Š์€ ์„ธ๊ทธ๋จผํŠธ์— ์ €์žฅ๋จ.


Q5. d๊ฐ€ ์œ„์น˜ํ•˜๋Š” ์„ธ๊ทธ๋จผํŠธ๋Š” ์–ด๋””์ธ๊ฐ€?

A. ์Šคํƒ ์„ธ๊ทธ๋จผํŠธ

โ†’ ํ•จ์ˆ˜์˜ ์ธ์ž, ์ง€์—ญ๋ณ€์ˆ˜, ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ๊ฐ™์€ ์ž„์‹œ ๋ณ€์ˆ˜๋“ค์€ ํ”„๋กœ๊ทธ๋žจ ์ค‘์— ๋™์ ์œผ๋กœ ํ• ๋‹น ๋ฐ›์•„ ์Šคํƒ ์„ธ๊ทธ๋จผํŠธ์— ์ €์žฅ๋จ.


Q6. c๊ฐ€ ์œ„์น˜ํ•˜๋Š” ์„ธ๊ทธ๋จผํŠธ๋Š” ์–ด๋””์ธ๊ฐ€?

A. BSS ์„ธ๊ทธ๋จผํŠธ

โ†’ ์ดˆ๊ธฐํ™”ํ•˜์ง€ ์•Š์€ ์ „์—ญ ๋ณ€์ˆ˜์ฒ˜๋Ÿผ ์ปดํŒŒ์ผ ์‹œ์ ์— ๊ฐ’์ด ์ •ํ•ด์ง€์ง€ ์•Š๋Š” ๋ฐ์ดํ„ฐ๋Š” BSS ์„ธ๊ทธ๋จผํŠธ ์ €์žฅ๋จ. ํ”„๋กœ๊ทธ๋žจ ์‹œ์ž‘ ์‹œ 0์œผ๋กœ ์ดˆ๊ธฐํ™”.


Q7. e๋Š” ์–ด๋Š ์„ธ๊ทธ๋จผํŠธ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š”๊ฐ€?

A. ํž™ ์„ธ๊ทธ๋จผํŠธ

โ†’ malloc(), calloc()๊ณผ ๊ฐ™์€ ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ํ”„๋กœ๊ทธ๋žจ ์ค‘ ๋™์ ์œผ๋กœ ํž™ ์„ธ๊ทธ๋จผํŠธ์— ํ• ๋‹น ๋ฐ›์Œ.


๐Ÿ”ธ Computer Architecture


  • ๋ช…๋ น์–ด ์ง‘ํ•ฉ๊ตฌ์กฐ(Instruction Set Architecture, ISA) : ์ปดํ“จํ„ฐ ๊ตฌ์กฐ ์ค‘์—์„œ CPU๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ๋ช…๋ น์–ด์™€ ๊ด€๋ จ๋œ ์„ค๊ณ„. ex) ์ธํ…”์˜ x86-64 ์•„ํ‚คํ…์ฒ˜


  • ์ปดํ“จํ„ฐ ๊ตฌ์กฐ์˜ ์„ธ๋ถ€ ๋ถ„์•ผ
    • ๊ธฐ๋Šฅ ๊ตฌ์กฐ์˜ ์„ค๊ณ„
      • ํฐ ๋…ธ์ด๋งŒ ๊ตฌ์กฐ
      • ํ•˜๋ฒ„๋“œ ๊ตฌ์กฐ
      • ์ˆ˜์ •๋œ ํ•˜๋ฒ„๋“œ ๊ตฌ์กฐ
    • ๋ช…๋ น์–ด ์ง‘ํ•ฉ๊ตฌ์กฐ
      • x86, x86-64
      • ARM
      • MIPS
      • AVR
    • ๋งˆ์ดํฌ๋กœ ์•„ํ‚คํ…์ฒ˜
      • ์บ์‹œ ์„ค๊ณ„
      • ํŒŒ์ดํ”„๋ผ์ด๋‹
      • ์Šˆํผ ์Šค์นผ๋ผ
      • ๋ถ„๊ธฐ ์˜ˆ์ธก
      • ๋น„์ˆœ์ฐจ์  ๋ช…๋ น์–ด ์ฒ˜๋ฆฌ
    • ํ•˜๋“œ์›จ์–ด ๋ฐ ์ปดํ“จํŒ… ๋ฐฉ๋ฒ•๋ก 
      • ์ง์ ‘ ๋ฉ”๋ชจ๋ฆฌ ์ ‘๊ทผ


  • ํฐ ๋…ธ์ด๋งŒ ๊ตฌ์กฐ

    image

    ์—ฐ์‚ฐ๊ณผ ์ œ์–ด๋Š” ์ค‘์•™์ฒ˜๋ฆฌ์žฅ์น˜(CPU), ์ €์žฅ์€ ๊ธฐ์–ต์žฅ์น˜(memory), ์žฅ์น˜๊ฐ„์— ๋ฐ์ดํ„ฐ๋‚˜ ์ œ์–ด ์‹ ํ˜ธ ๊ตํ™˜์€ ๋ฒ„์Šค(bus)๋ฅผ ์‚ฌ์šฉ.


    • ์ค‘์•™์ฒ˜๋ฆฌ์žฅ์น˜
      • ์‚ฐ์ˆ ๋…ผ๋ฆฌ์žฅ์น˜(ALU) - ์‚ฐ์ˆ /๋…ผ๋ฆฌ ์—ฐ์‚ฐ ์ฒ˜๋ฆฌ
      • ์ œ์–ด์žฅ์น˜(Control Unit) - CPU ์ œ์–ด
      • ๋ ˆ์ง€์Šคํ„ฐ(Register) - CPU์— ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ
    • ๊ธฐ์–ต์žฅ์น˜
      • ์ฃผ๊ธฐ์–ต์žฅ์น˜ : ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ๊ณผ์ •์—์„œ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ ์ž„์‹œ ์ €์žฅ์— ์‚ฌ์šฉ
        1. ๋žจ(Random-Access Memory, RAM)
      • ๋ณด์กฐ๊ธฐ์–ต์žฅ์น˜ : ์šด์˜์ฒด์ œ, ํ”„๋กœ๊ทธ๋žจ๊ณผ ๊ฐ™์€ ๋ฐ์ดํ„ฐ ์žฅ๊ธฐ๊ฐ„ ๋ณด๊ด€์— ์‚ฌ์šฉ
        1. ํ•˜๋“œ ๋“œ๋ผ์ด๋ธŒ
        2. SSD
    • ๋ฒ„์Šค
      • ๋ฐ์ดํ„ฐ ๋ฒ„์Šค(Data Bus)
      • ์ฃผ์†Œ ๋ฒ„์Šค(Address Bus)
      • ์ œ์–ด ๋ฒ„์Šค(Control Bus)
      • ๋žœ์„ 
      • ํ”„๋กœํ† ์ฝœ


    ๋ ˆ์ง€์Šคํ„ฐ๊ฐ€ ์กด์žฌํ•จ์—๋„ CPU ์•ˆ์— ๋ ˆ์ง€์Šคํ„ฐ๊ฐ€ ํ•„์š”ํ•œ ์ด์œ 
    CPU์˜ ์—ฐ์‚ฐ์†๋„๊ฐ€ ๊ธฐ์–ต์žฅ์น˜์™€์˜ ๋ฐ์ดํ„ฐ ๊ตํ™˜์†๋„๋ณด๋‹ค ์••๋„์ ์œผ๋กœ ๋น ๋ฅด๊ธฐ ๋•Œ๋ฌธ์—, ๊ธฐ์–ต์žฅ์น˜๋งŒ์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ณ‘๋ชฉํ˜„์ƒ์ด ๋ฐœ์ƒ. ๋”ฐ๋ผ์„œ ๊ตํ™˜์†๋„๋ฅผ ๋‹จ์ถ•ํ•˜๊ธฐ ์œ„ํ•ด ๋ ˆ์ง€์Šคํ„ฐ์™€ ์บ์‹œ๋ผ๋Š” ์ €์žฅ์žฅ์น˜๋ฅผ ๋‚ด๋ถ€์— ๊ฐ–๊ณ  ์žˆ์Œ.



๐Ÿ”ถ x86 Assembly


[Study] Assembly


Quiz: x86 Assembly 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[Register]
rbx = 0x401A40

=================================

[Memory]
0x401a40 | 0x0000000012345678
0x401a48 | 0x0000000000C0FFEE
0x401a50 | 0x00000000DEADBEEF
0x401a58 | 0x00000000CAFEBABE
0x401a60 | 0x0000000087654321

=================================

[Code]
1: mov rax, [rbx+8]
2: lea rax, [rbx+8]

Q1. ๋ ˆ์ง€์Šคํ„ฐ, ๋ฉ”๋ชจ๋ฆฌ ๋ฐ ์ฝ”๋“œ๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. Code๋ฅผ 1๊นŒ์ง€ ์‹คํ–‰ํ–ˆ์„ ๋•Œ, rax์— ์ €์žฅ๋œ ๊ฐ’์€?

โ†’ 0xC0FFEE


Q2. Code๋ฅผ 2๊นŒ์ง€ ์‹คํ–‰ํ–ˆ์„ ๋•Œ, rax์— ๋“ค์–ด์žˆ๋Š” ๊ฐ’์€?

โ†’ 0x401a48


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[Register]
rax = 0x31337
rbx = 0x555555554000
rcx = 0x2

=================================

[Memory]
0x555555554000| 0x0000000000000000
0x555555554008| 0x0000000000000001
0x555555554010| 0x0000000000000003
0x555555554018| 0x0000000000000005
0x555555554020| 0x000000000003133A

==================================

[Code]
1: add rax, [rbx+rcx*8]
2: add rcx, 2
3: sub rax, [rbx+rcx*8]
4: inc rax

Q3. ๋ ˆ์ง€์Šคํ„ฐ, ๋ฉ”๋ชจ๋ฆฌ ๋ฐ ์ฝ”๋“œ๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. Code๋ฅผ 1๊นŒ์ง€ ์‹คํ–‰ํ–ˆ์„ ๋•Œ, rax์— ์ €์žฅ๋œ ๊ฐ’์€?

โ†’ 0x3133A


Q4. Code๋ฅผ 3๊นŒ์ง€ ์‹คํ–‰ํ–ˆ์„ ๋•Œ, rax์— ์ €์žฅ๋œ ๊ฐ’์€?

โ†’ 0


Q5. Code๋ฅผ 4๊นŒ์ง€ ์‹คํ–‰ํ–ˆ์„ ๋•Œ, rax์— ์ €์žฅ๋œ ๊ฐ’์€?

โ†’ 1


1
2
3
4
5
6
7
8
9
10
11
[Register]
rax = 0xffffffff00000000
rbx = 0x00000000ffffffff
rcx = 0x123456789abcdef0

==================================

[Code]
1: and rax, rcx
2: and rbx, rcx
3: or rax, rbx

Q6. ๋ ˆ์ง€์Šคํ„ฐ, ๋ฉ”๋ชจ๋ฆฌ ๋ฐ ์ฝ”๋“œ๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. Code๋ฅผ 1๊นŒ์ง€ ์‹คํ–‰ํ–ˆ์„ ๋•Œ, rax์— ์ €์žฅ๋œ ๊ฐ’์€?

โ†’ 0x1234567800000000


Q7. Code๋ฅผ 2๊นŒ์ง€ ์‹คํ–‰ํ–ˆ์„ ๋•Œ, rbx์— ์ €์žฅ๋œ ๊ฐ’์€?

โ†’ 0x000000009ABCDEF0


Q8. Code๋ฅผ 3๊นŒ์ง€ ์‹คํ–‰ํ–ˆ์„ ๋•Œ, rax์— ์ €์žฅ๋œ ๊ฐ’์€?

โ†’ 0x123456789ABCDEF0


1
2
3
4
5
6
7
8
9
10
[Register]
rax = 0x35014541
rbx = 0xdeadbeef

==================================

[Code]
1: xor rax, rbx
2: xor rax, rbx
3: not eax

Q9. ๋ ˆ์ง€์Šคํ„ฐ, ๋ฉ”๋ชจ๋ฆฌ ๋ฐ ์ฝ”๋“œ๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. Code๋ฅผ 1๊นŒ์ง€ ์‹คํ–‰ํ–ˆ์„ ๋•Œ, rax์— ์ €์žฅ๋œ ๊ฐ’์€?

โ†’ 0xEBACFBAE


Q10. Code๋ฅผ 2๊นŒ์ง€ ์‹คํ–‰ํ–ˆ์„ ๋•Œ, rax์— ์ €์žฅ๋œ ๊ฐ’์€?

โ†’ 0x35014541


Q11. Code๋ฅผ 3๊นŒ์ง€ ์‹คํ–‰ํ–ˆ์„ ๋•Œ, rax์— ์ €์žฅ๋œ ๊ฐ’์€?

โ†’ 0xCAFEBABE


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[Register]
rcx = 0
rdx = 0
rsi = 0x400000

=======================

[Memory]
0x400000 | 0x67 0x55 0x5c 0x53 0x5f 0x5d 0x55 0x10
0x400008 | 0x44 0x5f 0x10 0x51 0x43 0x43 0x55 0x5d
0x400010 | 0x52 0x5c 0x49 0x10 0x47 0x5f 0x42 0x5c
0x400018 | 0x54 0x11 0x00 0x00 0x00 0x00 0x00 0x00

=======================

[code]
1: mov dl, BYTE PTR[rsi+rcx]
2: xor dl, 0x30
3: mov BYTE PTR[rsi+rcx], dl
4: inc rcx
5: cmp rcx, 0x19
6: jg end
7: jmp 1

Q12. end๋กœ ์ ํ”„ํ•˜๋ฉด ํ”„๋กœ๊ทธ๋žจ์ด ์ข…๋ฃŒ๋œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž. ํ”„๋กœ๊ทธ๋žจ์ด ์ข…๋ฃŒ๋์„ ๋•Œ, 0x400000 ๋ถ€ํ„ฐ 0x400019๊นŒ์ง€์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋Œ€์‘๋˜๋Š” ์•„์Šคํ‚ค ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•˜๋ฉด?

1
2
3
4
5
6
7
8
9
# solve.py

mem = [0x67,0x55,0x5c,0x53,0x5f,0x5d,0x55,0x10,
       0x44,0x5f,0x10,0x51,0x43,0x43,0x55,0x5d,
       0x52,0x5c,0x49,0x10,0x47,0x5f,0x42,0x5c,
       0x54,0x11,0x00,0x00,0x00,0x00,0x00,0x00]

for i in range(0x20):
    print(chr(mem[i]^0x30), end="")

โ†’ Welcome to assembly world!


Quiz: x86 Assembly 2

[Dreamhack] Quiz: x86 Assembly 2


Quiz: x86 Assembly 3

[Dreamhack] Quiz: x86 Assembly 3

[H4CKING GAME] REV - Keygen

[Study] Stack All-in-One