Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/ipwn/cmd_net.c
Views: 11766
/*1* Copyright (c) 2007 H D Moore <hdm [at] metasploit.com>2* This file is part of the Metasploit Framework.3* $Revision$4*/56#include <sys/types.h>7#include <sys/param.h>8#include <sys/stat.h>9#include <stdlib.h>10#include <limits.h>11#include <unistd.h>12#include <string.h>13#include <errno.h>14#include <stdio.h>15#include <dirent.h>16#include <pwd.h>17#include <grp.h>18#include <sys/fcntl.h>19#include <sys/socket.h>20#include <arpa/inet.h>21#include <netdb.h>2223#include "cmd.h"2425void cmd_download(int argc, char * argv[])26{27int src, dst, len, i;28char buff[4096];29char *path, *p, *t;30char *uri;31char *host;32struct sockaddr_in server;33struct hostent *haddr;34int port = 80;35int dmode = 0;36int off = 0;37int clen = 0;38int tot = 0;3940// src == socket41p = strstr(argv[1], "http://");42if( p == NULL) {43printf("The url must start with http://\n");44return;45}4647p+=7;4849t = strstr(p, "/");50if (t == NULL) {51printf("The url must contain a path\n");52return;53}5455uri = strdup(t);56*t = '\0';5758t = strstr(p, ":");59if (t != NULL) {60*t = '\0';61t++;62port = atoi(t);63}6465host = strdup(p);6667sprintf(buff, "GET %s HTTP/1.0\r\nHost: %s:%d\r\nConnection: Close\r\nUser-Agent: iPwn\r\n\r\n", uri, host, port);6869if( ( haddr = gethostbyname(host) ) == NULL ) {70free(host);71perror("gethostbyname");72return;73}7475free(host);7677if (port < 1 || port > 65535) {78free(uri);79perror("invalid port");80return;81}8283if( ( src = socket ( PF_INET, SOCK_STREAM, IPPROTO_TCP ) ) < 0 ) {84free(uri);85perror("socket");86return;87}8889memset ( &server, 0, sizeof( server ) );90server.sin_family = AF_INET;91server.sin_addr.s_addr = *( ( unsigned long * ) haddr->h_addr );92server.sin_port = htons ( port );9394if( connect ( src, ( struct sockaddr * )&server, sizeof( server ) ) < 0 ) {95free(uri);96close(src);97perror("connect");98return;99}100101if( send( src, buff, strlen(buff), 0 ) != strlen(buff) ) {102free(uri);103close(src);104perror("send");105return;106}107108path = strdup(argv[2]);109dst = open(path, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);110if (dst == -1) {111112if(errno == EISDIR) {113t = strrchr(uri, '/');114if (t != NULL) {115t++;116if(strlen(t) == 0) {117free(uri);118t = "download.out";119}120} else {121t = uri;122}123124p = malloc(strlen(path) + strlen(t) + 2);125sprintf(p, "%s/%s", path, t);126free(path);127path = p;128129dst = open(path, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);130if ( dst == -1 ) {131close(src);132free(path);133free(uri);134perror("open(dst)");135return;136}137138} else {139close(src);140free(path);141free(uri);142perror("open(dst)");143return;144}145}146147free(uri);148149memset(buff, 0, sizeof(buff));150off = 0;151tot = 0;152while (dmode == 0) {153154if (sizeof(buff)-1-off <= 0)155break;156157len = read(src, buff+off, sizeof(buff)-1-off);158159if (len == -1) break;160if (len == 0) break;161off += len;162163p = strstr(buff, "Content-Length:");164165if (p) {166p += 15;167clen = atoi(p);168}169170t = strstr(buff, "\r\n\r\n");171if (t) {172dmode = 1;173*t = '\0';174t += 4;175176i = (int) ((buff + off) - t);177write(dst, t, i);178tot += i;179}180}181182printf("\n====================\n");183printf("HTTP Server Response\n");184printf("====================\n\n%s\n\n",buff);185186if(! dmode || clen < 0) {187printf("could not parse the server response\n");188close(src);189close(dst);190unlink(path);191free(path);192return;193}194195if(clen > 0) {196printf("Receiving %d bytes...\n", clen);197while(clen > 0 && len > 0) {198len = read(src, buff, sizeof(buff));199if (len > 0) {200write(dst, buff, len);201tot += len;202}203clen -= len;204}205} else {206printf("Receiving data...\n");207while(len > 0) {208len = read(src, buff, sizeof(buff));209if (len > 0) {210write(dst, buff, len);211tot += len;212}213}214}215216printf("Received %d bytes\n", tot);217218close(src);219close(dst);220221chmod(path, 0755);222free(path);223}224225226