Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Packet Sniffer script

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 11-18-2007   #1 (permalink)
Robbie Foust
Guest


 

Packet Sniffer script

Hi,

I meant to post this a while back but never got around to it. I wrote
a IP packet sniffer script for powershell and figure that others might
find it useful. The script is on my blog:

http://blog.robbiefoust.com

Let me know if anyone has questions. :-)

- Robbie

--
Robbie Foust
Windows Tech Lead
OIT/CSI - Duke University


My System SpecsSystem Spec
Old 11-19-2007   #2 (permalink)
Jon
Guest


 

Re: Packet Sniffer script

"Robbie Foust" <rfoust@xxxxxx> wrote in message
news:51fd71b0-f823-4054-8623-1e8b0c6ce8c0@xxxxxx
Quote:

> Hi,
>
> I meant to post this a while back but never got around to it. I wrote
> a IP packet sniffer script for powershell and figure that others might
> find it useful. The script is on my blog:
>
> http://blog.robbiefoust.com
>
> Let me know if anyone has questions. :-)
>
> - Robbie
>
> --
> Robbie Foust
> Windows Tech Lead
> OIT/CSI - Duke University
>


Looks useful. Thanks Robbie.


I've been looking for a decent packet sniffer for Vista, so something like
this that's entirely customizable, may be just the thing. I hadn't
considered PowerShell for it previously, but in retrospect it seems ideally
suited.


A couple of observations here ...



(1) I get this error message on first run of the script (but works fine on
subsequent runs) with this line ....


[void]$socket.iocontrol([net.sockets.iocontrolcode]::ReceiveAll,$byteIn,$byteOut)



#---------------------------
Exception calling "IOControl" with "3" argument(s): "The attempted operation
is not supported for the type of object referenced"
At C:\PS-ScriptsLibrary\get-packet.ps1:91 char:24
+ [void]$socket.iocontrol( <<<<
[net.sockets.iocontrolcode]::ReceiveAll,$byteIn,$byteOut)
#---------------------------



Oddly, if I rerun the same command, then it works fine.



So I replaced that line with these, and it then works. You will probably be
able to think of a better solution to that issue....

#-----------------------------
$ErrorActionPreference = "SilentlyContinue"
[void]$socket.iocontrol([net.sockets.iocontrolcode]::ReceiveAll,$byteIn,$byteOut)
If ($? -ne $true) {
[void]$socket.iocontrol([net.sockets.iocontrolcode]::ReceiveAll,$byteIn,$byteOut) If ($? -ne $true) {exit}}$ErrorActionPreference = "stop"#-----------------------------(2) Secondly, the script doesn't pick up the local ip address for my USBInternet connection, if it's not specified (perhaps understandably sinceit's a USB connection rather than via a network card). Anyhow something likethis works fine ....$Winsock = New-Object -ComObject "MSWinsock.Winsock"$localIP = $Winsock.LocalIPOtherwise it works great, and I'm sure it will prove useful. Thanks forsharing.--Jon
My System SpecsSystem Spec
Old 11-19-2007   #3 (permalink)
Jon
Guest


 

Re: Packet Sniffer script

The second portion of that seems to have gotten hashed up .. so here's how
it SHOULD have read ....


2) Secondly, the script doesn't pick up the local ip address for my USB
Internet connection, if it's not specified (perhaps understandably sinceit's
a USB connection rather than via a network card). Anyhow something likethis
works fine ....


$Winsock = New-Object -ComObject "MSWinsock.Winsock"
$localIP = $Winsock.LocalIP



Otherwise it works great, and I'm sure it will prove useful. Thanks for
sharing.


--
Jon

My System SpecsSystem Spec
Old 11-19-2007   #4 (permalink)
Robbie Foust
Guest


 

Re: Packet Sniffer script

Hi Jon,

Thanks for the feedback. That iocontrol error is odd...I haven't run
into that problem. I'll investigate and see if I can figure out what
may be causing it. Otherwise, I'll just modify the code with your
changes. I also like your suggestion for obtaining the local IP.

One thing I didn't mention was that I know the script doesn't work on
w2k8 as is because w2k8 has ipv6 installed by default, and the way I'm
determining the local ip wasn't quite working the way I wanted.
Hopefully your suggestion will fix that, but if not, I'll find a
workaround and post it when I have a solution.

- Robbie


My System SpecsSystem Spec
Old 11-19-2007   #5 (permalink)
Jon
Guest


 

Re: Packet Sniffer script

"Robbie Foust" <rfoust@xxxxxx> wrote in message
news:2b4a25f3-b4db-44eb-80d2-c80727dd42f0@xxxxxx
Quote:

> Hi Jon,
>
> Thanks for the feedback. That iocontrol error is odd...I haven't run
> into that problem. I'll investigate and see if I can figure out what
> may be causing it. Otherwise, I'll just modify the code with your
> changes. I also like your suggestion for obtaining the local IP.
>
> One thing I didn't mention was that I know the script doesn't work on
> w2k8 as is because w2k8 has ipv6 installed by default, and the way I'm
> determining the local ip wasn't quite working the way I wanted.
> Hopefully your suggestion will fix that, but if not, I'll find a
> workaround and post it when I have a solution.
>
> - Robbie
>
>

Hi Robbie


Just been playing a bit more with this, and I've noticed that if I set


$byteIn[0] = 3 (corresponding to RCVALL_IPLEVEL - ie capture at the IP
level in non-promiscuous mode ???)

in the original script as opposed to

$byteIn[0] = 1 ( corresponding to promiscuous mode RCVALL_ON ??? )


then I don't get any errors with the script concerning my Internet
connection via modem, but still seem to get the packets captured to the
level I was looking for.

So it's perhaps the 'promiscuous mode' aspect to it that's causing the error
in my case, whereas capture at the ip level is ok.

I basically based the value of 3 from a guessed value for the
RCVALL_IPLEVEL option in the RCVALL_VALUE enumeration on this page

Winsock IOCTLs
http://msdn2.microsoft.com/en-us/library/bb736550.aspx

so I may well be off on completely the wrong track with that (in which case
please feel free to correct me), but hey it seems to work ;-)


So thanks again.


[ NB I've only tested it on Vista too, which I probably should also have
mentioned. ]

--
Jon



My System SpecsSystem Spec
Old 11-19-2007   #6 (permalink)
Kuma
Guest


 

Re: Packet Sniffer script

On Nov 20, 7:30 am, "Jon" <Email_Addr...@xxxxxx> wrote:
Quote:

> "Robbie Foust" <rfo...@xxxxxx> wrote in message
>
> news:2b4a25f3-b4db-44eb-80d2-c80727dd42f0@xxxxxx
>
>
>
>
>
Quote:

> > Hi Jon,
>
Quote:

> > Thanks for the feedback. That iocontrol error is odd...I haven't run
> > into that problem. I'll investigate and see if I can figure out what
> > may be causing it. Otherwise, I'll just modify the code with your
> > changes. I also like your suggestion for obtaining the local IP.
>
Quote:

> > One thing I didn't mention was that I know the script doesn't work on
> > w2k8 as is because w2k8 has ipv6 installed by default, and the way I'm
> > determining the local ip wasn't quite working the way I wanted.
> > Hopefully your suggestion will fix that, but if not, I'll find a
> > workaround and post it when I have a solution.
>
Quote:

> > - Robbie
>
> Hi Robbie
>
> Just been playing a bit more with this, and I've noticed that if I set
>
> $byteIn[0] = 3 (corresponding to RCVALL_IPLEVEL - ie capture at the IP
> level in non-promiscuous mode ???)
>
> in the original script as opposed to
>
> $byteIn[0] = 1 ( corresponding to promiscuous mode RCVALL_ON ??? )
>
> then I don't get any errors with the script concerning my Internet
> connection via modem, but still seem to get the packets captured to the
> level I was looking for.
>
> So it's perhaps the 'promiscuous mode' aspect to it that's causing the error
> in my case, whereas capture at the ip level is ok.
>
> I basically based the value of 3 from a guessed value for the
> RCVALL_IPLEVEL option in the RCVALL_VALUE enumeration on this page
>
> Winsock IOCTLshttp://msdn2.microsoft.com/en-us/library/bb736550.aspx
>
> so I may well be off on completely the wrong track with that (in which case
> please feel free to correct me), but hey it seems to work ;-)
>
> So thanks again.
>
> [ NB I've only tested it on Vista too, which I probably should also have
> mentioned. ]
>
> --
> Jon- Hide quoted text -
>
> - Show quoted text -
Tried this on XP SP2 but it seems that the first 2 characters of the
Data are chopped off for some reason.
My System SpecsSystem Spec
Old 11-19-2007   #7 (permalink)
Jon
Guest


 

Re: Packet Sniffer script

"Kuma" <kumasan76@xxxxxx> wrote in message
news:1d7794dc-34f0-4cd9-bd70-5fe7c5898855@xxxxxx
Quote:

> Tried this on XP SP2 but it seems that the first 2 characters of the
> Data are chopped off for some reason.


True, there does seem to be the odd byte missing.

Possibly some of the formulae in the ReadBytes(...) portions of the lines
with

$Data = .....

need to be reviewed (??).

--
Jon


My System SpecsSystem Spec
Old 11-20-2007   #8 (permalink)
Robbie Foust
Guest


 

Re: Packet Sniffer script

Ok, I fixed a few bugs and changed some things. (download the script
again at the same link). I found the off-by-2-bytes bug (I was
comparing words, not bytes. oops). Also I'm now scraping the output
of "route print 0*" to get the local IP address associated with the
default route. The problem with using "mswinsock.winsock" is that it
isn't installed by default, otherwise I'd use it. :-)

Jon, I think the problem you're seeing with promiscuous mode is that
your driver probably doesn't support it, so when you use
RCVALL_IPLEVEL, you're only receiving traffic that is destined for
your machine. Promiscuous mode receives all traffic, even if it isn't
destined for your system. The only way around that is to use a
driver shim, so for that you might as well download winpcap. ;-) Or
just settle for receiving only traffic destined for your system (which
is probably fine for what most of us use a packet capture program for
anyway).

I'll see if I can come up with a way to determine if a driver supports
that mode or not, and maybe fall back to the "iplevel" option.

Hope this helps,

- Robbie
My System SpecsSystem Spec
Old 11-20-2007   #9 (permalink)
Jon
Guest


 

Re: Packet Sniffer script

New script is working well.

Noticed that you've modified the IP Header and TCP Header sections, and in
the process resolved the 'missing data' issue that Kuma referred to. Also a
clever way of getting the local ip address.

Yes, I'm not overly bothered (if at all) about the promiscuous mode aspect
for my Internet connection. As you say, I pretty much exclusively use a
packet sniffer to keep tabs on what traffic is flowing to / from my machine
(for which ip level capture seems perfectly adequate), and for peeking
inside TCP packets to see what they contain. This script is now working well
with respect to both uses.

You've also taught me a lot about how all this works via your well-commented
script and this thread, and some of the articles which all this has
encouraged me to read - which I suspect we all appreciate far more than just
downloading some 'closed source' tool and learning nothing from it.

So keep up the good work!

--
Jon


"Robbie Foust" <rfoust@xxxxxx> wrote in message
news:7cff6bcb-5859-4d01-9e80-fab9696e1632@xxxxxx
Quote:

> Ok, I fixed a few bugs and changed some things. (download the script
> again at the same link). I found the off-by-2-bytes bug (I was
> comparing words, not bytes. oops). Also I'm now scraping the output
> of "route print 0*" to get the local IP address associated with the
> default route. The problem with using "mswinsock.winsock" is that it
> isn't installed by default, otherwise I'd use it. :-)
>
> Jon, I think the problem you're seeing with promiscuous mode is that
> your driver probably doesn't support it, so when you use
> RCVALL_IPLEVEL, you're only receiving traffic that is destined for
> your machine. Promiscuous mode receives all traffic, even if it isn't
> destined for your system. The only way around that is to use a
> driver shim, so for that you might as well download winpcap. ;-) Or
> just settle for receiving only traffic destined for your system (which
> is probably fine for what most of us use a packet capture program for
> anyway).
>
> I'll see if I can come up with a way to determine if a driver supports
> that mode or not, and maybe fall back to the "iplevel" option.
>
> Hope this helps,
>
> - Robbie
My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
IP Packet Sniffer Script dmex Beta Software 0 06-30-2008 09:43 PM
Packet sniffer and packet creator Marco Shaw PowerShell 3 12-10-2007 09:27 PM
HTTP Sniffer UnknownTBeast Vista networking & sharing 1 11-05-2007 05:31 AM
HTTP Sniffer UnknownTBeast Vista General 2 11-04-2007 07:27 PM
Ethereal (packet sniffer) CZ Vista General 10 11-03-2006 12:51 PM


Update your Vista Drivers Update Your Vista Drivers Now!!

Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008