Click here to Skip to main content
Click here to Skip to main content

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

By , 17 Jun 2003
 

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.
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Caio Proiete
Software Developer (Senior)
Portugal Portugal
Member
Caio Proiete has been programming for over 10 years, having first started with Clipper Summer'87.
 
He loves software development and reverse engineering. Today his main development technologies includes ASP .NET MVC, jQuery, HTML5, Silverlight, and WCF. 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:
http://caioproiete.net

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questiondelphi?memberp2bf28 Feb '07 - 6:01 
plizzzz delphi code?
 
oooooooooo

GeneralNo messages receivedmemberPolovski11 Feb '07 - 2:27 
QuestionForce line feed in messagememberzhur18 Oct '06 - 10:48 
Generalfake netSend from c# formmemberStojaneee17 May '06 - 13:41 
Generalsend to allmemberyaewo10 May '06 - 10:10 
GeneralProblem, program ends very fastmemberIustus-aTw31 Mar '06 - 10:47 
GeneralRe: Problem, program ends very fastmemberIustus-aTw3 Apr '06 - 7:28 
GeneralRe: Problem, program ends very fastmemberStojaneee17 May '06 - 13:33 
QuestionI didn't get a popup box, Why ???memberKENyroj20 Mar '06 - 16:47 
GeneralVB6 Codemembermattwright21 Dec '05 - 9:59 
Generalsudden end of program encountermembercharllyyy4eva22 Jul '05 - 2:23 
GeneralSend by IP addresssussYaroslav A. Golovach9 Jul '05 - 11:32 
GeneralHi dude!memberGoldSight6 Jun '05 - 10:19 
GeneralReceive message!memberTailana1 Jun '05 - 16:04 
GeneralQbasicmemberUnknown00425 Jan '05 - 18:05 
QuestionHow to delete the renamed filememberShadow294 Oct '04 - 21:59 
AnswerRe: How to delete the renamed filesussDrakonas13 Oct '04 - 19:47 
GeneralANSI or UNICODEmemberponkin28 Sep '04 - 21:09 
GeneralRe: ANSI or UNICODEmemberHernan5 Nov '04 - 7:01 
Generalmessage lengthmemberHernan23 Jul '04 - 4:12 
GeneralRe: message lengthmemberCaio Proiete29 Jun '05 - 11:37 
GeneralMultiple Sendsmembermatrixprogrammer20 May '04 - 16:09 
GeneralRe: Multiple SendsmemberCaio Proiete29 Jun '05 - 11:34 
Generalthank youmemberjalcot27 Apr '04 - 2:01 
GeneralRe: thank youmemberCaio Proiete29 Jun '05 - 11:39 
GeneralA little memory leakmembermmagnani7113 Apr '04 - 4:20 
GeneralNew to C++sussanonymous11 Feb '04 - 7:55 
GeneralRe: New to C++memberCaio Proiete14 Feb '04 - 15:17 
GeneralUsername doesn't workmemberltransler10 Feb '04 - 21:20 
GeneralRe: Username doesn't workmemberCaio Proiete14 Feb '04 - 15:22 
GeneralRe: Username doesn't worksussAnonymous2 Apr '04 - 0:53 
Questionvirtual ip on remote side?member*scenera*8 Feb '04 - 19:15 
AnswerRe: virtual ip on remote side?memberCaio Proiete14 Feb '04 - 16:16 
QuestionHow to get the message in the other computermemberFad B29 Jan '04 - 1:11 
AnswerRe: How to get the message in the other computermemberCaio Proiete14 Feb '04 - 16:10 
GeneralVB Code for FakeSendmemberasdfgafasfdasf19 Dec '03 - 16:19 
GeneralRe: VB Code for FakeSendmemberCaio Proiete14 Feb '04 - 16:05 
GeneralRe: VB Code for FakeSendsussanon vb newbie18 Mar '04 - 18:36 
Generaldon't know how to work itmemberJohnny B6 Dec '03 - 16:37 
GeneralRe: don't know how to work itmemberCaio Proiete6 Dec '03 - 23:18 
GeneralRe: don't know how to work itsussAnonymous7 Dec '03 - 5:20 
QuestionWhy does it send 2 messages out?memberasdfgafasfdasf5 Dec '03 - 19:41 
AnswerRe: Why does it send 2 messages out?memberCaio Proiete6 Dec '03 - 23:23 
GeneralWhy can't send msg to a IP Address.memberclonesheep1 Nov '03 - 20:42 
GeneralRe: Why can't send msg to a IP Address.memberCaio Proiete6 Dec '03 - 23:26 
QuestionHow do i put ICONS on itmemberOsrald6 Oct '03 - 3:31 
AnswerRe: How do i put ICONS on itmemberCaio Proiete6 Dec '03 - 23:29 
GeneralPlease Give Me Code VB6 Or C++ To Net Send AnonymoussussAnonymous3 Oct '03 - 17:32 
GeneralRe: Please Give Me Code VB6 Or C++ To Net Send AnonymousmemberCaio Proiete3 Oct '03 - 17:38 
GeneralRe: Please Give Me Code VB6 Or C++ To Net Send Anonymousmembermattwright21 Dec '05 - 9:59 

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 18 Jun 2003
Article Copyright 2003 by Caio Proiete
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid