ket

所属分类:TCP/IP协议栈
开发工具:Visual C++
文件大小:136KB
下载次数:1
上传日期:2013-12-02 13:37:16
上 传 者钱小小
说明:  此源码演示了如何通过原始套接字来模拟tcp包,对winsock网络编程及tcp/ip协议的学习 有帮助
(This source code demonstrates how to simulate tcp packets over raw sockets for winsock network programming and tcp/ip protocol learning to help)

文件列表:
tcpip_lib33\AsyncSocket.cpp (13877, 2001-11-09)
tcpip_lib33\AsyncSocket.h (6874, 2001-11-25)
tcpip_lib33\CriticalAutoRelease.cpp (2512, 2001-11-08)
tcpip_lib33\CriticalAutoRelease.h (2384, 2001-11-29)
tcpip_lib33\DebugHeap.h (509, 2001-11-08)
tcpip_lib33\ErrorHandler.cpp (5243, 2001-11-09)
tcpip_lib33\ErrorHandler.h (4419, 2001-11-09)
tcpip_lib33\FileLog.cpp (3568, 2001-11-08)
tcpip_lib33\FileLog.h (2630, 2001-09-15)
tcpip_lib33\GenericCriticalSection.cpp (1874, 2001-11-08)
tcpip_lib33\GenericCriticalSection.h (2219, 2001-10-29)
tcpip_lib33\GenericEvent.cpp (1863, 2001-11-09)
tcpip_lib33\GenericEvent.h (2189, 2001-11-09)
tcpip_lib33\GenericMutex.cpp (1844, 2001-11-08)
tcpip_lib33\GenericMutex.h (2312, 2001-11-11)
tcpip_lib33\GenericSemaphore.cpp (1902, 2001-11-08)
tcpip_lib33\GenericSemaphore.h (2357, 2001-10-04)
tcpip_lib33\GenericThread.cpp (2999, 2001-11-08)
tcpip_lib33\GenericThread.h (3546, 2001-10-09)
tcpip_lib33\ICMPSocket.cpp (12322, 2001-11-30)
tcpip_lib33\ICMPSocket.h (6086, 2001-11-30)
tcpip_lib33\ICMPSocketAsync.cpp (3633, 2001-11-08)
tcpip_lib33\ICMPSocketAsync.h (2476, 2001-08-27)
tcpip_lib33\Interfaces.cpp (4736, 2001-11-08)
tcpip_lib33\Interfaces.h (2806, 2001-08-27)
tcpip_lib33\IPCrafter.cpp (3887, 2001-11-08)
tcpip_lib33\IPCrafter.h (3260, 2001-09-18)
tcpip_lib33\KomodiaSettings.h (291, 2001-08-27)
tcpip_lib33\logo.gif (11080, 2000-05-13)
tcpip_lib33\MutexAutoRelease.cpp (2791, 2001-11-11)
tcpip_lib33\MutexAutoRelease.h (2476, 2001-11-29)
tcpip_lib33\OSManager.cpp (2841, 2001-11-09)
tcpip_lib33\OSManager.h (2796, 2001-11-09)
tcpip_lib33\ScreenLog.cpp (2409, 2001-11-08)
tcpip_lib33\ScreenLog.h (2296, 2001-09-15)
tcpip_lib33\SniffSocket.cpp (2702, 2001-11-08)
tcpip_lib33\SniffSocket.h (2487, 2001-08-27)
tcpip_lib33\SocketThreadManager.cpp (16248, 2001-11-09)
tcpip_lib33\SocketThreadManager.h (4561, 2001-11-29)
... ...

Index (v3.3) ---------- 1. What is this project ? 2. Who are we ? 3. Requirments ! 4. Architecture. 5. Library description. 6. Samples. 7. What's next ? 8. Contact us. 1. What is this project ? ------------------------- When I first started to learn about the world of security (and hacking) I had some knowledge of basic exploits and networking. I knew that if I want to find exploits my self I will have to deal with the nuts and bolts of the networking protocols. And so on, I began investigating the matter until I found sockets 2. Altough you must have nt2k to run this. (soon everyone will have) After a 2 month of writing this library. (you won't believe how many little things like wrong bits, slowed my pace) I'm releasing it so other programmers/hackers can learn/use this library. I consider this library to be a milestone in the learning of socket2 on W2K platform. (I hope you consider this as well when you use it) The library is very developed since it's first apperance, and your feedback is very important to me. Respectfully, Barak Weichselbaum 2. Who are we ? --------------- We are a new group at the scene of hacking/security, you can visit us at : http://www.komodia.com 3. Requirments ! ---------------- OS : win2k/XP (All of the regular networking works on all the platform (you need to install winsock2 update on 95), but the spoofing features onlt as stated before) To compile : you will need the recent DDK or SDK for win2k/XP. (Recomended) C++ : Please change the setting to multithreaded (go to project->settings, choose c++, and on the category tab select "code generation", change the runtime library to "debug multithreaded" (or just multithreaded if it's the release version) (On MFC it's mulithreaded by default) Link: add ws2_32.lib to the input list (link settings) Console mode: add the following lines (seen in the console samples) #include #include On a newer version of include files (like Platform SDK, or DDK), you need to put the following lines, in the stdafx.h: #include #include When you want to add the library to your code there are two options: 1. Add the relevant files into your project. 2. Link with my two libraries (That already contain the files), If you decide to link with them, make sure that if you are using MFC, choose the MFC configuration (debug), or MFC release (release), since the multithreaded setting for MFC are different from the one I'm using) 4. Architecture. ----------------------- This library uses sockets 2. It has two main features: 1. It opens up a raw socket, and sets it in a way it alows to send raw IP headers. (so you can do IP spoofing, and play with the bits and bolts of the protocol) 2. You can use it as a regular sockets, why - when I programmed my ATL server, I tried to open a socket, but since the MFC frameworks takes the instance with AfxGetInstance, the ATL server crashed, my application takes the application instance dynamicly. We change alot since the last version, we redesigned the OOD in order to acheive greater control and scalability. Instead of CSpoofSocket (as base class), we created CSpoofBase, moved some non direct socket functionality to that class, and added error handlers to the entire library. We implemented some of the classes using multiple inheritance, which has its drawbacks. (but we decided the pro's overweight the con's) Other classes in the library: (below CSpoofBase) 1. Class CSpoofSocket, the main IP carrier protocol. 2, Classes (Beneeth CSpoofSocket) CTCPSocket,CUDPSocket and CICMPSocket, Encapsulate the protocol they are named after. 3. Class CAsyncSocket, is the abstraction of asyn socket operations. 4. Class (Beneeth CAsyncSocket) CSniffSocket, Implementation of our sniffer socket. 5. Class CInterfaces, to retrieve a list of the available interfaces. 6. Beneeth CAsyncSocket and CTCPSocket,CUDPSocket and CICMPSocket, Comes : CTCPSocketAsync,CUDPSocketAsync and CICMPSocketAsync. These sockets are async sockets (no more SetBlock method) 7. CSocketThreadManager, manages the thread messaging. 8. CThreadPool, a worker thread manager. 9. CIPCrafter, CTCPCrafter, CUDPCrafter - for crafting packets. 10. Classes for multiplaform support (Critical section,mutex,sempahore,events,threads) There are two helper classes CIPOptions, CTCPOptions which control the options for both protocols (Currently IP options are supported for raw operations only) If you feel you need to have more control over the IP header, you can : A. Change the code. B. Inherit from its classes and override ConstructIPHeader. There are two basic ways to use the library : A. Raw sockets -------------- Allocate the desired class Set it's properties to raw Start messing around B. Regular messaging -------------------- Allocate the desired class Set the application instance (Instance is needed only for win9x apps) Create it Start working * When working with the library not that the classes CTCPSocket,CUDPSocket and CICMPSocket are blocking sockets, and CTCPSocketAsync,CUDPSocketAsync and CICMPSocketasync are async sockets. (Here my frameworks creates an handler for all the async requests, each time the socket status changes, it calls the appropriate OnSocketEvent i.e. for receive OnSocketReceive. In my samples I inherited from the classes (you have to because they are abstract virtual)) From V3.1, the handlers can be multithreaded (allocate an handler in each thread, this will allow faster proccessing of sockets). From V3.3, All methods that expect text IP (xXX.XXX.XXX.XXX) are overloaded to receive the unsigned long of the IP. 5. Library description. ----------------------- CSpoofBase ---------- Functions: static BOOL IsInitialized() --------------------------- Is the sockets initialized. Return value - True if initialized. void SetLocalLog(CSpoofLog *pLog) --------------------------------- Sets the local log. pLog - The log to use. static char FAR * LongToString(long lAddr) ------------------------------------------ Converts an IP address stored as long to string. lAddr - The address to convert. return value - The string address. void SetLog(CSpoofLog* pLog) ---------------------------- Sets the socket's log for the entire session (until shutdown is called) pLog - Your log class. static BOOL InitializeSockets(BOOL bMultiThreaded=FALSE,int iNumberOfThreads=0) ------------------------------------------------------------------------------- Must call on the beginning of your program. Initialize the socket engine. bMultithreaded - Create multiple number of threads. (FALSE means sockets runs in the context of the main thread, however it's safe to access from multiple threads) iNumberOfThreads - Number of threads, relevant only if multithreaded. return value - Non zero for success. static BOOL ShutdownSockets() ----------------------------- Clears the system resources, socket's and this library async mechanism. (If there are allocated async sockets, they will be deleted) return value - True for success. int GetLastError() const ------------------ return value - The last error that occured, zero if no error. Internal functions: static int GetNumberOfThreads() ------------------------------- Return value - Number of threads initialized. static BOOL IsMultiThreaded() ----------------------------- Return value - True when initialized as multithreaded. void ReportError(LPCSTR lpMethod) const --------------------------------- Reports an unknown error. lpMethod- The name of the calling method. static void ReportStaticError(LPCSTR lpClass,LPCSTR lpMethod) ------------------------------------------------------------- Report an error from a static member. lpClass - The calling class name. lpMethod - The calling method name. static void ReportStaticError(LPCSTR lpClass,LPCSTR lpMethod,LPCSTR lpMessage) ------------------------------------------------------------------------------ Report an error from a static member. lpClass - The calling class name. lpMethod - The calling method name. lpMessage - The message to report. virtual void NotifyShutdown() ----------------------------- Method for the derived class to inherit, will be called when sockets is shuttin down. void RegisterShutdown(CSpoofBase* pBase) ---------------------------------------- Registers a class to be notified when shutdown occurs. (1 class only per application) pBase - The class to be notified. void ReportError(LPCSTR lpMethod,LPCSTR lpMessage) const -------------------------------------------------- Report an application error. lpMethod - The name of the calling method. lpMessage - The error message. virtual void ReportError(LPSTR lpMethod,int iErrorCode) const ------------------------------------------------------- Report a socket error. lpMethod - The name of the calling method. iErrorCode - The socket error code. void SetName(LPCSTR lpName) const --------------------------- Set the name of the active class (for error reporting) lpName - The class name (string is copied and freed when dtor is called). void SetLastError(LPCSTR lpMethod) const ---------------------------------- Get the socket last error. lpMethod - The name of the calling method. void SetLastError(LPCSTR lpMethod,int iErrorCode) const ------------------------------------------------- Set a specific socket error. lpMethod - The calling method name. iErrorCode - The socket error code. CSpoofLog* GetLog() ------------------- Return value - The correct log (global or local). private variables: mutable int m_LastError - Last socket error that occured. static CSpoofLog* m_Log - Our log reporting class. CSpoofLog* m_LocalLog - Our local log. static BOOL m_Initialized - Is the socket system initialized ? mutable LPSTR m_lpClassName - Our class name (for error reporting). static CSpoofBase* m_pShutdownClass - Class to be notified when shutdown occurs. static BOOL m_bMultiThreaded - Are we multithreaded ? static int m_NumberOfThreads - Number of threads to allocate. CSpoofBase::CSpoofLog --------------------- Internal functions: virtual void ReportCatchError(LPCSTR lpClass,LPCSTR lpMethod,LPCSTR lpMessage)=0 ----------------------------------------------------------------------------- Reports an error captured from a catch phrase. lpClass - The class where the error occured. lpMethod - The method where the error occured. lpMessage - The error message. virtual void ReportInitiatedError(LPCSTR lpClass,LPCSTR lpMethod,LPCSTR lpMessage)=0 --------------------------------------------------------------------------------- Reports an error reported by the library (initiated, not catched) lpClass - The class where the error occured. lpMethod - The method where the error occured. lpMessage - The error message. virtual void ReportSocketError(LPCSTR lpClass,LPCSTR lpMethod,int iErrorCode)=0 ----------------------------------------------------------------------------- Reports a socket error. lpClass - The class where the error occured. lpMethod - The method where the error occured. iErrorCode - The socket error code. CSpoofSocket ------------ Functions: BOOL SetBroadcast(BOOL bBroadcast) ---------------------------------- Sets the socket to broadcast mode. bBroadcast - Switch broadcast on or off. Return value - True for success. BOOL CanWrite() const --------------- Checks if the socket is ready for writing. Return value - True when can write. BOOL CanRead() const -------------- Checks if the socket has data in it's buffer. Return value - True when there's data in the buffer. BOOL Shutdown(SocketShutdown eHow) ---------------------------------- Shutdown the socket - Disallow it to send/Receive or both (and not freeing its resources) eHow - How to shut it down (send/receive or both) unsigned short GetPeerPort() const ---------------------------- Return value - Port of the remote connected system. long GetPeerAddress() const --------------------- return value - Address of the remote address (Also ICMP and UDP) virtual BOOL Sniff(BOOL bSniff) ------------------------------- Set the socket to be a sniffer socket (recieve all packet on the binded interface - must bind) Return value - True of success. long ResolveDNS(LPCSTR lpAddress) const --------------------------------- Resolve a name IP address (www.xxx.com) and return the address. lpAddress - The address to resolve. Return value - zero when resolving failed, the address otherwise. BOOL ValidAddress(LPCSTR lpAddress) const ----------------------------------- Check if an address of the type xxx.yyy.zzz.*** is valid. lpAddress - the address to check. Return value - True when valid. virtual int Receive(char* buf,int bufLen) ----------------------------------------- Receives data from the socket buffer. buf - Buffer to receive data to. bufLen - Buffer length. return value - Number of bytes read, SOCKET_ERROR for error. CIPOptions* GetOptions() const ------------------------ Used to get the option structure the header contains. return value - options structure. void SetOptions(BOOL bOptions) ------------------------------ Allows options to be used within the socket. bOptions - True to allow options. void SetRaw(BOOL bRaw) ---------------------- Sets socket to raw or regular (need to be set before Create) bRaw - True for raw. void SetTTL(unsigned char ucTTL) -------------------------------- Set time to live on the packet. Could be used for tracing routes. unsigned short CalculatePseudoChecksum(const char *buf, int BufLength, LPCSTR lpDestinationAddress, int iPacketLength) --------------------------------------------------------- Calculate checksum for TCP and UDP which require calculating along a pseudo header. buf - The packet to checksum. BufLength - Size of the buf. lpDestinationAddress - Destination address of the packet. iPacketLength - Packet length. return value - The checksum. void SetSourceAddress(LPCSTR lpSourceAddress) --------------------------------------------- Set the source address of the socket. Calling bind before, will set the source address to the bind, but calling bind after won't change the value seted by the user. (this can be used to IP spoofing, setting the address to a diffrent one) virtual BOOL Close() -------------------- Close the socket and frees the resources. return value - True for success. virtual BOOL Bind(LPCSTR lpSourceAddress,int iPort=0) ----------------------------------------------------- Binds the socket to a specific network (i.e. nic). lpSourceAddress - The address to bind to. (Automaticly set source address as the bind address) iPort - The port to bind to. return value - True for success. virtual BOOL Send(LPCSTR lpDestinationAddress,char* buf,int bufLength,unsigned short usDestinationPort=0) -------------------------------------------------------------------------------------------- Send data using IP protocol. lpDestinationAddress - The destination address. buf - The buffer to send (can be other higher level protocols like tcp and udp) bufLength - Length of the buffer. usDestinationPort - Destination port. (TCP and UDP only) return value - True on success. BOOL Create(int iProtocol) -------------------------- iProtocol - The protocol to use. (defined in the header) return value - Non zero for success. Internal functions: void AssignSocket(SOCKET sok,unsigned char ucProtocol=IPPROTO_TCP) ----------------------------- Bind to a given socket. sok - The socket. ucProtocol - The protocol of the new socket. BOOL isRaw() const ------------ Check if this is a raw socket. Return value - True on raw socket. void SetProtocol(int iProtocol) ------------------------------- Adjust the protocol before socket creation. iProtocol - Protocol number (IPPROTO_IP ...) BOOL CheckSocketValid() const ----------------------- Checks if the socket is valid. Return value - True when socket is valid. unsigned short CalculateChecksum(const unsigned short* usBuf,int iSize) const ----------------------------------------------------------------------------- Calculate the IP header checksum for a given buffer. usBuf - The buffer to calculate the checksum to. iSize - Size of the given buffer. Return value - The calculated checksum. BOOL ValidSocket() const ------------------ Checks if the socket is valid. Return value - True when socket is valid. SOCKET getHandle() const ------------------ Return value - The socket handle. virtual void InitializeIP() --------------------------- Initialize private class members. virtual void SetIPHeaderAddress(LPIpHeader lpHead,LPCSTR lpSourceAddress,LPCSTR lpDestinationAddress) ----------------------------------------------------------------------------------------------------- Set the data within the given IP header. lpHead - The header to change. lpSourceAddress - The new source address for the header. lpDestinationAddress - The new destination address for the header. virtual LPIpHeader ConstructIPHeader (unsigned char ucProtocol, unsigned short usFragmentationFlags, unsigned char ucTTL, unsigned short usIdentification, unsigned char ucHeaderLength) -------------------------------------------------------------------- Create the IP header for the send. ucProtocol - The protocol we are sending. usFragmentationFlags - Fragmentation flags (Fragment/Never fragment). ucTTL - Packet time to live. usIdentification - How to ID our packet (needed for fragmentation). ucHeaderLength - The length of the header including IP options. Return value - The new constructed IP header. virtual void FinalIPHeader(LPIpHeader lpHead) --------------------------------------------- Notify that this is the last chance to modify the header before sending it. lpHead - The header. sockaddr_in pResolveDNS(LPCSTR lpAddress) const ----------------------------------------- Resolve the DNS address. lpAddress - the remote address to resolve. Return value - The resolved address. private variables: sockaddr_in m_ConnectedTo - Where the socket is connected to. CIPOptions* m_IPOptions - Our IP options. BOOL m_Options - Do we even have options. BOOL m_Raw - Is the socket raw. unsigned char m_TTL - Our time to live. unsigned char m_Protocol - Our protocol. LPCSTR m_SourceAddress - Our source address. SOCKET m_SpoofSocket - The socket handle. CIPOptions ---------- Functions: void AddOption_Option --------------------- Add ... ...

近期下载者

相关文件


收藏者