Created
August 26, 2011 13:25
-
-
Save lattera/1173394 to your computer and use it in GitHub Desktop.
Broadcast Shell
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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <string.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <sys/select.h> | |
#include <fcntl.h> | |
int main(int argc, char *argv[]) | |
{ | |
int fd, logfd, sockfd, newsockfd, i, j; | |
struct sockaddr_in server, client; | |
int maxfds; | |
fd_set master, readfds; | |
char buf[400]; | |
fd = open("/root/typescript", O_RDONLY); | |
if (fd < 0) | |
{ | |
perror("open"); | |
exit(1); | |
} | |
lseek(fd, SEEK_END, 0); | |
logfd = open("/root/logscript", O_WRONLY | O_CREAT | O_TRUNC); | |
if (logfd < 0) | |
{ | |
perror("open"); | |
exit(1); | |
} | |
sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); | |
if (sockfd < 0) | |
{ | |
perror("socket"); | |
exit(1); | |
} | |
server.sin_family = AF_INET; | |
server.sin_port = htons(8080); | |
server.sin_addr.s_addr = INADDR_ANY; | |
if (bind(sockfd, (struct sockaddr *)&server, (socklen_t)sizeof(server)) < 0) | |
{ | |
perror("bind"); | |
exit(1); | |
} | |
if (listen(sockfd, 5) < 0) | |
{ | |
perror("listen"); | |
exit(1); | |
} | |
maxfds = sockfd; | |
FD_ZERO(&master); | |
FD_SET(sockfd, &master); | |
FD_SET(fd, &master); | |
while (1) | |
{ | |
readfds = master; | |
if (select(maxfds+1, &readfds, NULL, NULL, NULL) <= 0) | |
continue; | |
if (FD_ISSET(sockfd, &readfds)) | |
{ | |
i = sizeof(client); | |
newsockfd = accept(sockfd, (struct sockaddr *)&client, (socklen_t *)&i); | |
if (newsockfd < 0) | |
{ | |
perror("accept"); | |
continue; | |
} | |
maxfds = (maxfds < newsockfd) ? newsockfd : maxfds; | |
FD_SET(newsockfd, &master); | |
} | |
else if (FD_ISSET(fd, &readfds)) | |
{ | |
j = read(fd, buf, sizeof(buf)); | |
if (j < 0) | |
{ | |
perror("read"); | |
continue; | |
} | |
if (j == 0) | |
continue; | |
write(logfd, buf, j); | |
for (i=0; i<=maxfds; i++) | |
if (FD_ISSET(i, &master)) | |
if (i != fd && i != sockfd) | |
send(i, buf, j, 0); | |
} | |
else | |
{ | |
for (i=0; i<=maxfds; i++) | |
{ | |
if (FD_ISSET(i, &readfds)) | |
{ | |
if (recv(i, &j, sizeof(j), 0) <= 0) | |
{ | |
close(i); | |
FD_CLR(i, &master); | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment