Windows Mobile Support

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Sunday, 11 March 2012

Can we write a network sniffer in .NET using only .NET API?

Posted on 06:37 by Unknown
Impreuna cu un coleg de birou am fost curiosi sa vedem daca putem sa scriem un sniffer cu API de .NET, fara sa folosim alte librari.
Pe internet exista destul de multe librari care fac acest lucru. Cea mai populara am vazut ca este WinPCap. Pentru al putea folosii este nevoie sa instalam niste drivere care o sa ne permita sa accesam orice packet de date care circula pe fir.
Acuma sa vedem ce putem sa facem din .NET. Prin intermediul clasei Socket, putem sa ajungem sub layer-ul de transport si sa accesam layer-ul de retea( network). Atat .NET cat si sistemul de operate ne limiteaza accesul doar la packetele care au ca sursa sau destinatie calculatorul nostru. Nu va asteptati sa puteti accesa packete care nu va sunt destinate. Din ce am gasit pana acuma, nu este posibil sa interceptam pachetele de date dintre alte doua device-uri din reteaua noastra. Acest lucru se poate face din .NET daca ne folosim de un wrapper peste WinPCap.
Folosind direct API-ul .NET nu o sa putem sa facem un sniffer care sa intercepteze traficul din retea. Mai jos gasiti un exemplu de sniffer care intercepteaza traficul local:
Socket socket = new Socket(
AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.IP);
socket.Bind(new IPEndPoint(IPAddress.Parse("192.168.100.105"), 0));
socket.SetSocketOption(
SocketOptionLevel.IP,
SocketOptionName.HeaderIncluded,
true);
socket.IOControl(
IOControlCode.ReceiveAll,
new byte[4] {1, 0, 0, 0},
new byte[4] {1, 0, 0, 0});
socket.BeginReceive(
packetData,
0,
packetData.Length,
SocketFlags.None,
new AsyncCallback(OnPacketIntercept), null);
In cazul in care vrem sa prindem doar packetele pe un anumit tip de protocol, putem sa ne folosim de enumeratie ProtocalType, care include atat TCP cat si UDP. Cand vrem sa facem un bind la o anumita adresa trebuie sa avem grija ca aceasta adresa fie a calculatorului nostru. In cazul in care incercati sa folositi o alta adresa veti primii o eroare care va avertizeaza ca adresa este incompatibila cu protocolul selectat.
Este destul de important sa setati ca header-ul pachetului sa fie inclus. Din header o sa vedem putin mai tarziu cum putem extrage informatii pretioase precum ip sursa, ip destinatie, protocol etc. Trebuie sa avem un sir de biti care o sa fie folositi pentru a scrie continutul pachetului. In cazul meu, packetData reprezinta acest sir de biti.
De fiecare data cand un pachet este primit sau trimis metoda OnPacketIntercept o sa fie apelata. Chiar daca continutul la packet o sa se afle in variabila packetData, avem nevoie sa stim care este lumgimea pachetului. Acest lucru il putem face in metoda OnPacketIntercept prin
 int paketLenght = mainSocket.EndReceive(ar);
Dupa acest pas putem sa incarcam sirul de biti intr-un memory stream si sa extragem date. Nu o sa descriu fiecare informatie din header. In link-ul dat mai jos puteti sa gasiti informatiile pe care le contine un IP Packet Header si locatia acestora in sirul de biti
http://en.wikipedia.org/wiki/Network_packet#Example:_IP_packets
Iar la urmatoarea adresa o sa gasiti o implementarea a unei clase care pe baza unui sir de biti extrage toate informatii din header-ul unui packet http://www.koders.com/csharp/fid779A18B94E1A0885E4569E99B6E0CB9339E1D268.aspx?s=treeview
Pentru a putea primii urmatorul packet, este nevoie sa apelam din nou socket.BeginReceive in interiorul metodei OnPacketIntercept.

Daca dorim sa interceptam pachetele care nu ne sunt destinate noua este nevoie sa ne folosim de o librarie precum WinPCap. Din ce am vazut eu pana acuma nu avem posibilitatea sa accesam in mod direct aceste packete si nici nu am aveam cum.
Daca vreti sa aflati mai multe despre socket-uri in .NET va recomand urmatoarea pagina: http://msdn.microsoft.com/en-us/library/orm-9780596527570-03-14.aspx
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in sniffer, socket | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • Service Bus Topic - Automatic forward messages from a subscription to a topic
    Windows Azure Service Bus Topic is a service that enables us to distribute the same messages to different consumers without having to know e...
  • CDN is not the only solution to improve the page speed - Reverse Caching Proxy
    I heard more and more often think like this: “If your website is to slow, you should use a CDN.” Great, CDN is THE solution for any kind of ...
  • Content Types - Level 6: Rich Media
    Level 6: Rich Media NOTE: This is part 7 of 7 and the conclusion of this continuing series; please see earlier posts for more background inf...
  • Publishing our CellCast Widget for iPad
    The rush has been on this week as our development team worked to design a new version of our CellCast Widget specifically for Apple's up...
  • Patterns in Windows Azure Service Bus - Message Splitter Pattern
    In one of my post about Service Bus Topics from Windows Azure I told you that I will write about a post that describe how we can design an a...
  • E-Learning Vendors Attempt to Morph Mobile
    The sign should read: " Don't touch! Wet Paint !" I had a good chuckle today after receiving my latest emailed copy of the eLe...
  • SQL - UNION and UNION ALL
    I think that all of us used until now UNION in a SQLstatement. Using this operator we can combine the result of 2 queries. For example we wa...
  • Cum sa salvezi un stream direct intr-un fisier
    Cred ca este a 2-a oara când întâlnesc aceasta cerința in decurs de câteva săptămâni. Se da un stream și o locație unde trebuie salvat, se c...
  • Task.Yield(...), Task.Delay(...)
    I think that a lot of person already heard about these new methods. In this post I want to clarify some things about these new methods that ...
  • Content Types - Level 4: Reference
    Level 4: Reference Materials & Static Content NOTE: This is part 5 of 7 in a continuing series; please see earlier posts for more backgr...

Categories

  • .NET
  • .NET nice to have
  • #if DEBUG
  • 15 iunie 2011
  • 15 octombrie 2011
  • 2011
  • abstracta
  • action
  • adaugare
  • ajax
  • Amsterdam
  • Android
  • aplicatii
  • App Fabric
  • Apple iSlate
  • array
  • as
  • ASP.NET
  • AsReadOnly
  • Assembly comun
  • async
  • Asynchronous programming
  • asyncron
  • Autofac
  • AutoMapper
  • az
  • Azure
  • Azure AppFabric Cache
  • Azure backup solution
  • Azure Storage Explorer
  • azure. cloud
  • backup
  • BCP utility
  • bing maps v7
  • BitArray
  • BlackBerry
  • blob
  • BlobContainerPublicAccessType
  • breakpoint
  • bucuresti
  • C#
  • cache
  • CallerMemberName
  • CellCast
  • Certificate
  • CES
  • change
  • ChannelFactory
  • clasa
  • classinitialize
  • clean code
  • click event
  • close
  • Cloud
  • Cluj
  • cluj-napoca
  • Code contracts
  • code retrat
  • codecamp
  • CollectionAssert
  • Compact Edition
  • compara
  • Comparer T .Default
  • CompareTo
  • comparison
  • comunitate
  • concurs
  • Conditional attribute
  • configurare
  • connection string
  • container
  • content type
  • control
  • Convert
  • convertAll
  • convertor
  • cross platform
  • CRUD
  • css
  • custom properties
  • custom request
  • DACPAC
  • Daniel Andres
  • data sync service
  • database
  • date time
  • datetime
  • debug
  • default
  • delegate
  • dependency injection
  • deploy
  • DeploymentItem
  • design patterns
  • Dev de Amsterdam
  • development stoage
  • dictionary
  • diferente
  • digging
  • director
  • Directory.Exist
  • disable
  • dispatcher
  • dispose
  • dropdown
  • dynamic
  • EF
  • email
  • encoding
  • entity framework
  • enum
  • enumerable
  • Environment.NewLine
  • error
  • error 404
  • error handling
  • eveniment
  • event
  • ews
  • excel
  • exception
  • exchange
  • exita
  • explicit
  • export
  • extension
  • field
  • File.Exist
  • finalize
  • fire and forget
  • Fluent interface pattern
  • format
  • func
  • GC.SuppressFinalize
  • generic
  • getdirectoryname
  • globalization
  • gmail
  • hackathon
  • Hadoop
  • handle
  • HTML
  • html 5
  • Html.ActionLink
  • http://www.blogger.com/img/blank.gif
  • HttpModule
  • IComparable
  • IE
  • ienumerable
  • IIS
  • image
  • implicit
  • import
  • int
  • internationalization
  • Internet Explorer
  • interop
  • Ioc
  • IP Filter
  • iPhone
  • iQuest
  • IStructuralEquatable
  • ITCamp
  • itspark
  • java script
  • javascript
  • July 2012
  • KeyedByTypeCollection
  • KeyNotFoundException
  • Kinect SDK
  • lambda expression
  • LightSwitch Microsoft Silverlight
  • linq
  • list
  • lista
  • lista servicii
  • liste
  • Live Connect
  • Live ID
  • load
  • localization
  • lock
  • m-learning
  • MAC
  • Mango
  • map
  • mapare
  • mapare propietati
  • messagequeue
  • meta properties
  • method
  • MethodImpl
  • Metro App
  • Microsoft
  • Microsoft Sync Framework
  • mlearning
  • mlearning devices
  • Mobile Apps
  • mobile in the cloud
  • mobile learning
  • mobile services
  • Mobile Web
  • mongoDb
  • monitorizare
  • msmq
  • multitasking
  • MVC
  • MVC 3
  • MVVM
  • namespace
  • nextpartitionkey
  • nextrowkey
  • Ninject
  • nivel acces
  • no result
  • normalize
  • nosql
  • null expcetion
  • null object pattern
  • NullReferenceException
  • OAuth API
  • office
  • offline
  • Open ID
  • openhackeu2011
  • operations
  • operator
  • optimization
  • option
  • outputcache
  • OutputCacheProvider
  • override
  • paginare
  • pagination
  • path
  • persistare
  • Portable Library tool
  • Post event – CodeCamp Cluj-Napoca
  • predicate
  • predictions
  • prezentare
  • process
  • proiect
  • property
  • propietati
  • query
  • ReadOnlyCollection
  • ReadOnlyDictionary
  • referinta
  • reflection
  • remote
  • reply command
  • request
  • request response
  • resouce
  • REST
  • REST Client
  • RESTSharp
  • ronua
  • rss
  • rulare
  • salvare in fisier
  • sc
  • schimbare timp
  • select
  • select nodes
  • send
  • serializare
  • serialization
  • Server.Transfer. Resposen.Redirect
  • service bus
  • ServiceBase
  • servicecontroller
  • sesiune
  • session
  • Session_End
  • Session_Start
  • setup
  • Sibiu
  • signalR
  • Silverlight
  • sincronizare
  • Single Responsibility Principle
  • SkyDrive
  • skype
  • smartphones
  • smtp
  • Snapguide
  • sniffer
  • socket
  • solid
  • spec#
  • sql
  • Sql Azure
  • SQL CE
  • sql server 2008 RC
  • SRP
  • startuptype
  • stateful
  • stateless
  • static
  • stergere
  • store
  • store procedure
  • stream
  • string
  • string.join
  • struct
  • StructuralEqualityComparer
  • submit
  • switch
  • Symbian
  • Synchronized
  • system
  • tabele
  • table
  • techEd 2012
  • tempdata
  • test
  • testcleanup
  • testinitialize
  • testmethod
  • thread
  • timer
  • ToLower
  • tool
  • tostring
  • Total Cost Calculator
  • trace ASP.NET
  • transcoding
  • tuplu
  • tutorial
  • TWmLearning
  • type
  • unit test
  • unittest
  • UrlParameter.Optional
  • Validate
  • validation
  • verificare
  • video
  • view
  • ViewBag
  • virtual
  • visual studio
  • VM role
  • Vunvulea Radu
  • wallpaper
  • WCF
  • WebBrower
  • WebRequest
  • where clause
  • Windows
  • windows 8
  • Windows Azure
  • Windows Azure Service Management CmdLets
  • windows live messenger
  • Windows Mobile
  • Windows Phone
  • windows service
  • windows store application
  • Windows Task
  • WinRT
  • word
  • workaround
  • XBox
  • xml
  • xmlns
  • XNA
  • xpath
  • YMesseger
  • Yonder
  • Zip

Blog Archive

  • ►  2013 (139)
    • ►  November (17)
    • ►  October (12)
    • ►  September (10)
    • ►  August (7)
    • ►  July (8)
    • ►  June (15)
    • ►  May (12)
    • ►  April (17)
    • ►  March (16)
    • ►  February (9)
    • ►  January (16)
  • ▼  2012 (251)
    • ►  December (9)
    • ►  November (19)
    • ►  October (26)
    • ►  September (13)
    • ►  August (35)
    • ►  July (28)
    • ►  June (27)
    • ►  May (24)
    • ►  April (18)
    • ▼  March (17)
      • What is HTML5 for native Windows 8 applications
      • How to use C# library from JavaScript in Windows 8
      • Structure and "Cannot modify the return value of X...
      • Post event - Intâlnire CodeCamp la Cluj-Napoca - 2...
      • How to get friendly format for GetType().Name
      • One way to persist objects in isolated storage
      • Can we mock an extension method?
      • Access a property using reflection
      • How to extract a file extension from a string path
      • Intâlnire CodeCamp la Cluj-Napoca - 24 martie 2012
      • Can we write a network sniffer in .NET using only ...
      • Resources from base class
      • How to improve our MVVM application using CallerMe...
      • In Praise of “The New iPad 4G HD 3”
      • One way to use Dispatcher in a Silverlight applica...
      • How to access RSS feeds from code
      • How to compare two generic dictionaries
    • ►  February (20)
    • ►  January (15)
  • ►  2011 (127)
    • ►  December (11)
    • ►  November (20)
    • ►  October (8)
    • ►  September (8)
    • ►  August (8)
    • ►  July (10)
    • ►  June (5)
    • ►  May (8)
    • ►  April (9)
    • ►  March (14)
    • ►  February (20)
    • ►  January (6)
  • ►  2010 (26)
    • ►  December (1)
    • ►  November (1)
    • ►  October (1)
    • ►  June (2)
    • ►  May (1)
    • ►  April (4)
    • ►  March (1)
    • ►  February (1)
    • ►  January (14)
Powered by Blogger.

About Me

Unknown
View my complete profile