Commit 7d03f95f authored by Jorand Jean-baptiste's avatar Jorand Jean-baptiste
Browse files

Add security rules

parent 8759496f
Loading
Loading
Loading
Loading
+30
Changes for AdaptationParallel/include/statsgen.h: 30 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -52,6 +52,16 @@ struct minMax {
};


struct SecurityRules {
	double nbSecurePassword;
	int minLength;
	int minSpecial;
	int minDigit;
	int minLower;
	int minUpper;
};


/**
 * @brief All needed variables to give to each thread
 */
@@ -74,6 +84,8 @@ struct thread_data {
	std::wregex current_regex;
	bool use_regex = false;
	bool withcount = false;

	SecurityRules sr;
};


@@ -140,6 +152,12 @@ public:
	void setNbThread(int);


	/**
	 * @brief Defining all security rules
	 */
	void setSecurityRules();


	/**
	 * @brief Calculate all statistics for a database which the
	 * name is given by argument
@@ -182,8 +200,20 @@ private:
	double total_filter = 0;		// number of analysed passwords after a filter

	minMax minMaxValue;				// save all genral data from passwords 


	// Security policy

	double nbSecurePassword = 0;	// all passwords that respect the security rules
	int minLength = 8;
	int minSpecial = 0;
	int minDigit = 1;
	int minLower = 1;
	int minUpper = 1;
};



/**
 * @brief analyse a letter in a password
 * @param letter : current letter
+5 −1
Changes for AdaptationParallel/src/main.cpp: 5 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -86,6 +86,11 @@ int main(int argc,char* argv[]) {
				i++;
			}

			else if (string(argv[i]) == "--security" || string(argv[i]) == "-s") {
				test.setSecurityRules();
				i++;
			}

			else {
				filename = argv[i];
			}
@@ -101,7 +106,6 @@ int main(int argc,char* argv[]) {




	if (test.generate_stats(filename)) {
		test.print_stats();
	}
+56 −8
Changes for AdaptationParallel/src/statsgen.cpp: 56 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -38,6 +38,8 @@ void Statsgen::showHelp() {
    wcout << "\t--limitsimplemasks, -lsm [value]\t:\tLimit the size of the simple masks at [value], if size>[value]: othermasks" << endl;
    wcout << "\t--parallel, -p [value]\t\t\t:\tNumber of usable threads" << endl;
    
    wcout << "\n\nSecurity rules : " << endl;
    wcout << "\t--security, -s\t:\tDefine the security rules" << endl;
    wcout << "\n" << endl;
}

@@ -88,6 +90,23 @@ void Statsgen::setNbThread(int nb) {
}


void Statsgen::setSecurityRules() {
    wcout << "\nMinimal length of a password:" << endl;
    cin >> minLength;

    wcout << "\nMinimum of special characters in a password:" << endl;
    cin >> minSpecial;

    wcout << "\nMinimum of digits in a password:" << endl;
    cin >> minDigit;

    wcout << "\nMinimum of lower characters in a password:" << endl;
    cin >> minLower;

    wcout << "\nMinimum of upper characters in a password:" << endl;
    cin >> minUpper;
}


int Statsgen::generate_stats(const string & filename) {
    int nbline = nbline_file(filename);
@@ -113,6 +132,13 @@ int Statsgen::generate_stats(const string & filename) {
        td[i].lineBegin = i*(nbline/nbThread) + 1;
        td[i].lineEnd = (i+1)*nbline/nbThread;

        td[i].sr.nbSecurePassword = 0;
        td[i].sr.minLength = minLength;
        td[i].sr.minSpecial = minSpecial;
        td[i].sr.minDigit = minDigit;
        td[i].sr.minLower = minLower;
        td[i].sr.minUpper = minUpper;

        wcout << "Thread " << td[i].thread_id << " analyse : " << td[i].lineBegin << " --> " << td[i].lineEnd << endl;
        rc = pthread_create(&threads[i], NULL, generate_stats_thread, (void *)&td[i] );
      
@@ -149,6 +175,8 @@ int Statsgen::generate_stats(const string & filename) {
        policyMax.upper = td[i].minMaxValue.maxupper;
        policyMax.special = td[i].minMaxValue.maxspecial;

        nbSecurePassword += td[i].sr.nbSecurePassword;

        updateMinMax(minMaxValue, policyMin);
        updateMinMax(minMaxValue, policyMax);

@@ -193,6 +221,16 @@ void Statsgen::print_stats() {
        << perc << " %)" << endl;


    wcout << "\nSecurity rules : " << endl;
    wcout << "\tMinimal length of a password: " << minLength << endl;
    wcout << "\tMinimum of special characters in a password: " << minSpecial << endl;
    wcout << "\tMinimum of digits in a password: " << minDigit << endl;
    wcout << "\tMinimum of lower characters in a password: " << minLower << endl;
    wcout << "\tMinimum of upper characters in a password: " << minUpper << endl;

    float perce = (float) 100 * (nbSecurePassword / total_counter);
    wcout << "\n\t\t-->" << nbSecurePassword << " passwords\t(" << perce << " %) repect the security rules\n" << endl;


    wcout << "\nmin - max\n" << endl;
    wcout << setw(43) << right << "digit:  " 
@@ -210,7 +248,7 @@ void Statsgen::print_stats() {
    showMap(stats_length, top, total_counter, hiderare, count);

    wcout << "\nStatistics relative to charsets: \n" << endl;
    showMap(stats_charactersets, top, total_counter, hiderare, count);
    showMap(stats_charactersets, -1, total_counter, hiderare, count);


    wcout << "\nStatistics relative to simplemasks: \n" << endl;
@@ -229,9 +267,14 @@ void Statsgen::print_stats() {
        wcout << endl;
        readResult(stats_advancedmasks[L"othermasks"], L"othermasks", count, total_counter, hiderare);
    }

}






void analyse_letter(const char & letter, char & last_simplemask, wstring & simplemask_string, wstring & advancedmask_string, Policy & policy, int & sizeAdvancedMask, int & sizeSimpleMask) {
    sizeAdvancedMask++;

@@ -274,6 +317,7 @@ void analyse_letter(const char & letter, char & last_simplemask, wstring & simpl
    }
}


void analyse_charset(wstring & charset, const Policy & policy) {
    if (policy.digit && !policy.lower && !policy.upper && !policy.special) {
        charset = L"numeric";
@@ -325,7 +369,7 @@ void analyse_charset(wstring & charset, const Policy & policy) {
}


void analyze_password(const wstring & password, Container & c) {
void analyze_password(const wstring & password, Container & c, SecurityRules & sr) {
    c.pass_length = password.size();

    char last_simplemask = 'a';
@@ -339,6 +383,14 @@ void analyze_password(const wstring & password, Container & c) {
    }

    analyse_charset(c.characterset, c.pol);

    if ( (c.pass_length >= sr.minLength) && 
         (c.pol.digit >= sr.minDigit) && 
         (c.pol.lower >= sr.minLower) && 
         (c.pol.upper >= sr.minUpper) && 
         (c.pol.special >= sr.minSpecial) ) {
        sr.nbSecurePassword++;
    }
}


@@ -375,9 +427,6 @@ void updateMinMax(minMax & minMaxValue, const Policy & pol) {






void * generate_stats_thread(void * threadarg) {
    struct thread_data *my_data;
    my_data = (struct thread_data *) threadarg;
@@ -398,7 +447,6 @@ void * generate_stats_thread(void * threadarg) {


        if (line.size() == 0) {
            //wcout << "Error empty password, line " << my_data->total_counter << endl;
            continue;
        }

@@ -422,7 +470,7 @@ void * generate_stats_thread(void * threadarg) {

            if ( !my_data->use_regex || (my_data->use_regex && regex_match(password,my_data->current_regex)) ) {
                my_data->total_filter += nbPasswords;
                analyze_password(password, c);
                analyze_password(password, c, my_data->sr);

                my_data->length[ c.pass_length ] += nbPasswords;
                my_data->charactersets[ c.characterset ] += nbPasswords;
@@ -433,7 +481,7 @@ void * generate_stats_thread(void * threadarg) {
        else {
            my_data->total_counter++;
            if ( !my_data->use_regex || (my_data->use_regex && regex_match(line,my_data->current_regex)) ) {
                analyze_password(line, c);
                analyze_password(line, c, my_data->sr);
                
                my_data->total_filter++;