"cd directory" - BruteForce Programm



  • Hallo.

    ich habe spasseshalber ein Programm geschrieben, das per BruteForce alle möglichen Unix-Directory Namen mit "cd <xyz>" ausprobiert und bei Erfolg diese in eine File schreibt. (Für denn Fall, dass man keine Lese- aber Ausführungsrechte hat)

    Doch ein Problem habe ich noch:

    - Bisher kann ich nur maximal Ordner der Länge 3 scannen. Natürlich könnte ich auch mehr Buchstaben scannen, nur müsste ich dann copy/paste machen und weitere for-SChleifen an den "Hauptalgorithmus" dranhängen, was sehr unschön und unötig ist (zum Testen einfach: "./scanofold 3 inputfile" eingeben, aber vorher Ordner mit 3 Buchstaben erstellen9,

    - außerdem bin ich natürlich daran interessiert, das Programm zu beschleunigen....

    Wenn ihr Lösungen für diese Probleme habt....Wenn euch außerdem Verbesserungen jeglicher Art einfallen...immer her damit 🙂 .Ich bin noch Anfänger...

    /*
     * 
     * name  :      scanofold
     * author:      sharp
     * version:     0.2
     * description: scanofold try to open all possible
     *              directories and save valid ones in
     *              in a file you specified.
     * use:         ./scanofold <maximum digits> <input file>
     * 
     * */
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <fcntl.h>
    
    #define BGN_CMD 3     /* ["cd x" | x = BGN_CMD] (begin command)  */
    #define NMB_CHR 64    /* number of characters                    */
    #define NLL_BYT 1     /* Null-Byte at the end of string          */
    #define DEV_NUL 12    /* 2>/dev/null | send stderr to Null       */
    
    void tryOpen (char ordner[], int last);
    
    /*All possible characters of a unix file*/
    
    int ascii[NMB_CHR] = {'.','0','1','2','3','4','5','6','7','8','9','A',\
                          'B','C','D','E','F','G','H','I','J','K','L','M',\
                          'N','O','P','Q','R','S','T','U','V','W','X','Y',\
                          'Z','_','a','b','c','d','e','f','g','h','i','j',\
                          'k','l','m','n','o','p','q','r','s','t','u','v',\
                          'w','x','y','z'}; 
    
    int maxDigits = 0;    /* maximum number of digits of the unix-folder */
    FILE *inputFile;      /* file, where found unix-folder where print   */
    
    int main(int argc, char **argv)
    { 
    	if((maxDigits = atoi(argv[1])) < 1) 
        {                                   
        	fprintf(stderr,"The first parameter has to be a number and" \
        	                " greater than 0.\n");
        	exit(1);
    	}
    	if(argc == 2)                  /* If user doesn't specify a File */
        {
       		if( (inputFile = fopen("foundDirectories", "w")) == NULL)
    		{	
    			fprintf(stderr, "Could not create file.\n");
    			exit(1);
    		}
        }
        else if( (inputFile = fopen(argv[2], "w")) == NULL)/* If he does */
    	{
    		fprintf(stderr, "Could not open file %s.\n", argv[2]);
    		exit(1);
    	}
    
        char ordner[BGN_CMD + maxDigits + DEV_NUL + NLL_BYT]; 
      /*char ordner["cd "   + filename  + DEV_NUL + NLL_BYT];*/
    
        ordner[0] = 'c';         /* with "cd x" you change the directory */
        ordner[1] = 'd';         /* where x = filename                   */
        ordner[2] = ' ';
    
    	tryOpen(ordner, BGN_CMD);                           /* Schritt 1 */ 
    	if(maxDigits < 2)
    	{
    		fclose(inputFile);
    		return 0;
    	}    
    
    	int chr = 0;                                        /* Schritt 2 */
    	for(chr = 0; chr < NMB_CHR; chr++)
    	{
    		ordner[BGN_CMD] = ascii[chr];
    		tryOpen(ordner, BGN_CMD + 1);
    	}                             
        if(maxDigits < 3)
    	{
    		fclose(inputFile);
    		return 0;
    	}  
    
    	int aktChar;                                        /* Schritt 3 */
    	int aktDigit = BGN_CMD;
    	int setNLL_BYT = BGN_CMD + NLL_BYT;                   /* = 3 + 1 */
    
    	for(aktChar = 0; aktChar < NMB_CHR ; aktChar++)
    	{
    		ordner[aktDigit] = ascii[aktChar];
    		int aktChar2;
    		for(aktChar2 = 0; aktChar2 < NMB_CHR; aktChar2++)
    		{
    			ordner[aktDigit + 1] = ascii[aktChar2];
    			tryOpen(ordner, 5);
    		}
    	}
    	if(maxDigits < 4)
    	{
    		fclose(inputFile);
    		return 0;
    	} 
    }
    
    /*tryOpen tries to open ordner ++ x | x = all characters */
    
    void tryOpen(char ordner[], int last) 
    {               /* with "2>/dev/null" you redirect stderr to Nirwana */
    	char devNull[DEV_NUL + NLL_BYT] = {' ','2','>','/','d','e','v','/'\
    	                                            ,'n','u','l','l','\0'};
    	int dvNl = last + 1;                     /* after last character */
    	int i = 0;
    	for(;i < (sizeof devNull);)            /* save devNull in ordner */
    	{
    		ordner[dvNl++] = devNull[i++];
    	}                                             
    	int vp = 0;
    	int found = 7;                              /* 7 is just a dummy */
    	for(vp = 0; vp < NMB_CHR;)          /* now you fill ordner[last] */
    	{
    		ordner[last] = ascii[vp++];
    		if(found = system(ordner) == 0)       /*tries to open folder */
    		{
    			int j = 0;
    			int q = BGN_CMD;
    			char folder[(last - BGN_CMD) + NLL_BYT];
    			for(;q <= last ;j++,q++)  /* extract just the foldername */
    			{
    				folder[j] = ordner[j + BGN_CMD];
    			}
    			folder[j] = '\0';
    			fprintf(inputFile,"%s\n",folder);/* print f.name to file */
    		}
    	}
    }
    

Anmelden zum Antworten