Click here to Skip to main content
15,861,125 members
Articles / Desktop Programming / MFC
Article

Fake (or Anonymous) NetSend for Windows NT/2000/XP

Rate me:
Please Sign up or sign in to vote.
4.86/5 (25 votes)
17 Jun 2003Apache2 min read 231.2K   6.5K   33   83
The Fake NetSend is a little utility that can be used to send anonymous (or fake) messages through your Windows Network.

Image 1

Introduction

A few years ago I was working at a software development company and one day my co-worker asked me if would be possible to use the Windows Net Send mechanism to send messages through the corporate network using some programming language like C++. I found the idea very interesting since we could implement this functionality in some of our applications so they could send messages to our computers when an exception occurs and/or something goes wrong.

After some time of MSDN and Google, learning about MailSlots, I could write the first test program and, for our surprise, worked like a charm.

With our program ready, we've started do create a generic function to send the messages, so we could make it avaliable at our "shared" directory, when I got an idea: "What if we use an invalid User Name to send the message?". Well, I think I don't need to tell you how this history ends :)

Background Information

The FakeSend (and Net Send) mechanism is very simple. It uses an Interprocess Communication (IPC) resource called "MailSlots". We can create MailSlots to swap messages between applications on a network environment just like we do with Sockets, Named Pipes, etc... The MailSlot internally uses a datagram socket, which means that we cannot be sure if the message has arrived. To learn more about MailSlots read the MailSlot Reference from MSDN Library.

Using the code

The code is very straightforward. We have the main function NetSend() that receives three values:

  • szSender - The name of the person who is sending the message (Any name you want);
  • szReceiver - The User Name (Network Login) or Machine name of the person who will receive the message;
  • szMessage - The Message you want to send.
C++
bool NetSend(const char * szSender, const char * szReceiver,
             const char * szMessage)
{
    // Our main variables
    char * pszMailSlot = NULL;
    unsigned char * pucMessage = NULL;
    unsigned int nMailSlotLen, nMsgFormatLen;

    HANDLE hHandle;
    DWORD dwBytesWritten;
    bool bRet = false;

    // Get the length of the strings
    nMailSlotLen  = strlen(szReceiver) + sizeof(szMailSlotPath);
    nMsgFormatLen = strlen(szSender) + strlen(szReceiver) +
                    strlen(szMessage) + sizeof(szMsgFormat);

    // Allocate necessary memory
    pszMailSlot = new char[nMailSlotLen];
    pucMessage  = new unsigned char[nMsgFormatLen];

    // Network path for <Receiver> MailSlot:
    // "\\Receiver\MAILSLOT\messngr"
    sprintf(pszMailSlot, szMailSlotPath, szReceiver);

    // Message Format:
    // "Sender\0Receiver\0Message\0"
    // sprintf doesn't work with \0 so here I'm using \r
    sprintf((char *)pucMessage, szMsgFormat, szSender,
                                szReceiver, szMessage);

    // Replace all '\r' characters with '\0'
    for (unsigned short i = 0; i < nMsgFormatLen; i++)
    {
        if (pucMessage[i] == '\r')
            pucMessage[i] = '\0';

        else if (pucMessage[i] == '\0')
            break;
    }

    // Create the file into Receiver's MailSlot and get a Handle to this file
    hHandle = CreateFile(pszMailSlot, GENERIC_WRITE, FILE_SHARE_READ, NULL,
                            OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

    // Do we have a valid handle?
    if (hHandle)
    {
        // Write the message to file
        bRet = (WriteFile(hHandle, pucMessage, (DWORD)nMsgFormatLen,
                    &dwBytesWritten, NULL) == TRUE);

        // Free the handle
        CloseHandle(hHandle);
    }

    return bRet;
}

History

  • 2003-06-08 - First version of FakeSend. Bugs: Zero, I hope :)
  • 2003-06-11 - Version 1.01. Just improving the code.

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0


Written By
Chief Technology Officer Stablehouse
Bermuda Bermuda
C. Augusto Proiete has been programming for over 20 years, having first started with Clipper Summer'87.

He loves software development and reverse engineering. Today his main development technologies includes Blazor, ASP .NET Core, C#, SQL Server. He also has the following titles:

MVP - Microsoft Most Valuable Professional;
MCT - Microsoft Certified Trainer;
MCPD - Microsoft Certified Professional Developer;
MCTS - Microsoft Certified Technology Specialist;
MCSD - Microsoft Certified Solution Developer;
MCDBA - Microsoft Certified Database Administrator.

Technical blog:
https://augustoproiete.net

Comments and Discussions

 
GeneralANSI or UNICODE Pin
ponkin28-Sep-04 21:09
ponkin28-Sep-04 21:09 
GeneralRe: ANSI or UNICODE Pin
Hernan5-Nov-04 7:01
Hernan5-Nov-04 7:01 
Generalmessage length Pin
Hernan23-Jul-04 4:12
Hernan23-Jul-04 4:12 
GeneralRe: message length Pin
C. Augusto Proiete29-Jun-05 11:37
C. Augusto Proiete29-Jun-05 11:37 
GeneralMultiple Sends Pin
matrixprogrammer20-May-04 16:09
matrixprogrammer20-May-04 16:09 
GeneralRe: Multiple Sends Pin
C. Augusto Proiete29-Jun-05 11:34
C. Augusto Proiete29-Jun-05 11:34 
Generalthank you Pin
jalcot27-Apr-04 2:01
jalcot27-Apr-04 2:01 
GeneralRe: thank you Pin
C. Augusto Proiete29-Jun-05 11:39
C. Augusto Proiete29-Jun-05 11:39 
Thank you. Hope it was useful.

Regards,
C. Augusto Proiete
https://augustoproiete.net


modified 25-May-20 19:24pm.

GeneralA little memory leak Pin
mmagnani7113-Apr-04 4:20
mmagnani7113-Apr-04 4:20 
GeneralNew to C++ Pin
monahanb11-Feb-04 7:55
monahanb11-Feb-04 7:55 
GeneralRe: New to C++ Pin
C. Augusto Proiete14-Feb-04 15:17
C. Augusto Proiete14-Feb-04 15:17 
GeneralUsername doesn't work Pin
ltransler10-Feb-04 21:20
ltransler10-Feb-04 21:20 
GeneralRe: Username doesn't work Pin
C. Augusto Proiete14-Feb-04 15:22
C. Augusto Proiete14-Feb-04 15:22 
GeneralRe: Username doesn't work Pin
Anonymous2-Apr-04 0:53
Anonymous2-Apr-04 0:53 
Questionvirtual ip on remote side? Pin
*scenera*8-Feb-04 19:15
*scenera*8-Feb-04 19:15 
AnswerRe: virtual ip on remote side? Pin
C. Augusto Proiete14-Feb-04 16:16
C. Augusto Proiete14-Feb-04 16:16 
QuestionHow to get the message in the other computer Pin
Fad B29-Jan-04 1:11
Fad B29-Jan-04 1:11 
AnswerRe: How to get the message in the other computer Pin
C. Augusto Proiete14-Feb-04 16:10
C. Augusto Proiete14-Feb-04 16:10 
GeneralVB Code for FakeSend Pin
asdfgafasfdasf19-Dec-03 16:19
asdfgafasfdasf19-Dec-03 16:19 
GeneralRe: VB Code for FakeSend Pin
C. Augusto Proiete14-Feb-04 16:05
C. Augusto Proiete14-Feb-04 16:05 
GeneralRe: VB Code for FakeSend Pin
VB newbie18-Mar-04 18:36
VB newbie18-Mar-04 18:36 
Generaldon't know how to work it Pin
Johnny B6-Dec-03 16:37
Johnny B6-Dec-03 16:37 
GeneralRe: don't know how to work it Pin
C. Augusto Proiete6-Dec-03 23:18
C. Augusto Proiete6-Dec-03 23:18 
GeneralRe: don't know how to work it Pin
Anonymous7-Dec-03 5:20
Anonymous7-Dec-03 5:20 
QuestionWhy does it send 2 messages out? Pin
asdfgafasfdasf5-Dec-03 19:41
asdfgafasfdasf5-Dec-03 19:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.