Check.cpp
上传用户:yitai_qhd
上传日期:2008-04-24
资源大小:31k
文件大小:6k
开发平台:

Visual C++

  1. #include "check.h"
  2. #include "packet.h"
  3. #include "windef.h"
  4. #include "stdio.h"
  5. #include "string.h"
  6. int CheckTcp(
  7. PIP_HEADER pIpHeader, 
  8. PTCP_HEADER pTcpHeader, 
  9. BOOLEAN IsSend, 
  10. UINT LookaheadBufferSize,
  11. PVOID pVoid
  12. )
  13. {
  14. NTSTATUS Status;
  15. NTSTATUS LogStatus;
  16.     UNICODE_STRING UnicodeFilespec;
  17. UNICODE_STRING UnicodeLogFile;
  18.     OBJECT_ATTRIBUTES ObjectAttributes;
  19. OBJECT_ATTRIBUTES LogObjectAttributes;
  20.     HANDLE FileHandle;
  21. HANDLE LogFileHandle;
  22. DWORD IP;
  23.     IO_STATUS_BLOCK Iosb;
  24. IO_STATUS_BLOCK LogIosb;
  25.     RtlInitUnicodeString(&UnicodeFilespec, L"\DosDevices\C:\ip.dat");
  26. RtlInitUnicodeString(&UnicodeLogFile, L"\DosDevices\C:\iplog.dat");
  27.     InitializeObjectAttributes(&ObjectAttributes,           // ptr to structure
  28.                                &UnicodeFilespec,            // ptr to file spec
  29.                                OBJ_CASE_INSENSITIVE,        // attributes
  30.                                NULL,                        // root directory handle
  31.                                NULL );                      // ptr to security descriptor
  32. InitializeObjectAttributes(&LogObjectAttributes,           // ptr to structure
  33.                                &UnicodeLogFile,            // ptr to file spec
  34.                                OBJ_CASE_INSENSITIVE,        // attributes
  35.                                NULL,                        // root directory handle
  36.                                NULL );                      // ptr to security descriptor
  37.     Status = ZwCreateFile(&FileHandle,                      // returned file handle
  38.                           (GENERIC_READ| SYNCHRONIZE),    // desired access
  39.                           &ObjectAttributes,                // ptr to object attributes
  40.                           &Iosb,                            // ptr to I/O status block
  41.                           0,                                // allocation size
  42.                           FILE_ATTRIBUTE_NORMAL,            // file attributes
  43.                           0,                                // share access
  44.                           FILE_SUPERSEDE,                   // create disposition
  45.                           FILE_SYNCHRONOUS_IO_NONALERT,     // create options
  46.                           NULL,                             // ptr to extended attributes
  47.                           0);                               // length of ea buffer
  48.     LogStatus = ZwCreateFile(&LogFileHandle,                      // returned file handle
  49.                           (FILE_APPEND_DATA| SYNCHRONIZE),    // desired access
  50.                           &LogObjectAttributes,                // ptr to object attributes
  51.                           &LogIosb,                            // ptr to I/O status block
  52.                           0,                                // allocation size
  53.                           FILE_ATTRIBUTE_NORMAL,            // file attributes
  54.                           0,                                // share access
  55.                           FILE_SUPERSEDE,                   // create disposition
  56.                           FILE_SYNCHRONOUS_IO_NONALERT,     // create options
  57.                           NULL,                             // ptr to extended attributes
  58.                           0);      
  59.     //
  60.     // Check the system service status
  61.     //
  62.     if( !NT_SUCCESS(Status) )
  63.         {
  64.       }
  65.     //
  66.     // Check the returned status too...
  67.     //
  68.     if(!NT_SUCCESS(Iosb.Status) )
  69.         {
  70.        }
  71.     Status = ZwReadFile(FileHandle,                   
  72.                          0,                           
  73.                          NULL,                        
  74.                          NULL,                        
  75.                          &Iosb,                       
  76.                          &IP,                      
  77.                          4,                
  78.                          0,                            
  79.                          NULL);                        
  80. if(IsSend)
  81. {
  82. while(!NT_SUCCESS(Status)&&!NT_SUCCESS(Iosb.Status))
  83. {
  84. Status = ZwReadFile(FileHandle,                   
  85.             0,                           
  86. NULL,                        
  87. NULL,                        
  88. &Iosb,                       
  89. &IP,                      
  90. 4,                
  91. 0,                            
  92. NULL);
  93. if(strcmp((char *)pIpHeader->SourceIp,(char *)IPTrans(IP)))
  94. {
  95. LogStatus = ZwWriteFile(LogFileHandle,                   
  96.             0,                           
  97. NULL,                        
  98. NULL,                        
  99. &Iosb,                       
  100. &IP,                      
  101. 4,                
  102. 0,                            
  103. NULL);
  104. return -1;
  105. }
  106. }
  107. }
  108. else
  109. {
  110. while(!NT_SUCCESS(Status)&&!NT_SUCCESS(Iosb.Status))
  111. {
  112. Status = ZwReadFile(FileHandle,                   
  113.             0,                           
  114. NULL,                        
  115. NULL,                        
  116. &Iosb,                       
  117. &IP,                      
  118. 4,                
  119. 0,                            
  120. NULL);
  121. if(strcmp((char *)pIpHeader->DestinationIp,(char *)IPTrans(IP)))
  122. {
  123. LogStatus = ZwWriteFile(LogFileHandle,                   
  124.             0,                           
  125. NULL,                        
  126. NULL,                        
  127. &Iosb,                       
  128. &IP,                      
  129. 4,                
  130. 0,                            
  131. NULL);
  132. return -1;
  133. }
  134. }
  135. }
  136.     //
  137.     // Well, That's all folks!
  138.     //
  139.     Status = ZwClose(FileHandle);
  140. return 1;
  141. }
  142. int CheckUdp(
  143. PIP_HEADER pIpHeader, 
  144. PUDP_HEADER pUdpHeader, 
  145. BOOLEAN IsSend,
  146. UINT LookaheadBufferSize,
  147. void *pVoid
  148. )
  149. {
  150. if(pUdpHeader->Length!=LookaheadBufferSize)
  151. return 1;
  152. return 0;
  153. }
  154. int CheckIcmp(
  155. PIP_HEADER pIpHeader, 
  156. PICMP_HEADER pIcmpHeader, 
  157. BOOLEAN IsSend,
  158. UINT LookaheadBufferSize 
  159. )
  160. {
  161. if(!IsSend) return -1;
  162. return 1;
  163. }
  164. unsigned char* IPTrans(DWORD IP)
  165. {
  166. int ip1=(IP&0x000000ff);
  167. int ip2=(IP&0x0000ff00)>>8;
  168. int ip3=(IP&0x00ff0000)>>16;
  169. int ip4=(IP&0xff000000)>>24;
  170. unsigned char Tmp[4];
  171. sprintf((char*)Tmp,"%c",(char)ip1);
  172. sprintf((char *)&Tmp[1],"%c",(char)ip2);
  173. sprintf((char *)&Tmp[2],"%c",(char)ip3);
  174. sprintf((char *)&Tmp[3],"%c",(char)ip4);
  175. return Tmp;
  176. }