00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "stdafx.h"
00037 #include "IPCrafter.h"
00038
00039 #include "ErrorHandlerMacros.h"
00040
00041 #include "SpoofSocket.h"
00042
00043 #ifdef _MEMORY_DEBUG
00044 #define new DEBUG_NEW
00045 #define malloc DEBUG_MALLOC
00046 static char THIS_FILE[] = __FILE__;
00047 #endif
00048
00049 KOMODIA_NAMESPACE_START
00050
00051 #define CIPCrafter_Class "CIPCrafter"
00052
00053 CIPCrafter::CIPCrafter() : CSpoofBase()
00054 {
00055 try
00056 {
00057
00058 SetName(CIPCrafter_Class);
00059
00060
00061 SetDefault();
00062 }
00063 ERROR_HANDLER("CIPCrafter")
00064 }
00065
00066 CIPCrafter::~CIPCrafter()
00067 {
00068 }
00069
00070 void CIPCrafter::SetFragmentationFlags(unsigned short usFragmentationFlags)
00071 {
00072 m_usFragmentationFlags=usFragmentationFlags;
00073 }
00074
00075 void CIPCrafter::SetIdentification(unsigned short usIdentification)
00076 {
00077 m_usIdentification=usIdentification;
00078 }
00079
00080 void CIPCrafter::SetHeaderLength(unsigned char ucHeaderLength)
00081 {
00082 m_ucHeaderLength=ucHeaderLength;
00083 }
00084
00085 void CIPCrafter::SetTypeOfService(unsigned char ucTypeOfService)
00086 {
00087 m_ucTypeOfService=ucTypeOfService;
00088 }
00089
00090 void CIPCrafter::SetDefault()
00091 {
00092 try
00093 {
00094 m_usFragmentationFlags=IpFragFlag_DONT_FRAG;
00095 m_usIdentification=(unsigned short)GetCurrentProcessId();
00096 m_ucHeaderLength=IPCRAFTER_DEFAULT_HEADER_SIZE;
00097 m_ucTypeOfService=IpService_ROUTINE;
00098
00099
00100 m_DefaultChecksum=TRUE;
00101 }
00102 ERROR_HANDLER("SetDefault")
00103 }
00104
00105 void CIPCrafter::SetChecksum(unsigned short usChecksum)
00106 {
00107 m_usChecksum=usChecksum;
00108 }
00109
00110 void CIPCrafter::SetUseDefaultChecksum(BOOL bDefault)
00111 {
00112 m_DefaultChecksum=bDefault;
00113 }
00114
00115 LPIpHeader CIPCrafter::ConstructCraftedIPHeader(unsigned char ucProtocol,
00116 unsigned short usFragmentationFlags,
00117 unsigned char ucTTL,
00118 unsigned short usIdentification,
00119 unsigned char ucHeaderLength)const
00120 {
00121 try
00122 {
00123
00124 LPIpHeader lpHead;
00125
00126
00127 unsigned char ucLength;
00128
00129 if (m_ucHeaderLength==IPCRAFTER_DEFAULT_HEADER_SIZE)
00130 ucLength=ucHeaderLength;
00131 else
00132 ucLength=m_ucHeaderLength;
00133
00134
00135 lpHead=CSpoofSocket::ConstructStaticIPHeader(ucProtocol,
00136 m_usFragmentationFlags,
00137 ucTTL,
00138 m_usIdentification,
00139 ucLength);
00140
00141
00142 if (!lpHead)
00143 return NULL;
00144
00145
00146
00147 lpHead->ucTypeOfService=m_ucTypeOfService;
00148
00149
00150 return lpHead;
00151 }
00152 ERROR_HANDLER_RETURN("ConstructCraftedIPHeader",NULL)
00153 }
00154
00155 char* CIPCrafter::GetCraftedPacket(const CSpoofSocket* pSocket,
00156 unsigned long ulDestinationAddress,
00157 const char* pBuffer,
00158 unsigned short usBufferSize,
00159 unsigned short& usTotalSize)const
00160 {
00161 try
00162 {
00163
00164 if (IpHeaderLength+(long)usBufferSize>65535)
00165 {
00166
00167 ReportError("GetCraftedPacket","Packet can't be greater then 65k!");
00168
00169
00170 return NULL;
00171 }
00172
00173
00174 unsigned char ucHeaderLength=IpHeaderLength;
00175
00176 if (pSocket->HasOptions())
00177 ucHeaderLength+=pSocket->GetOptions()->GetBufferLength();
00178
00179
00180 LPIpHeader lpHead=pSocket->ConstructIPHeader(pSocket->GetProtocol(),
00181 IpFragFlag_DONT_FRAG,
00182 pSocket->GetTTL(),
00183 (unsigned short)GetCurrentProcessId(),
00184 ucHeaderLength);
00185
00186
00187 pSocket->SetIPHeaderAddress(lpHead,
00188 pSocket->GetSourceAddress(),
00189 ulDestinationAddress);
00190
00191
00192 unsigned short usTotalLength;
00193 usTotalLength=ucHeaderLength+usBufferSize;
00194
00195
00196 lpHead->usTotalLength=htons(usTotalLength);
00197
00198
00199 char* pNewBuf;
00200 pNewBuf=new char[usTotalLength];
00201
00202
00203 memcpy(pNewBuf,
00204 lpHead,
00205 IpHeaderLength);
00206
00207
00208 if (pSocket->HasOptions())
00209 memcpy(pNewBuf+IpHeaderLength,
00210 pSocket->GetOptions()->GetBuffer(),
00211 pSocket->GetOptions()->GetBufferLength());
00212
00213
00214 if (pBuffer)
00215 memcpy(pNewBuf+ucHeaderLength,
00216 pBuffer,
00217 usBufferSize);
00218
00219
00220 lpHead->usChecksum=pSocket->CalculateChecksum((unsigned short*)pNewBuf,
00221 ucHeaderLength);
00222
00223
00224 pSocket->FinalIPHeader(lpHead);
00225
00226
00227 memcpy(pNewBuf,
00228 lpHead,
00229 IpHeaderLength);
00230
00231
00232 usTotalSize=usTotalLength;
00233
00234
00235 return pNewBuf;
00236 }
00237 ERROR_HANDLER_RETURN("GetCraftedPacket",NULL)
00238 }
00239
00240 KOMODIA_NAMESPACE_END