resume.c 4.87 KB
Newer Older
1 2 3 4 5 6 7
/*
 * RESUME
 *
 * resume.c - Dump y Resume de sesiones de escaneo
 *
 */

8
#define _GNU_SOURCE
9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include "dirb.h"


/*
 * DUMP: Vuelca a fichero el estado de una sesion de escaneo
 *
 */

void dump(void) {
  FILE *dumpfile;
  struct words *current;
  struct stat buffer;
  struct tm *ptr;
  time_t tm;
23 24 25 26 27
  char *home=NULL;
  char *dumppath=NULL;
  char *optionspath=NULL;
  char *wordlistpath=NULL;
  char *dirlistpath=NULL;
28 29 30 31 32 33 34

  tm = time(NULL);
  ptr = localtime(&tm);

  if(options.debuging>2) printf("[++] dump() Dumping Session State AT %s", asctime(ptr)); fflush(stdout);


35
  // Comprobamos si existe el directorio y sino intentamos crealo
36

37 38 39 40 41
  home = getenv("HOME");
  asprintf(&dumppath,"%s/%s", home, DUMP_DIR);
  asprintf(&optionspath, "%s/%s", home, OPTIONS_DUMP);
  asprintf(&wordlistpath, "%s/%s", home, WORDLIST_DUMP);
  asprintf(&dirlistpath, "%s/%s", home, DIRLIST_DUMP);
42

43 44
  if(stat(dumppath, &buffer)<0) {
    if(mkpath(dumppath, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)<0) {
45 46 47 48 49 50 51 52
      printf("\n(!) FATAL: Error creating resume directory: %s\n", DUMP_DIR);
      exit(-1);
      }
    }


  // Volcamos la estructura options

53 54
  unlink(optionspath);
  dumpfile=abrir_file(optionspath);
55 56 57 58 59 60
  fwrite(&options, sizeof(struct opciones), 1, dumpfile);
  fclose(dumpfile);


  // Volcamos la wordlist

61 62
  unlink(wordlistpath);
  dumpfile=abrir_file(wordlistpath);
63 64 65 66 67 68 69 70 71 72 73 74 75 76
  current=wordlist_base;

  while(current->siguiente!=0) {
    if(options.debuging>6) printf("[++++++] dump() WORD: %s\n", current->word);
    fwrite(current->word, strlen(current->word), 1, dumpfile);
    fwrite("\n", 1, 1, dumpfile);
    current=current->siguiente;
    }

  fclose(dumpfile);


  // Volvamos la dirlist

77 78
  unlink(dirlistpath);
  dumpfile=abrir_file(dirlistpath);
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
  current=dirlist_current;

  while(current->siguiente!=0) {
    if(options.debuging>6) printf("[++++++] dump() DIR: %s\n", current->word);
    fwrite(current->word, strlen(current->word), 1, dumpfile);
    fwrite("\n", 1, 1, dumpfile);
    current=current->siguiente;
    }

  fclose(dumpfile);


}


/*
 * RESUME: Restaura el estado de una sesion de escaneo
 *
 */

void resume(void) {
  FILE *desc;
101 102 103 104 105
  char *home=NULL;
  char *dumppath=NULL;
  char *optionspath=NULL;
  char *wordlistpath=NULL;
  char *dirlistpath=NULL;
106 107

  resuming=1;
108 109 110 111 112
  home = getenv("HOME");
  asprintf(&dumppath,"%s/%s",home,DUMP_DIR);
  asprintf(&optionspath, "%s/%s",home,OPTIONS_DUMP);
  asprintf(&wordlistpath, "%s/%s",home,WORDLIST_DUMP);
  asprintf(&dirlistpath, "%s/%s",home,DIRLIST_DUMP);
113 114 115

  // Recuperamos la estructura options

116 117
  if((desc=fopen(optionspath, "r"))==0) {
    printf("\n(!) FATAL: Error opening input file: %s\n", optionspath);
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    exit(-1);
    }
  fread(&options, sizeof(struct opciones), 1, desc);
  fclose(desc);


  // Inicializamos

  check_url(options.current_dir);
  get_options();
  init_exts();
  IMPRIME("CURRENT_BASE: %s\n", options.current_dir);
  IMPRIME("CURRENT_WORD: %s\n", options.current_word);
  IMPRIME("\n-----------------\n\n");


  // Mutations

  /*
  if(options.mutations_file) {
    muts_base=crea_wordlist_fich(options.mutation_file);
    } else if(options.mutations_list) {
    muts_base=crea_extslist(options.mutation_list);
    }
  */

  // Recuperamos la dirlist

146
  dirlist_current=crea_wordlist_fich(dirlistpath);
147 148 149 150 151 152 153 154
  dirlist_base=dirlist_current;
  while(dirlist_current->siguiente!=0) { dirlist_current=dirlist_current->siguiente; }
  dirlist_final=dirlist_current;
  dirlist_current=dirlist_base;


  // Recuperamos la wordlist

155
  wordlist_current=crea_wordlist_fich(wordlistpath);
156 157 158 159 160
  wordlist_base=wordlist_current;
  while(wordlist_current->siguiente!=0) { wordlist_current=wordlist_current->siguiente; }
  wordlist_final=wordlist_current;
  wordlist_current=wordlist_base;
  while(wordlist_current->siguiente!=0 && strncmp(wordlist_current->word, options.current_word, STRING_SIZE-1)!=0) { wordlist_current=wordlist_current->siguiente; }
161 162 163
  
  // Avanzamos la wordlist en 1
  wordlist_current=wordlist_current->siguiente;
164 165 166 167 168 169


  // Relanzamos el scan

  lanza_ataque(options.current_dir, wordlist_current);

170

171 172 173 174 175 176 177 178
  // Finalizamos

  cierre();
  exit(0);

}


179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
/*
 * MKPATH: Crea el el directorio de volcado recursivamente (pcatalina)
 *
 */

int mkpath(const char *s, mode_t mode){
  char *q, *r = NULL, *path = NULL, *up = NULL;
  int rv;

  rv = -1;

  if(strcmp(s, ".") == 0 || strcmp(s, "/") == 0) return(0);

  if((path = strdup(s)) == NULL) return(-1);

  if((q = strdup(s)) == NULL) return(-1);

  if((r = dirname(q)) == NULL) goto out;

  if((up = strdup(r)) == NULL) return(-1);

  if((mkpath(up, mode) == -1) && (errno != EEXIST)) goto out;

  if((mkdir(path, mode) == -1) && (errno != EEXIST)) rv = -1;
    else rv = 0;

out:
  if(up != NULL) free(up);
  free(q);
  free(path);
  return(rv);

}
212