Copy payload from ndis IM Driver

Dear all,

-----start of code-------
NdisZeroMemory(pPacketContent, totalLength ) ;

NdisQueryBufferSafe(
NdisBuffer,
&tembuffer,
&copysize,
NormalPagePriority
…blah blah

now i have the “pPacketcontent = full network packets” & “DataOffset=Length of packets”

i try to use sprintf to grep the pPacketContent[54] until end of the packet, it will always cause the BSOD. Code as below:

--------------start of code----------
for(j=54;j<=DataOffset;j++)
{
sprintf(Buffer1,“%.*s”,pPacketContent[j]);
}
MyDriverWriteFile((PVOID)pPacketContent, DataOffset, m_ghFileHandle);

-----end-----------

The purpose of the code above, i want to copy the pPacketContent start from byte 54 until the end to Buffer1, and i will use the MyDriveWriteFile to write the content to text file in c:\xxx.txt using WorkItem method.

But it will always cause the blue screen. Anyone can help to see any problems in my code above? thanks

Please read the NDIS_PACKET Discussion at this URL:

http://ndis.com/ndis-ndis5/default.htm

It should help you understand where you are going wrong.

Do not use sprintf in the kernel. Use a function like UTILReadOnPacket, from
the NDIS_PACKET Discussion.

Thomas F. Divine
http://www.pcausa.com


From:
Sent: Friday, July 16, 2010 4:59 AM
To: “Windows System Software Devs Interest List”
Subject: [ntdev] Copy payload from ndis IM Driver

> Dear all,
>
> -----start of code-------
> NdisZeroMemory(pPacketContent, totalLength ) ;
>
> NdisQueryBufferSafe(
> NdisBuffer,
> &tembuffer,
> &copysize,
> NormalPagePriority
> …blah blah
>
> now i have the “pPacketcontent = full network packets” &
> “DataOffset=Length of packets”
>
> i try to use sprintf to grep the pPacketContent[54] until end of the
> packet, it will always cause the BSOD. Code as below:
>
> --------------start of code----------
> for(j=54;j<=DataOffset;j++)
> {
> sprintf(Buffer1,“%.*s”,pPacketContent[j]);
> }
> MyDriverWriteFile((PVOID)pPacketContent, DataOffset, m_ghFileHandle);
> …
> -----end-----------
>
> The purpose of the code above, i want to copy the pPacketContent start
> from byte 54 until the end to Buffer1, and i will use the MyDriveWriteFile
> to write the content to text file in c:\xxx.txt using WorkItem method.
>
> But it will always cause the blue screen. Anyone can help to see any
> problems in my code above? thanks
>
>
>
> —
> NTDEV is sponsored by OSR
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer

xxxxx@gmail.com wrote:

-----start of code-------
NdisZeroMemory(pPacketContent, totalLength ) ;

NdisQueryBufferSafe(
NdisBuffer,
&tembuffer,
&copysize,
NormalPagePriority
…blah blah

now i have the “pPacketcontent = full network packets” & “DataOffset=Length of packets”

i try to use sprintf to grep the pPacketContent[54] until end of the packet, it will always cause the BSOD. Code as below:

You have apparently invented your own definition of the word “grep”
here, because your code doesn’t do anything like “grep”.

--------------start of code----------
for(j=54;j<=DataOffset;j++)
{
sprintf(Buffer1,“%.*s”,pPacketContent[j]);
}
MyDriverWriteFile((PVOID)pPacketContent, DataOffset, m_ghFileHandle);

-----end-----------

Are you joking? That can’t really be your code. What on earth do you
think that sprintf is doing? You aren’t really trying to copy one byte
at a time using sprintf, are you? Because that would be utter nonsense.

If you want to copy from pPacketContent[54] to the end into Buffer1,
then that’s what you should do:
RtlCopyMemory( Buffer1, &pPacketContent[54], DataOffset-54 );
Best make damn sure that DataOffset is bigger than 54. Also best make
sure that Buffer1 has enough room to hold all the data.

However, note that your call to MyDriverWriteFile isn’t writing from
Buffer1, it’s writing from pPacketContent. If you want to write part of
a buffer, you don’t need to make a copy at all.

MyDriverWriteFile( &pPacketContent[54], DataOffset-54, m_ghFileHandle );


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.