Click here to Skip to main content
15,867,568 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.5K   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

 
GeneralRe: Message size is limited Pin
otwesten2-Sep-03 7:44
otwesten2-Sep-03 7:44 
GeneralRe: Message size is limited Pin
C. Augusto Proiete3-Oct-03 17:37
C. Augusto Proiete3-Oct-03 17:37 
GeneralRe: Message size is limited Pin
otwesten6-Oct-03 1:56
otwesten6-Oct-03 1:56 
GeneralUse of mailslots Pin
rama_ii25-Jun-03 14:33
rama_ii25-Jun-03 14:33 
GeneralRe: Use of mailslots Pin
C. Augusto Proiete6-Dec-03 23:32
C. Augusto Proiete6-Dec-03 23:32 
GeneralDate of version Pin
QuiOui18-Jun-03 14:33
QuiOui18-Jun-03 14:33 
GeneralRe: Date of version Pin
C. Augusto Proiete18-Jun-03 16:41
C. Augusto Proiete18-Jun-03 16:41 
QuestionUnicode ? Pin
Davide Calabro18-Jun-03 9:40
Davide Calabro18-Jun-03 9:40 
AnswerRe: Unicode ? Pin
C. Augusto Proiete18-Jun-03 12:53
C. Augusto Proiete18-Jun-03 12:53 
GeneralWhere does the messenger service gets invoked here Pin
VijayKarthik18-Jun-03 3:18
VijayKarthik18-Jun-03 3:18 
GeneralRe: Where does the messenger service gets invoked here Pin
C. Augusto Proiete18-Jun-03 5:58
C. Augusto Proiete18-Jun-03 5:58 
GeneralMissing Something... Pin
Stephen Quattlebaum11-Jun-03 6:01
Stephen Quattlebaum11-Jun-03 6:01 
Questionwhy not close handle of mailslot? Pin
Elias Bachaalany11-Jun-03 4:33
Elias Bachaalany11-Jun-03 4:33 
AnswerRe: why not close handle of mailslot? Pin
C. Augusto Proiete12-Jun-03 3:34
C. Augusto Proiete12-Jun-03 3:34 
GeneralA few issues... Pin
Ryan Binns9-Jun-03 4:12
Ryan Binns9-Jun-03 4:12 
GeneralRe: A few issues... Pin
C. Augusto Proiete12-Jun-03 3:33
C. Augusto Proiete12-Jun-03 3:33 
Generalmessage fairy Pin
Simon.W8-Jun-03 18:57
Simon.W8-Jun-03 18:57 
GeneralRe: message fairy Pin
C. Augusto Proiete12-Jun-03 3:49
C. Augusto Proiete12-Jun-03 3:49 
Questionwhy not use NetMessageBufferSend sdk api?? Pin
kenpingliu8-Jun-03 18:09
kenpingliu8-Jun-03 18:09 
AnswerRe: why not use NetMessageBufferSend sdk api?? Pin
Mandalay8-Jun-03 19:53
Mandalay8-Jun-03 19:53 
QuestionDouble-send? Pin
Ryan Binns8-Jun-03 17:52
Ryan Binns8-Jun-03 17:52 
AnswerRe: Double-send? Pin
Andy Marks9-Jun-03 2:45
Andy Marks9-Jun-03 2:45 
GeneralRe: Double-send? Pin
Ryan Binns9-Jun-03 4:03
Ryan Binns9-Jun-03 4:03 

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.