How To Read A PCap File From Wireshark With C++ - Rhyous
How To Read A PCap File From Wireshark With C++ - Rhyous
How To Read A PCap File From Wireshark With C++ - Rhyous
Rhyous
Knight of the Code
In my Computer Security class I am taking as part of my Masters of Computer Science course, we need to parse a Pcap dump file.
Prerequisites
It is expected you have Visual Studio 2010 already. It may work the same with Visual C++ 2010.
02/06/2013
I already have post on creating a WinPcap project in Visual Studio and getting it to compile, so follow it. How to compile WinPcap with Visual Studio 2010?
/ /N o t e :e r r b u fi np c a p _ o p e nf u n c t i o n si sa s s u m e dt ob ea b l et oh o l da tl e a / / P C A P _ E R R B U F _ S I Z Ei sd e f i n e da s2 5 6 . / /h t t p : / / w w w . w i n p c a p . o r g / d o c s / d o c s _ 4 0 _ 2 / h t m l / g r o u p _ _ w p c a p _ _ d e f . h t m l c h a re r r b u f f [ P C A P _ E R R B U F _ S I Z E ] ; / * *S t e p4-O p e nt h ef i l ea n ds t o r er e s u l ti np o i n t e rt op c a p _ t * /
/ /U s ep c a p _ o p e n _ o f f l i n e / /h t t p : / / w w w . w i n p c a p . o r g / d o c s / d o c s _ 4 1 b 5 / h t m l / g r o u p _ _ w p c a p f u n c . h t m l # g 9 1 0 7 8 1 p c a p _ t*p c a p=p c a p _ o p e n _ o f f l i n e ( f i l e . c _ s t r ( ) ,e r r b u f f ) ;
2/5
www.rhyous.com/2011/11/13/how-to-read-a-pcap-file-from-wireshark-with-c/
02/06/2013
3 8 3 9 4 0 4 1 4 2 4 3 4 4 4 5 4 6 4 7 4 8 4 9 5 0 5 1 5 2 5 3 5 4 5 5 5 6 5 7 5 8 5 9 6 0 6 1 6 2 6 3 6 4 6 5 6 6 6 7 6 8 6 9 7 0 7 1 7 2 7 3 7 4 7 5 7 6 7 7 7 8 7 9 8 0 8 1 8 2 8 3 8 4 8 5 8 6 8 7 8 8 8 9
/ * *S t e p5-C r e a t eah e a d e ra n dad a t ao b j e c t * / / /C r e a t eah e a d e ro b j e c t : / /h t t p : / / w w w . w i n p c a p . o r g / d o c s / d o c s _ 4 0 _ 2 / h t m l / s t r u c t p c a p _ _ p k t h d r . h t m l s t r u c tp c a p _ p k t h d r* h e a d e r ; / /C r e a t eac h a r a c t e ra r r a yu s i n gau _ c h a r / /u _ c h a ri sd e f i n e dh e r e : / /C : \ P r o g r a mF i l e s( x 8 6 ) \ M i c r o s o f tS D K s \ W i n d o w s \ v 7 . 0 A \ I n c l u d e \ W i n S o c k 2 . h / /t y p e d e fu n s i g n e dc h a r u _ c h a r ; c o n s tu _ c h a r* d a t a ; / * *S t e p6-L o o pt h r o u g hp a c k e t sa n dp r i n tt h e mt os c r e e n * / u _ i n tp a c k e t C o u n t=0 ; w h i l e( i n tr e t u r n V a l u e=p c a p _ n e x t _ e x ( p c a p ,& h e a d e r ,& d a t a )> =0 ) { / /P r i n tu s i n gp r i n t f .S e ep r i n t fr e f e r e n c e : / /h t t p : / / w w w . c p l u s p l u s . c o m / r e f e r e n c e / c l i b r a r y / c s t d i o / p r i n t f / / /S h o wt h ep a c k e tn u m b e r p r i n t f ( " P a c k e t#% i \ n " ,+ + p a c k e t C o u n t ) ; / /S h o wt h es i z ei nb y t e so ft h ep a c k e t p r i n t f ( " P a c k e ts i z e :% db y t e s \ n " ,h e a d e r > l e n ) ;
/ /P r i n te a c ho c t e ta sh e x( x ) ,m a k es u r et h e r ei sa l w a y st w oc h a r a p r i n t f ( " % . 2 x" ,d a t a [ i ] ) ;
/ /A d dt w ol i n e sb e t w e e np a c k e t s p r i n t f ( " \ n \ n " ) ;
You are now reading packets in C++. Now you can start working on differentiating the packet types.
www.rhyous.com/2011/11/13/how-to-read-a-pcap-file-from-wireshark-with-c/ 3/5
02/06/2013
8 Comments
1. Chingon says: April 24, 2013 at 5:25 am hello Rhyous. You could help me with a project based on UPnP to connect to wireless router using the WPS protocol? I know it has nothing to do with your thread, but no one to turn to who understands C + + The project will compile without problems, no errors, just need to modify a bit the code, but I know very little about C + + 2. Chingon says: April 23, 2013 at 1:28 pm Solved! i added this line: "#pragma comment (lib, "wpcap")" Rhyous says: April 23, 2013 at 1:42 pm I am so glad you found a solution. 3. Chingon says: April 23, 2013 at 10:59 am Thanks but dont work: Error 1 error LNK2019: unresolved external symbol _pcap_next_ex that is referenced in function _main C:\Users\Chingon\Documents\VisualStudio2010\Projects\std\std.obj Error 2 Error LNK2019: unresolved external symbol _pcap_open_offline that is referenced in function _main C:\Users\Chingon\Documents\VisualStudio2010\Projects\std\std.obj 4. Proxy Servers read from a PCap file and print out IP addresses and port numbers in c, but my result seem wrong says: October 21, 2012 at 10:10 am [...] I am reading a pcap file and I want to print out the ip address and port number of each packet. I am using code from www.tcpdump.org/pcap.htm and www.rhyous.com/2011/11/13/how-to-read-a-pcap-file-from-wireshark-with-c/. [...] 5. Emanuel Felipe says: October 17, 2012 at 2:14 pm
www.rhyous.com/2011/11/13/how-to-read-a-pcap-file-from-wireshark-with-c/ 4/5
02/06/2013
When I did Copy/Paste the code didn't work, le wild error in lines 66 and 70 just appeared. To fix you should delete the 'l' after %, and "your" code should work fine. toto says: October 29, 2012 at 9:53 am Replying with a pokemon quote is dumb, please stop using the internet. 6. How to compile WinPcap with Visual Studio 2010? | Rhyous says: November 13, 2011 at 7:07 pm [...] to compile and it should work. You are now ready to develop using WinPcap. Next: How to read a PCap file from Wireshark with C++ Category: C++ | Comment (RSS) [...]
www.rhyous.com/2011/11/13/how-to-read-a-pcap-file-from-wireshark-with-c/
5/5