# Ćwiczenia 10, grupa śr. 17-19, 26 kwietnia 2023
###### tags: `ASK23` `ćwiczenia` `pwit`
## Deklaracje
Gotowość rozwiązania zadania należy wyrazić poprzez postawienie X w odpowiedniej kolumnie! Jeśli pożądasz zreferować dane zadanie (co najwyżej jedno!) w trakcie dyskusji oznacz je znakiem ==X== na żółtym tle.
**UWAGA: Tabelkę wolno edytować tylko wtedy, gdy jest na zielonym tle!**
:::danger
| | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| ----------------------:| --- | --- | --- | --- | --- | --- | --- | --- | --- |
| Kamila Goszcz | | x | |==x==| x | | | | |
| Mateusz Materek | X | X | | X |==X==| | X | | |
:::
:::info
**Uwaga:** Po rozwiązaniu zadania należy zmienić kolor nagłówka na zielony.
:::
## Zadanie 1
:::success
Autor: Mateusz Materek
:::

```
gcc -ggdb -Og -Wall -g0 -fno-common -S -o data.s data.c
-ggdb -> Produce debugging information for use by GDB. This means to use the most expressive format available (DWARF, stabs, or the native format if neither of those are supported), including GDB extensions if at all possible.
-Og -> Optimize debugging experience. -Og should be the optimization level of choice for the standard edit-compile-debug cycle, offering a reasonable level of optimization while maintaining fast compilation and a good debugging experience.
-Wall -> This enables all the warnings about constructions that some users consider questionable, and that are easy to avoid (or modify to prevent the warning), even in conjunction with macros.
-fno-common -> The default is -fno-common, which specifies that the compiler places uninitialized global variables in the BSS section of the object file.
-S -> generuj plik asm
-o -> nazwa pliku wyjściowego
```
Dyrektywy asemblera w kodzie:
- `.globl` -> **modyfikuje tablicę symboli** .global makes the symbol visible to ld. If you define symbol in your partial program, its value is made available to other partial programs that are linked with it. Otherwise, symbol takes its attributes from a symbol of the same name from another file linked into the same program.
- `.data` -> **przełącza sekcje** .data tells as to assemble the following statements onto the end of the data subsection numbered subsection (which is an absolute expression). If subsection is omitted, it defaults to zero.
- `.align` -> **modyfikuje nagłówek** Pad the location counter (in the current subsection) to a particular storage boundary. The first expression (which must be absolute) is the alignment required, as described below. If this expression is omitted then a default value of 0 is used, effectively disabling alignment requirements.
- `.type` -> This directive is used to set the type of a symbol. This sets the type of symbol name to be either a function symbol or an object symbol. There are five different syntaxes supported for the type description field, in order to provide compatibility with various other assemblers.
- STT_FUNC (function) -> Mark the symbol as being a function name.
- STT_GNU_IFUNC (gnu_indirect_function)-> Mark the symbol as an indirect function when evaluated during reloc processing. (This is only supported on assemblers targeting GNU systems).
- STT_OBJECT (object) -> Mark the symbol as being a data object.
- STT_TLS (tls_object) -> Mark the symbol as being a thread-local data object.
- STT_COMMON (common) -> Mark the symbol as being a common data object.
- STT_NOTYPE (notype) -> Does not mark the symbol in any way. It is supported just for completeness.
- gnu_unique_object -> Marks the symbol as being a globally unique data object. The dynamic linker will make sure that in the entire process there is just one symbol with this name and type in use. (This is only supported on assemblers targeting GNU systems).
- `.size` -> This directive is used to set the size associated with a symbol.
- `.value` -> The .value directive generates an initialized word (16-bit, two's complement value) for each expression into the current section. Each expression must be a 16-bit integer value. The .value directive is not valid for the .bss section.
- `.section` -> **dopisuje do sekcji** Use the .section directive to assemble the following code into a section named name.
- `.string` -> Copy the characters in str to the object file
- `.bss` -> **przełącza sekcję** .bss tells as to assemble the following statements onto the end of the bss section.
- `.zero` -> This directive emits size 0-valued bytes. size must be an absolute expression.
- `.quad` -> .quad expects zero or more bignums, separated by commas. For each bignum, it emits an 8-byte integer. If the bignum won’t fit in 8 bytes, it prints a warning message; and just takes the lowest order 8 bytes of the bignum.
- `.long` -> (same as int) Expect zero or more expressions, of any section, separated by commas. For each expression, emit a number that, at run time, is the value of that expression. The byte order and bit size of the number depends on what kind of target the assembly is for.
## Zadanie 2
:::success
Autor: Kamila Goszcz
:::

### Definicje symboli:
Linia 3 - .type
### Czemu assembler nie może wygenerować ostatecznego ciągu bajtów?
Ponieważ pliki *.c nie zostały jeszcze skonsolidowane i nie znają offsetu który powstanie między funkcjami po ich połączeniu, tworzona jest zmienna, w której miejsce zostanie wpisany offset
## Zadanie 3
:::danger
Autor:
:::
## Zadanie 4
:::success
Autor: Kamila Goszcz
:::

### Przepełnienie bufora
Polega na zapisaniu większej ilości danych do pamięci niż ilość zarezerwowanego na ten cel miejsca. Prowadzi to do nadpisania danych znajdujących się bezpośrednio za wyznaczonym miejscem.
### Programowanie zorientowane na powroty
Technika wykorzystująca lukę w zabezpieczeniach komputera , która umożliwia atakującemu wykonanie kodu w obecności zabezpieczeń, takich jak ochrona przestrzeni wykonywalnej i podpisywanie kodu . W tej technice atakujący przejmuje kontrolę nad stosem wywołań w celu przejęcia przepływu sterowania programem, a następnie wykonuje starannie wybrane sekwencje instrukcji maszynowych , które są już obecne w pamięci maszyny, zwane „gadżetami”. Każdy gadżet zwykle kończy się instrukcją powrotu i znajduje się w podprogramie w ramach istniejącego programu i / lub kodu biblioteki współdzielonej. Połączone ze sobą gadżety umożliwiają atakującemu wykonywanie dowolnych operacji na maszynie wykorzystującej mechanizmy obronne, które udaremniają prostsze ataki.
### Wyszukiwanie adresów:
```
objdump -d ropex | grep "gadget":
adres - 401d7b
little endian - 7b1d 4000
```
```
objdump -d ropex | grep "syscall":
adres - 47a88d
little endian - 8da8 4700
```
### Tłumaczenie:
`xxd -r ropex.in.txt >ropex.in`
## Zadanie 5
:::success
Autor: Mateusz Materek
:::

**Kanarek**: szczególne miejsce w pamięci (zaraz za końcem ramki stosu) z losową wartością której naruszenie wskazuje na naruszenie bezpieczeństwa
Szczególne miejsce w pamięci: `%fs:0x28` - to tu przetrzymywany jest kanarek
Parę przykładowych wartości kanarka:
```
0x89d04134039b7600
0xd1968733b1fe6d00
0x7501dd9e3b334d00
```
*(reszta w Terminalu)*
## Zadanie 6
:::danger
Autor:
:::
## Zadanie 7
:::success
Autor: Mateusz Materek
:::

**Konsolidator dynamiczny** -> jego zadaniem jest załadowanie pliku, który używa bibliotek współdzielonych; konsolidator odnajduje potrzebne odwołania i wiąże je z symbolami z programu
*(np. potrafi zlokalizować kod odpowiedzialny za funkcję `printf` wykorzystywaną w naszym programie we (współdzielonej) bibliotece standardowej języka C i powiązać wystąpienie symbolu `printf` w naszym programie z kodem procedury zawartym w innym pliku)*
**Etapy pracy ld**:
1. Startup -> kernel przekazuje kontrolę nie do programu, a do linkera (określonego w sekcji .init)
2. Finding objects -> każdy plik może zawierać sekcję z potrzebnymi bibliotekami współdzielonymi. Trzeba je znaleźć! (mogą być określone przez użytkownika - np. ld.so.conf, przez wpisy DT_RUNPATH w sekcji .dynamic, domyślne ścieżki `/usr/lib`)
3. Relocation -> ld albo zrobi natychmiastową relokację, albo wiązanie zostanie odroczone w czasie dzięki Procedure Linkage Table (PLT) (metoda leniwa jest domyślna)
4. Initialization -> zwrócenie kontroli do programu
*(reszta w Terminalu)*
## Zadanie 8
:::danger
Autor:
:::
## Zadanie 9
:::danger
Autor:
:::