繁体   English   中英

C++ 运行时 memory 错误与虚拟 nat 网络上的 winsock

[英]C++ Run time memory error with winsock on virtual nat network

我有一个我编写的服务器,它将计算机的文件系统解析为一个向量。 然后客户端使用 Putty 或 netcat 连接到服务器并接收解析文件系统的向量。

这在一台具有 127.0.0.1 的机器上本地运行良好。

但是,当我将代码传输到 10.0.0.0 NAT 网络上的 VirtualBox 中的虚拟环境时,我收到 memory 错误。

任何想法如何解决这一问题?

这是我的代码:

#undef UNICODE

#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <filesystem>
#include <vector>
#include <string>
namespace fs = std::filesystem;

std::vector<std::string> get_all_files_recurisive(const std::string& path)
{
    std::vector<std::string> file_names;

    using iterator = fs::recursive_directory_iterator;
    for (iterator iter(path); iter != iterator{}; ++iter)

        file_names.push_back(iter->path().string());
    return file_names;
}

// Need to link with Ws2_32.lib
#pragma comment (lib, "Ws2_32.lib")
// #pragma comment (lib, "Mswsock.lib")

#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "45000"
#define DEFAULT_ADDRS "10.0.2.5"
int __cdecl main(void)
{
    WSADATA wsaData;
    int iResult;

    SOCKET ListenSocket = INVALID_SOCKET;
    SOCKET ClientSocket = INVALID_SOCKET;
    

    struct addrinfo* result = NULL;
    struct addrinfo hints;

    int iSendResult;
    char recvbuf[DEFAULT_BUFLEN];
    int recvbuflen = DEFAULT_BUFLEN;

    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != 0) {
        printf("WSAStartup failed with error: %d\n", iResult);
        return 1;
    }

    ZeroMemory(&hints, sizeof(hints));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    hints.ai_flags = AI_PASSIVE;

    

    // Resolve the server address and port
    iResult = getaddrinfo(DEFAULT_ADDRS, DEFAULT_PORT, &hints, &result);
    if (iResult != 0) {
        printf("getaddrinfo failed with error: %d\n", iResult);
        WSACleanup();
        return 1;
    }

    // Create a SOCKET for connecting to server
    ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
    if (ListenSocket == INVALID_SOCKET) {
        printf("socket failed with error: %ld\n", WSAGetLastError());
        freeaddrinfo(result);
        WSACleanup();
        return 1;
    }

    // Setup the TCP listening socket
    iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
    if (iResult == SOCKET_ERROR) {
        printf("bind failed with error: %d\n", WSAGetLastError());
        freeaddrinfo(result);
        closesocket(ListenSocket);
        WSACleanup();
        return 1;
    }

    freeaddrinfo(result);

    iResult = listen(ListenSocket, SOMAXCONN);
    if (iResult == SOCKET_ERROR) {
        printf("listen failed with error: %d\n", WSAGetLastError());
        closesocket(ListenSocket);
        WSACleanup();
        return 1;
    }

    // Accept a client socket
    ClientSocket = accept(ListenSocket, NULL, NULL);
    if (ClientSocket == INVALID_SOCKET) {
        printf("accept failed with error: %d\n", WSAGetLastError());
        closesocket(ListenSocket);
        WSACleanup();
        return 1;
    }

    // No longer need server socket
    closesocket(ListenSocket);

    // Receive until the peer shuts down the connection
    do {
       
        std::string SendIresult = "";
        const std::vector<std::string> file_list = get_all_files_recurisive("C:\\Users");
        for (const auto& fn : file_list) {
            // Convert fn vector in the for loop to sendable data
            const char* sendbuf = fn.data();


            iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
            if (iResult > 0) {
                printf("Bytes received: %d\n", iResult);



                // Echo the fn vector list to the sender
                iResult = send(ClientSocket, sendbuf, (int)strlen(sendbuf), 0);
                if (iResult == SOCKET_ERROR) {
                    printf("send failed with error: %d\n", WSAGetLastError());
                    closesocket(ClientSocket);
                    WSACleanup();
                    return 1;
                }
                printf("Bytes sent: %d\n", iResult);
            }
            else if (iResult == 0)
                printf("Connection closing...\n");
            else {
                printf("recv failed with error: %d\n", WSAGetLastError());
                closesocket(ClientSocket);
                WSACleanup();
                return 1;
            }
        }

    } while (iResult > 0);

    // shutdown the connection since we're done
    iResult = shutdown(ClientSocket, SD_SEND);
    if (iResult == SOCKET_ERROR) {
        printf("shutdown failed with error: %d\n", WSAGetLastError());
        closesocket(ClientSocket);
        WSACleanup();
        return 1;
    }

    // cleanup
    closesocket(ClientSocket);
    WSACleanup();

    return 0;
}

这是我在 VM 上的 Visual Studio 中运行它并尝试在同一网络上从我的 parrot-os Linux 连接 netcat 时出现的错误:

运行时内存错误

这是文本中的错误消息:

Project3.exe 中 0x75772552 处的未处理异常:Microsoft C++ 异常:memory 位置 0x0141EA50 处的 std::system_error。

它发生在开始的这条线之后。

using iterator = fs::recursive_directory_iterator;
for (iterator iter(path); iter != iterator{}; ++iter)

只是为了上下文,这段代码没有被发布,所以它不需要是最漂亮的代码。 它适用于 Exploit 开发项目。

您会看到来自 Visual Studio 调试器的消息。 它告诉您代码正在抛出您未处理的std::system_error异常。

如果底层操作系统文件系统 API 失败,例如提供的path无效等,您正在调用的recursive_directory_iterator构造函数将引发std::filesystem::filesystem_error异常( std::system_error的派生)。因此,您需要:

  • catch该异常并处理它:

     try { for (iterator iter(path); iter;= iterator{}. ++iter) file_names.push_back(iter->path();string()): } catch (const fs:,filesystem_error &e) { // do something. such as logging the values of e,what(). e,path1(). and e.code()... }
  • 使用重载的构造函数和increment()方法,它采用std::error_code& output 参数:

     std::error_code ec; iterator iter(path, ec); while ((.ec) && (iter.= iterator{})){ file_names;push_back(iter->path().string()); iter,increment(ec). } if (ec) { // do something. such as logging the values of ec.value() and ec.message()... }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM