diff --git a/include/rp.h b/include/rp.h index c24f2fd15..f4bb33b3d 100644 --- a/include/rp.h +++ b/include/rp.h @@ -26,6 +26,8 @@ int conv_itoc (const u8 c); int generate_random_rule (char rule_buf[RP_RULE_SIZE], const u32 rp_gen_func_min, const u32 rp_gen_func_max); +bool is_hex_notation (char *rule_buf, u32 rule_len, u32 rule_pos); + int cpu_rule_to_kernel_rule (char *rule_buf, u32 rule_len, kernel_rule_t *rule); int kernel_rule_to_cpu_rule (char *rule_buf, kernel_rule_t *rule); diff --git a/src/rp.c b/src/rp.c index cbb9e9add..2af06a644 100644 --- a/src/rp.c +++ b/src/rp.c @@ -6,6 +6,7 @@ #include "common.h" #include "types.h" #include "memory.h" +#include "convert.h" #include "event.h" #include "shared.h" #include "filehandling.h" @@ -217,8 +218,8 @@ int generate_random_rule (char rule_buf[RP_RULE_SIZE], const u32 rp_gen_func_min #define INCR_POS if (++rule_pos == rule_len) return (-1) #define SET_NAME(rule,val) (rule)->cmds[rule_cnt] = ((val) & 0xff) << 0 -#define SET_P0(rule,val) INCR_POS; (rule)->cmds[rule_cnt] |= ((val) & 0xff) << 8 -#define SET_P1(rule,val) INCR_POS; (rule)->cmds[rule_cnt] |= ((val) & 0xff) << 16 +#define SET_P0(rule,val) INCR_POS; if (is_hex_notation (rule_buf, rule_len, rule_pos) == true) { (rule)->cmds[rule_cnt] |= (hex_convert (rule_buf[rule_pos + 3] & 0xff) << 8) | (hex_convert (rule_buf[rule_pos + 2] & 0xff) << 12); rule_pos += 4; } else { (rule)->cmds[rule_cnt] |= ((val) & 0xff) << 8; } +#define SET_P1(rule,val) INCR_POS; if (is_hex_notation (rule_buf, rule_len, rule_pos) == true) { (rule)->cmds[rule_cnt] |= (hex_convert (rule_buf[rule_pos + 3] & 0xff) << 16) | (hex_convert (rule_buf[rule_pos + 2] & 0xff) << 20); rule_pos += 4; } else { (rule)->cmds[rule_cnt] |= ((val) & 0xff) << 16; } #define GET_NAME(rule) rule_cmd = (((rule)->cmds[rule_cnt] >> 0) & 0xff) #define GET_P0(rule) INCR_POS; rule_buf[rule_pos] = (((rule)->cmds[rule_cnt] >> 8) & 0xff) #define GET_P1(rule) INCR_POS; rule_buf[rule_pos] = (((rule)->cmds[rule_cnt] >> 16) & 0xff) @@ -228,6 +229,19 @@ int generate_random_rule (char rule_buf[RP_RULE_SIZE], const u32 rp_gen_func_min #define GET_P0_CONV(rule) INCR_POS; rule_buf[rule_pos] = (char) conv_itoc (((rule)->cmds[rule_cnt] >> 8) & 0xff) #define GET_P1_CONV(rule) INCR_POS; rule_buf[rule_pos] = (char) conv_itoc (((rule)->cmds[rule_cnt] >> 16) & 0xff) +bool is_hex_notation (char *rule_buf, u32 rule_len, u32 rule_pos) +{ + if ((rule_pos + 4) > rule_len) return false; + + if (rule_buf[rule_pos + 0] != '\\') return false; + if (rule_buf[rule_pos + 1] != 'x') return false; + + if (is_valid_hex_char (rule_buf[rule_pos + 2]) == false) return false; + if (is_valid_hex_char (rule_buf[rule_pos + 3]) == false) return false; + + return true; +} + int cpu_rule_to_kernel_rule (char *rule_buf, u32 rule_len, kernel_rule_t *rule) { u32 rule_pos; diff --git a/src/rp_cpu.c b/src/rp_cpu.c index fe2b58b1c..04db4f5d6 100644 --- a/src/rp_cpu.c +++ b/src/rp_cpu.c @@ -5,6 +5,8 @@ #include "common.h" #include "types.h" +#include "convert.h" +#include "memory.h" #include "rp.h" #include "rp_cpu.h" @@ -490,14 +492,30 @@ int _old_apply_rule (const char *rule, int rule_len, char in[RP_PASSWORD_SIZE], memcpy (out, in, out_len); + char *rule_new = hcstrdup (rule); + int rule_pos; + for (rule_pos = 0; rule_pos < rule_len; rule_pos++) + { + if (is_hex_notation (rule_new, rule_len, rule_pos)) + { + const u8 c = hex_to_u8 (rule_new + rule_pos + 2); + + rule_new[rule_pos + 0] = c; + rule_new[rule_pos + 1] = ' '; + rule_new[rule_pos + 2] = ' '; + rule_new[rule_pos + 3] = ' '; + + } + } + for (rule_pos = 0; rule_pos < rule_len; rule_pos++) { int upos, upos2; int ulen; - switch (rule[rule_pos]) + switch (rule_new[rule_pos]) { case ' ': break; @@ -561,12 +579,12 @@ int _old_apply_rule (const char *rule, int rule_len, char in[RP_PASSWORD_SIZE], case RULE_OP_MANGLE_APPEND: NEXT_RULEPOS (rule_pos); - out_len = mangle_append (out, out_len, rule[rule_pos]); + out_len = mangle_append (out, out_len, rule_new[rule_pos]); break; case RULE_OP_MANGLE_PREPEND: NEXT_RULEPOS (rule_pos); - out_len = mangle_prepend (out, out_len, rule[rule_pos]); + out_len = mangle_prepend (out, out_len, rule_new[rule_pos]); break; case RULE_OP_MANGLE_DELETE_FIRST: @@ -603,14 +621,14 @@ int _old_apply_rule (const char *rule, int rule_len, char in[RP_PASSWORD_SIZE], NEXT_RULEPOS (rule_pos); NEXT_RPTOI (rule, rule_pos, upos); NEXT_RULEPOS (rule_pos); - out_len = mangle_insert (out, out_len, upos, rule[rule_pos]); + out_len = mangle_insert (out, out_len, upos, rule_new[rule_pos]); break; case RULE_OP_MANGLE_OVERSTRIKE: NEXT_RULEPOS (rule_pos); NEXT_RPTOI (rule, rule_pos, upos); NEXT_RULEPOS (rule_pos); - out_len = mangle_overstrike (out, out_len, upos, rule[rule_pos]); + out_len = mangle_overstrike (out, out_len, upos, rule_new[rule_pos]); break; case RULE_OP_MANGLE_TRUNCATE_AT: @@ -622,12 +640,12 @@ int _old_apply_rule (const char *rule, int rule_len, char in[RP_PASSWORD_SIZE], case RULE_OP_MANGLE_REPLACE: NEXT_RULEPOS (rule_pos); NEXT_RULEPOS (rule_pos); - out_len = mangle_replace (out, out_len, rule[rule_pos - 1], rule[rule_pos]); + out_len = mangle_replace (out, out_len, rule_new[rule_pos - 1], rule_new[rule_pos]); break; case RULE_OP_MANGLE_PURGECHAR: NEXT_RULEPOS (rule_pos); - out_len = mangle_purgechar (out, out_len, rule[rule_pos]); + out_len = mangle_purgechar (out, out_len, rule_new[rule_pos]); break; case RULE_OP_MANGLE_TOGGLECASE_REC: @@ -716,7 +734,7 @@ int _old_apply_rule (const char *rule, int rule_len, char in[RP_PASSWORD_SIZE], case RULE_OP_MANGLE_TITLE_SEP: NEXT_RULEPOS (rule_pos); - out_len = mangle_title_sep (out, out_len, rule[rule_pos]); + out_len = mangle_title_sep (out, out_len, rule_new[rule_pos]); break; case RULE_OP_MANGLE_TITLE: @@ -774,12 +792,12 @@ int _old_apply_rule (const char *rule, int rule_len, char in[RP_PASSWORD_SIZE], case RULE_OP_REJECT_CONTAIN: NEXT_RULEPOS (rule_pos); - if (strchr (out, rule[rule_pos]) != NULL) return (RULE_RC_REJECT_ERROR); + if (strchr (out, rule_new[rule_pos]) != NULL) return (RULE_RC_REJECT_ERROR); break; case RULE_OP_REJECT_NOT_CONTAIN: NEXT_RULEPOS (rule_pos); - char *match = strchr (out, rule[rule_pos]); + char *match = strchr (out, rule_new[rule_pos]); if (match != NULL) { pos_mem = (int)(match - out); @@ -792,12 +810,12 @@ int _old_apply_rule (const char *rule, int rule_len, char in[RP_PASSWORD_SIZE], case RULE_OP_REJECT_EQUAL_FIRST: NEXT_RULEPOS (rule_pos); - if (out[0] != rule[rule_pos]) return (RULE_RC_REJECT_ERROR); + if (out[0] != rule_new[rule_pos]) return (RULE_RC_REJECT_ERROR); break; case RULE_OP_REJECT_EQUAL_LAST: NEXT_RULEPOS (rule_pos); - if (out[out_len - 1] != rule[rule_pos]) return (RULE_RC_REJECT_ERROR); + if (out[out_len - 1] != rule_new[rule_pos]) return (RULE_RC_REJECT_ERROR); break; case RULE_OP_REJECT_EQUAL_AT: @@ -805,7 +823,7 @@ int _old_apply_rule (const char *rule, int rule_len, char in[RP_PASSWORD_SIZE], NEXT_RPTOI (rule, rule_pos, upos); if ((upos + 1) > out_len) return (RULE_RC_REJECT_ERROR); NEXT_RULEPOS (rule_pos); - if (out[upos] != rule[rule_pos]) return (RULE_RC_REJECT_ERROR); + if (out[upos] != rule_new[rule_pos]) return (RULE_RC_REJECT_ERROR); break; case RULE_OP_REJECT_CONTAINS: @@ -816,7 +834,7 @@ int _old_apply_rule (const char *rule, int rule_len, char in[RP_PASSWORD_SIZE], int c; int cnt; for (c = 0, cnt = 0; c < out_len && cnt < upos; c++) { - if (out[c] == rule[rule_pos]) + if (out[c] == rule_new[rule_pos]) { cnt++; pos_mem = c; @@ -837,6 +855,8 @@ int _old_apply_rule (const char *rule, int rule_len, char in[RP_PASSWORD_SIZE], memset (out + out_len, 0, RP_PASSWORD_SIZE - out_len); + hcfree (rule_new); + return (out_len); }