-
Notifications
You must be signed in to change notification settings - Fork 0
/
revshell_server.c
67 lines (53 loc) · 1.54 KB
/
revshell_server.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <string.h>
#define BUFFER_SIZE 1024
void *recvData(void *vargp);
int main(int argc, char const *argv[])
{
if (argc != 2)
{
printf("Usage %s <port>\n", argv[0]);
exit(1);
}
struct sockaddr_in serv_addr, cli_addr;
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(atoi(argv[1]));
serv_addr.sin_addr.s_addr = INADDR_ANY;
bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
listen(sockfd, 5);
int clilen = sizeof(cli_addr);
int clientfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
char *cliip = inet_ntoa(cli_addr.sin_addr);
printf("[+] Connected to client %s\n", cliip);
pthread_t thread_id;
pthread_create(&thread_id, NULL, recvData, (void *)&clientfd);
//pthread_join(thread_id, NULL);
char buffer[BUFFER_SIZE];
do {
bzero(buffer,BUFFER_SIZE);
fgets(buffer, BUFFER_SIZE - 1, stdin);
write(clientfd, buffer, strlen(buffer));
} while (strcmp(buffer, "exit\n") != 0);
pthread_cancel(thread_id);
close(clientfd);
close(sockfd);
return 0;
}
void *recvData(void *vargp)
{
int *clientfd = (int *)vargp;
char buffer[BUFFER_SIZE];
while (1)
{
bzero(buffer,BUFFER_SIZE);
int n = read(*clientfd, buffer, BUFFER_SIZE - 1);
if (n > -1) printf("> %s", buffer);
sleep(0.5);
}
return NULL;
}