Search code examples
cwindowssocketsraw-sockets

Can't create raw socket with admin account on Windows 7


According to this page, creation of raw sockets under Windows 7 is allowed if the account has administrative privileges. My local account is an admin but it still won't allow me to create the socket.

If I run this:

#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#include <ws2tcpip.h>

void sockerr(char *message)
{
        char errbuf[300];
        FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, WSAGetLastError(),
                      0, errbuf, sizeof(errbuf), NULL);
        printf("%s: (%d) %s", message, WSAGetLastError(), errbuf);
}

int main()
{
    int sock;
    struct sockaddr_in sin;
    struct WSAData data;

    if (WSAStartup(2, &data)) {
        fprintf(stderr, "Error in WSAStartup: %d\n", WSAGetLastError());
        exit(1);
    }

    sock = socket(AF_INET, SOCK_RAW, IPPROTO_IGMP);
    if (sock == -1) {
        sockerr("socket failed");
        exit(1);
    }
    printf("opened raw socket\n");
    closesocket(sock);
    return 0;    
}

I get this:

socket failed: (10013) An attempt was made to access a socket in a way forbidden by its access permissions.

But I'm an admin:

user account

This is on my home desktop with Windows 7 Pro. I have McAfee Antivirus Plus installed, however disabling it had no effect.

This program works fine on my Windows 7 Enterprise laptop.

Any ideas?


Solution

  • Not sure why you want to use raw sockets, but it seems the solution is to use winpcap If you google you can find examples on using this. Here is one sample: http://www.binarytides.com/raw-sockets-packets-with-winpcap/

    Before I try this I would also make sure that the program is running as Administrator. maybe you are an admin, but the program is not running with that privilege.