[GH-ISSUE #2122] Rule on Rule stacking #1417

Closed
opened 2026-10-15 04:16:33 +07:00 by bsakaguchi · 16 comments
Owner

Originally created by @jfoug on GitHub (Apr 17, 2016).
Original GitHub issue: https://github.com/openwall/john/issues/2122

Called 'multi-rule' in hash-cat. This seems pretty easy to add. I would think we would stack like this:

[list.rules.rule1]
$1
$2
$3
[list.rules.rule2]
^a
^b
[list.rules.rule3]
[lc]$-

this would produce: -rules=rule1+rule2+rule3 (note the command line syntax is not set in stone, just something I typed on the fly)

$1
$2
$3
^a
^b
l$-
c$-
$1^a
$1^b
$1l$-
$2c$-
$2^a
$2^b
$2l$-
$2c$-
$3^a
$3^b
$3l$-
$3c$-
$1^al$-
$1^ac$-
$1^bl$-
$1^bc$-
$2^al$-
$2^ac$-
$2^bl$-
$2^bc$-
$3^al$-
$3^ac$-
$3^bl$-
$3^bc$-

But I could see 'other' ideas on how to stack this (like having a$-$1 then c$-$1 etc

Originally created by @jfoug on GitHub (Apr 17, 2016). Original GitHub issue: https://github.com/openwall/john/issues/2122 Called 'multi-rule' in hash-cat. This seems pretty easy to add. I would think we would stack like this: ``` [list.rules.rule1] $1 $2 $3 [list.rules.rule2] ^a ^b [list.rules.rule3] [lc]$- ``` this would produce: -rules=rule1+rule2+rule3 (note the command line syntax is not set in stone, just something I typed on the fly) ``` $1 $2 $3 ^a ^b l$- c$- $1^a $1^b $1l$- $2c$- $2^a $2^b $2l$- $2c$- $3^a $3^b $3l$- $3c$- $1^al$- $1^ac$- $1^bl$- $1^bc$- $2^al$- $2^ac$- $2^bl$- $2^bc$- $3^al$- $3^ac$- $3^bl$- $3^bc$- ``` But I could see 'other' ideas on how to stack this (like having `a$-$1 then c$-$1 etc`
bsakaguchi 2026-10-15 04:16:33 +07:00
Author
Owner

@jfoug commented on GitHub (Apr 18, 2016):

Note, it would be a smart thing to do some analysis of rules also for this (and even for just standard rules). I have been seeing some crap like this, when analyzing some of the hashcat rule sets (first TONS of dupes, but then some no-op things). The 'easiest' way was if there was some cannonzation function.

$1$2]]     (no op)

and this block of 'rich' duplicates from hashcat's dive.rule (simply grepped for ss5 but there are MANY more dupes than this all over the place)
ss5
ss5 se3
ss5 si1 se3
ss5 se3 si1
ss5 se3 sa@
ss5 sa@ se3
ss5 se3 so0
ss5 so0 se3
ss5 sa@
ss5 si1
ss5 sa@ se3 si1
ss5 si1 sa@ se3
ss5 se3 sa@ si1
ss5 se3 si1 sa@
ss5 si1 se3 sa@
ss5 sa@ si1 se3
ss5 sa@ si1
ss5 si1 sa@
ss5 so0 si1
ss5 si1 so0
ss5 si1 so0 se3
ss5 se3 si1 so0
ss5 so0 se3 si1
ss5 se3 so0 si1
ss5 si1 se3 so0
ss5 so0 si1 se3
ss5 so0
ss5 sa@ so0
ss5 so0 sa@
ss5 se3 so0 sa@
ss5 sa@ se3 so0
ss5 sa@ so0 se3
ss5 so0 se3 sa@
ss5 se3 sa@ so0
ss5 so0 sa@ se3
ss5 sa@ se3 si1 so0
ss5 si1 so0 sa@
ss5 so0 sa@ si1
ss5 sa@ so0 si1
ss5 so0 si1 sa@
ss5 si1 sa@ so0
ss5 sa@ si1 so0
ss5 sa@ so0 si1 se3
ss5 se3 so0 sa@ si1
ss5 so0 sa@ se3 si1
ss5 se3 si1 sa@ so0
ss5 so0 se3 si1 sa@
ss5 si1 so0 sa@ se3
ss5 sa@ so0 se3 si1
ss5 sa@ si1 so0 se3
ss5 si1 sa@ so0 se3
ss5 sa@ se3 so0 si1
ss5 si1 se3 sa@ so0
ss5 si1 so0 se3 sa@
ss5 se3 so0 si1 sa@
ss5 se3 sa@ si1 so0
ss5 so0 sa@ si1 se3
ss5 se3 si1 so0 sa@
ss5 so0 si1 se3 sa@
ss5 si1 se3 so0 sa@
ss5 so0 si1 sa@ se3
ss5 si1 sa@ se3 so0
ss5 sa@ si1 se3 so0
ss5 se3 sa@ so0 si1
ss5 so0 se3 sa@ si1
*41ss5
c ss5
ss5 sa4
c se3 ss5
c ss5 se3
ss5 so0 sa4
ss5 sa4 so0
ss5 u
si1 ss5
ss5se3
Z2 ss5
sSLss5i4s
*41 ss5
ss5 i4s
sa4 ss5
so0 ss5
se3 ss5
sa@ ss5

<!-- gh-comment-id:211137505 --> @jfoug commented on GitHub (Apr 18, 2016): Note, it would be a smart thing to do some analysis of rules also for this (and even for just standard rules). I have been seeing some crap like this, when analyzing some of the hashcat rule sets (first TONS of dupes, but then some no-op things). The 'easiest' way was if there was some cannonzation function. ``` $1$2]] (no op) and this block of 'rich' duplicates from hashcat's dive.rule (simply grepped for ss5 but there are MANY more dupes than this all over the place) ss5 ss5 se3 ss5 si1 se3 ss5 se3 si1 ss5 se3 sa@ ss5 sa@ se3 ss5 se3 so0 ss5 so0 se3 ss5 sa@ ss5 si1 ss5 sa@ se3 si1 ss5 si1 sa@ se3 ss5 se3 sa@ si1 ss5 se3 si1 sa@ ss5 si1 se3 sa@ ss5 sa@ si1 se3 ss5 sa@ si1 ss5 si1 sa@ ss5 so0 si1 ss5 si1 so0 ss5 si1 so0 se3 ss5 se3 si1 so0 ss5 so0 se3 si1 ss5 se3 so0 si1 ss5 si1 se3 so0 ss5 so0 si1 se3 ss5 so0 ss5 sa@ so0 ss5 so0 sa@ ss5 se3 so0 sa@ ss5 sa@ se3 so0 ss5 sa@ so0 se3 ss5 so0 se3 sa@ ss5 se3 sa@ so0 ss5 so0 sa@ se3 ss5 sa@ se3 si1 so0 ss5 si1 so0 sa@ ss5 so0 sa@ si1 ss5 sa@ so0 si1 ss5 so0 si1 sa@ ss5 si1 sa@ so0 ss5 sa@ si1 so0 ss5 sa@ so0 si1 se3 ss5 se3 so0 sa@ si1 ss5 so0 sa@ se3 si1 ss5 se3 si1 sa@ so0 ss5 so0 se3 si1 sa@ ss5 si1 so0 sa@ se3 ss5 sa@ so0 se3 si1 ss5 sa@ si1 so0 se3 ss5 si1 sa@ so0 se3 ss5 sa@ se3 so0 si1 ss5 si1 se3 sa@ so0 ss5 si1 so0 se3 sa@ ss5 se3 so0 si1 sa@ ss5 se3 sa@ si1 so0 ss5 so0 sa@ si1 se3 ss5 se3 si1 so0 sa@ ss5 so0 si1 se3 sa@ ss5 si1 se3 so0 sa@ ss5 so0 si1 sa@ se3 ss5 si1 sa@ se3 so0 ss5 sa@ si1 se3 so0 ss5 se3 sa@ so0 si1 ss5 so0 se3 sa@ si1 *41ss5 c ss5 ss5 sa4 c se3 ss5 c ss5 se3 ss5 so0 sa4 ss5 sa4 so0 ss5 u si1 ss5 ss5se3 Z2 ss5 sSLss5i4s *41 ss5 ss5 i4s sa4 ss5 so0 ss5 se3 ss5 sa@ ss5 ```
Author
Owner

@magnumripper commented on GitHub (Apr 18, 2016):

See also #745

<!-- gh-comment-id:211486696 --> @magnumripper commented on GitHub (Apr 18, 2016): See also #745
Author
Owner

@jfoug commented on GitHub (Apr 18, 2016):

You want me to close this one, and move these messages to the prior?

<!-- gh-comment-id:211500830 --> @jfoug commented on GitHub (Apr 18, 2016): You want me to close this one, and move these messages to the prior?
Author
Owner

@jfoug commented on GitHub (Apr 18, 2016):

I think one of the hardest things, is to remove duplicates when we start stacking rules, I have been working on some new code (for rules work), and a couple things I have been working on may help in this.

One is rules cannonization (hard). But this actually can improve runtime (by reducing rules AND by allowing some rules to optimize themselves in the code), AND this also can find dupes. The nice thing about cannonization, is that you can also frequently optimize. So $1$2$3$4 can become 1 rule Az"1234" (just 1 rule), and $1^4$2^3$3^2$4^1 or $1$2$3$4^4^3^2^1 or ^4^3^2^1$1$2$3$4 can all become A0"1234"Az"1234" which shows they are the same, and reduces 8 to 2

One is backfinding a rule (or many rules) targeting single mostly, based upon found found word, and input word(s). This is also a good way to find dupes, when the finding code is checked against all rules, and not just the first that cracks the word. It is also really good at finding THE good rules out of an ocean of rules that work.

One is dupe finding in a targetted manner.

There is only one program I am actually writing (it will be a symlink to john, likely called rulez). But I am also doing some other prototyping in some simple code and perl scripts.

<!-- gh-comment-id:211506101 --> @jfoug commented on GitHub (Apr 18, 2016): I think one of the hardest things, is to remove duplicates when we start stacking rules, I have been working on some new code (for rules work), and a couple things I have been working on may help in this. One is rules cannonization (hard). But this actually can improve runtime (by reducing rules AND by allowing some rules to optimize themselves in the code), AND this also can find dupes. The nice thing about cannonization, is that you can also frequently optimize. So $1$2$3$4 can become 1 rule Az"1234" (just 1 rule), and $1^4$2^3$3^2$4^1 or $1$2$3$4^4^3^2^1 or ^4^3^2^1$1$2$3$4 can all become A0"1234"Az"1234" which shows they are the same, and reduces 8 to 2 One is backfinding a rule (or many rules) targeting single mostly, based upon found found word, and input word(s). This is also a good way to find dupes, when the finding code is checked against all rules, and not just the first that cracks the word. It is also really good at finding THE good rules out of an ocean of rules that work. One is dupe finding in a targetted manner. There is only one program I am actually writing (it will be a symlink to john, likely called rulez). But I am also doing some other prototyping in some simple code and perl scripts.
Author
Owner

@jfoug commented on GitHub (Apr 18, 2016):

How about this for 'syntax' ?

[r1]
$a
$b

[r1]
l
c

[r3]
\[
\]

-rule=r1+r2+r3

$al\[
$ac\[
$al\]
$ac\]
$bl\[
$bc\[
$bl\]
$bc\]

-rule=r1&r2&r3

$a
$b
l
c
\[
\]

-rule=r1&+r2&+r3

$a
$b
l
c
\[
\]
$al\[
$ac\[
$al\]
$ac\]
$bl\[
$bc\[
$bl\]
$bc\]

-rule=r1^r2 (not showing all 3) NOTE, in this case it is not useful, but for many rule sets it 'could' be useful mixing order.

$al
$ac
$bl
$bc
c$a
c$b
l$a
l$b

There could probably be other operators. - would come to mind, and be VERY useful.

Minus would be -rules=r2-r1 This would run using r2 rules, but removing all r1 rules found from r2 i.e. -rules=r1 was already done.

Ok, I know this is a bit,but hell, if we are going to do this, then why not do it right, and allow it to be more useful (and more optimal) to the end user.

<!-- gh-comment-id:211511736 --> @jfoug commented on GitHub (Apr 18, 2016): How about this for 'syntax' ? ``` [r1] $a $b [r1] l c [r3] \[ \] ``` -rule=r1+r2+r3 ``` $al\[ $ac\[ $al\] $ac\] $bl\[ $bc\[ $bl\] $bc\] ``` -rule=r1&r2&r3 ``` $a $b l c \[ \] ``` -rule=r1&+r2&+r3 ``` $a $b l c \[ \] $al\[ $ac\[ $al\] $ac\] $bl\[ $bc\[ $bl\] $bc\] ``` -rule=r1^r2 (not showing all 3) NOTE, in this case it is not useful, but for many rule sets it 'could' be useful mixing order. ``` $al $ac $bl $bc c$a c$b l$a l$b ``` There could probably be other operators. - would come to mind, and be VERY useful. Minus would be -rules=r2-r1 This would run using r2 rules, but removing all r1 rules found from r2 i.e. -rules=r1 was already done. Ok, I know this is a bit,but hell, if we are going to do this, then why not do it right, and allow it to be more useful (and more optimal) to the end user.
Author
Owner

@jfoug commented on GitHub (Apr 18, 2016):

here is a good example of dupes (I have many, but here is another)

D6 i74 i9i o8k oAd
D6 i74 i9i oAd o8k
D6 i74 o8k i9i oAd

In this case, we can normalize by:

  • sort o based upon 1st parameter
  • o can shuffle, but controlled by an i or D (etc) with lower 1st parameter (i.e. can not move to the right of that insert / delete)
  • an i can also move, but a lower one must come before a higher one.
  • we can define that all i should come before all o (with legal moves)

So canonizing the above rule(s) would be

D6 i74 i9i o8k oAd   (well canonized would have spaces removed also)

The hard part of canonizing, is to find the 'rules', and then parsing and handing adjustment of the data. It gets more tricky when more and more rules and their interplay with each other are put together.

The other way I am dup looking, is by using the rules themselves, looking for identical end results, then grouping rules that produce identical, and continuing to feed them different data (rotating letters, varying lengths, etc), to weed out the ones that are not really duplicate. This probably is not a valid way to do things at run time. This would be offline optimization work on existing rule sets.

Some examples of 'dupe' rules, but which only run testing may show are things like:

>6o6b
o6b
D6i6b
i6bD7

This could be 'argued' (inaccurately), that these are not dupes, since the 2nd one will output the original candidate for words less than 6 bytes. But I would disagree, and state that they are dupes. Now, the 3rd/4th ones, are ONLY a dupe with the first if word is > 6 bytes. Other than that, in JtR mode, it will add a 'b' char to the input word. But they are 100% duplicate to each other, and would be very hard to tell apart from each other.

<!-- gh-comment-id:211527976 --> @jfoug commented on GitHub (Apr 18, 2016): here is a good example of dupes (I have many, but here is another) ``` D6 i74 i9i o8k oAd D6 i74 i9i oAd o8k D6 i74 o8k i9i oAd ``` In this case, we can normalize by: - sort o based upon 1st parameter - o can shuffle, but controlled by an i or D (etc) with lower 1st parameter (i.e. can not move to the right of that insert / delete) - an i can also move, but a lower one must come before a higher one. - we can define that all i should come before all o (with legal moves) So canonizing the above rule(s) would be ``` D6 i74 i9i o8k oAd (well canonized would have spaces removed also) ``` The hard part of canonizing, is to find the 'rules', and then parsing and handing adjustment of the data. It gets more tricky when more and more rules and their interplay with each other are put together. The other way I am dup looking, is by using the rules themselves, looking for identical end results, then grouping rules that produce identical, and continuing to feed them different data (rotating letters, varying lengths, etc), to weed out the ones that are not really duplicate. This probably is not a valid way to do things at run time. This would be offline optimization work on existing rule sets. Some examples of 'dupe' rules, but which only run testing may show are things like: ``` >6o6b o6b D6i6b i6bD7 ``` This could be 'argued' (inaccurately), that these are not dupes, since the 2nd one will output the original candidate for words less than 6 bytes. But I would disagree, and state that they are dupes. Now, the 3rd/4th ones, are ONLY a dupe with the first if word is > 6 bytes. Other than that, in JtR mode, it will add a 'b' char to the input word. But they are 100% duplicate to each other, and would be very hard to tell apart from each other.
Author
Owner

@frank-dittrich commented on GitHub (Apr 18, 2016):

o can shuffle, but controlled by an i or D (etc) with lower 1st parameter (i.e. can not move to the right of that insert / delete)

The etc is important here. There are other commands which have to be considered ({, }, ], d, r, f, ...).
Future releases might add even more of such commands, So, instead of "blacklisting" commands which don't allow shuffling the o commands, you'd need to define a whitelist of commands which allow such shuffling.

Not sure whether this will be worth it.

<!-- gh-comment-id:211532471 --> @frank-dittrich commented on GitHub (Apr 18, 2016): > o can shuffle, but controlled by an i or D (etc) with lower 1st parameter (i.e. can not move to the right of that insert / delete) The _etc_ is important here. There are other commands which have to be considered ({, }, ], d, r, f, ...). Future releases might add even more of such commands, So, instead of "blacklisting" commands which don't allow shuffling the o commands, you'd need to define a whitelist of commands which allow such shuffling. Not sure whether this will be worth it.
Author
Owner

@jfoug commented on GitHub (Apr 18, 2016):

It may not be 'worth' it, but if/when we start stacking rules, having ability to quickly get as close as we can to a canonized rule allows removal of duplicates.

If you stack 3 or 4 rule sets (or even 2 big ones), and allow 2 way many to many stacking there are literally millions of rules that get generated, and a huge percentage of these are likely to produce identical results. If these can not be trimmed down, then the end performance is only a tiny fraction of what it should be.

Now, trying to get things 100%, I do not think that is worth the time involved. Rules are HARD. Without generating a pure interpretive environment, it is really hard to always know that 2 things are identical. But there are many things which CAN be normalized and done some fairly easily, and lots of dupes can get removed.

Things like:

  • always merging adjacent $ and ^ into singular A0"abc"Az"def" (with A0 defined to be before Az)
  • sorting adjacent s rule based upon base letter after removing s 'cycles' (where an input is used more than 1 time, or where an output is later used as an input, this 2nd one).
  • sorting adjacent [ and ] to do all ] first then all [

and finding as many of these 'easy' canonizations, ends up finding MANY dupes, AND also optimizes. This work is actually pretty easy, and then it could simply be expanded as additional 'safe/easy' canonization rules are added. Some of the rule sets appearing are very 'good', but are not great because of a relatively high volume of duplicate rules. When we allow "rule x rule" logic, that problem will be exponentially worse.

<!-- gh-comment-id:211563974 --> @jfoug commented on GitHub (Apr 18, 2016): It may not be 'worth' it, but if/when we start stacking rules, having ability to quickly get as close as we can to a canonized rule allows removal of duplicates. If you stack 3 or 4 rule sets (or even 2 big ones), and allow 2 way many to many stacking there are literally millions of rules that get generated, and a huge percentage of these are likely to produce identical results. If these can not be trimmed down, then the end performance is only a tiny fraction of what it should be. Now, trying to get things 100%, I do not think that is worth the time involved. Rules are HARD. Without generating a pure interpretive environment, it is really hard to always know that 2 things are identical. But there are many things which CAN be normalized and done some fairly easily, and lots of dupes can get removed. Things like: - always merging adjacent $ and ^ into singular A0"abc"Az"def" (with A0 defined to be before Az) - sorting adjacent s rule based upon base letter after removing s 'cycles' (where an input is used more than 1 time, or where an output is later used as an input, this 2nd one). - sorting adjacent [ and ] to do all ] first then all [ and finding as many of these 'easy' canonizations, ends up finding MANY dupes, AND also optimizes. This work is actually pretty easy, and then it could simply be expanded as additional 'safe/easy' canonization rules are added. Some of the rule sets appearing are very 'good', but are not great because of a relatively high volume of duplicate rules. When we allow "rule x rule" logic, that problem will be exponentially worse.
Author
Owner

@frank-dittrich commented on GitHub (Apr 18, 2016):

Things like:

  • always merging adjacent $ and ^ into singular A0"abc"Az"def" (with A0 defined to be before Az)

Unless the character after $ or ^ is ". Then you have to pick another character to enclose that string.

Just removing plain duplicate rules would be a worthwhile first step.
AFAIK, currently, the preprocessor would remove duplicate

$[ -~]

But it will still produce duplicate $[0-9] for this:

$[ -~]
$[0-9]

Or was there any recent change in jumbo I missed?

<!-- gh-comment-id:211581588 --> @frank-dittrich commented on GitHub (Apr 18, 2016): > Things like: > - always merging adjacent $ and ^ into singular A0"abc"Az"def" (with A0 defined to be before Az) Unless the character after $ or ^ is ". Then you have to pick another character to enclose that string. Just removing plain duplicate rules would be a worthwhile first step. AFAIK, currently, the preprocessor would remove duplicate ``` $[ -~] ``` But it will still produce duplicate $[0-9] for this: ``` $[ -~] $[0-9] ``` Or was there any recent change in jumbo I missed?
Author
Owner

@jfoug commented on GitHub (Apr 18, 2016):

Nope, I think rpp will remove these (I think). It is done with rules_remove_dups() at load time.

I see many rule sets which have 123k, end up with 121k when loaded. It is this dup removal. There are dupe lines like:

$1 $2 $3
$1$2$3

that will get removed. This is done after the apply_rules call which converts the first into the same string as the 2nd. So in your case, there would be 95 rules, and then 10 more dupes, then during rule loading, those 10 are thrown away (I am pretty sure, but not 100% certain).

<!-- gh-comment-id:211585548 --> @jfoug commented on GitHub (Apr 18, 2016): Nope, I think rpp will remove these (I think). It is done with rules_remove_dups() at load time. I see many rule sets which have 123k, end up with 121k when loaded. It is this dup removal. There are dupe lines like: ``` $1 $2 $3 $1$2$3 ``` that will get removed. This is done after the apply_rules call which converts the first into the same string as the 2nd. So in your case, there would be 95 rules, and then 10 more dupes, then during rule loading, those 10 are thrown away (I am pretty sure, but not 100% certain).
Author
Owner

@jfoug commented on GitHub (Apr 19, 2016):

This 'rule-x-rule' may be better done as a offline script (for the time being). I will see if I can whip up a POC, it should not be too hard.

<!-- gh-comment-id:211977590 --> @jfoug commented on GitHub (Apr 19, 2016): This 'rule-x-rule' may be better done as a offline script (for the time being). I will see if I can whip up a POC, it should not be too hard.
Author
Owner

@jfoug commented on GitHub (Apr 19, 2016):

Well, here is config.c functions converted to Perl, just a bit simpler, lol. This jtrconf.pm module will be placed into run, and then we can easily make scripts to load and process john's configuration. (Not bad for 130 or so lines, lol)

# handles jtr .conf files (including the .include of files and sections)
package jtrconf;
use strict;
use warnings;
use Exporter;

my $basepath = '../run/';
my $confname = 'john.conf';
my %sections = ();

our @ISA= qw( Exporter );

# these CAN be exported.
our @EXPORT_OK = qw( setbasepath setname load getsection getparam );

# these are exported by default.
#our @EXPORT = qw( setbasepath setname getsection );

# 'default' is ../run/  Call this function to change that.
sub setbasepath {
    $basepath = $_[0];
    if (!defined $basepath || length($basepath) == 0) {
        $basepath = './';
    }
    if (substr($basepath, length($basepath)-1) ne '/') {
        $basepath .= '/';
    }
}

# set non-standard john.conf name
sub setname { $confname = $_[0]; }

# this inserts include data into the array 1 element
# past where we are (i.e. after the include line).
sub handle_include { my ($line, $i) = @_;
    # there are 4 include flavors.
    my $inc_name = substr($line, 10, length($line)-11);
    if (substr($line, 0, 10) eq '.include [') {
        # section
        $inc_name = lc $inc_name;
        splice $_[2], $i+1, 0, @{$sections{$inc_name}};
        return;
    }
    my $missing_ok = 0;
    my $dirname = "";
    if (substr($line, 0, 10) eq '.include <') {
        # basepath - forced.
        $dirname = $basepath;
    } elsif (substr($line, 0, 10) eq '.include "') {
        # local dir - forced
        $dirname = './';
    } elsif (substr($line, 0, 10) eq '.include \'') {
        # local dir - missing OK.
        $dirname = './';
        $missing_ok = 1;
    } else {
        die print STDERR "invalid . conf command $line\n";
    }
    if (-f $dirname.$inc_name) {
        open (FILE, $dirname.$inc_name);
        my @lines = (<FILE>);
        close (FILE);
        splice $_[2], $i+1, 0, @lines;
    } else {
        if ($missing_ok != 0) { return; }
        die print STDERR "can not include file $dirname.$inc_name\n";
    }
}

# this loads the .conf file.
sub load {
    open (FILE, $basepath.$confname) or die "cant open $basepath$confname\n";
    my @lines = (<FILE>);
    close (FILE);
    my $hc = 0;
    my @cur_section = ();
    my $cur_section_name="";
    for (my $i = 0; $i < scalar @lines; ++$i) {
        my $line = $lines[$i];
        chomp $line;
        if (length($line) == 0 || substr($line, 0, 1) eq '#') {next;}
        if (substr($line, 0, 9) eq '.include ') {
            handle_include($line, $i, \@lines);
            next;
        }
        if ($hc == 0 && substr($line, 0, 1) eq '[') {
            if (length($cur_section_name) > 0) {
                $sections{$cur_section_name} = [@cur_section];
            }
            $cur_section_name = lc substr($line, 1, length($line)-2);
            @cur_section = ();
            next;
        }
        if (substr($line, 0, 3) eq "!! ") {
            if ($line eq "!! hashcat logic ON") { $hc = 1; }
            if ($line eq "!! hashcat logic OFF") { $hc = 0; }
        }
        push @cur_section, "$line\n";
    }
    $sections{$cur_section_name} = @cur_section;
}

# returns a section's data.
sub getsection { my ($section) = @_;
    $section = lc $section;
    my @a = @{$sections{$section}};
    return @a;
}

# returns the data for a param (i.e. from Options).
sub getparam { my ($section, $param) = @_;
    $section = lc $section;
    my @a = @{$sections{$section}};
    foreach my $s (@a) {
        my $pos = index($s, '=');
        if ($pos > -1) {
            my $p = substr($s, 0, $pos);
            $p =~ s/\s+$//g;
            if (lc $param eq lc $p) {
                $s = substr($s, $pos+1);
                $s =~ s/^\s+//g;
                return $s;
            }
        }
    }
    return "";
}

1;
<!-- gh-comment-id:212067024 --> @jfoug commented on GitHub (Apr 19, 2016): Well, here is config.c functions converted to Perl, just a bit simpler, lol. This jtrconf.pm module will be placed into run, and then we can easily make scripts to load and process john's configuration. (Not bad for 130 or so lines, lol) ``` Perl # handles jtr .conf files (including the .include of files and sections) package jtrconf; use strict; use warnings; use Exporter; my $basepath = '../run/'; my $confname = 'john.conf'; my %sections = (); our @ISA= qw( Exporter ); # these CAN be exported. our @EXPORT_OK = qw( setbasepath setname load getsection getparam ); # these are exported by default. #our @EXPORT = qw( setbasepath setname getsection ); # 'default' is ../run/ Call this function to change that. sub setbasepath { $basepath = $_[0]; if (!defined $basepath || length($basepath) == 0) { $basepath = './'; } if (substr($basepath, length($basepath)-1) ne '/') { $basepath .= '/'; } } # set non-standard john.conf name sub setname { $confname = $_[0]; } # this inserts include data into the array 1 element # past where we are (i.e. after the include line). sub handle_include { my ($line, $i) = @_; # there are 4 include flavors. my $inc_name = substr($line, 10, length($line)-11); if (substr($line, 0, 10) eq '.include [') { # section $inc_name = lc $inc_name; splice $_[2], $i+1, 0, @{$sections{$inc_name}}; return; } my $missing_ok = 0; my $dirname = ""; if (substr($line, 0, 10) eq '.include <') { # basepath - forced. $dirname = $basepath; } elsif (substr($line, 0, 10) eq '.include "') { # local dir - forced $dirname = './'; } elsif (substr($line, 0, 10) eq '.include \'') { # local dir - missing OK. $dirname = './'; $missing_ok = 1; } else { die print STDERR "invalid . conf command $line\n"; } if (-f $dirname.$inc_name) { open (FILE, $dirname.$inc_name); my @lines = (<FILE>); close (FILE); splice $_[2], $i+1, 0, @lines; } else { if ($missing_ok != 0) { return; } die print STDERR "can not include file $dirname.$inc_name\n"; } } # this loads the .conf file. sub load { open (FILE, $basepath.$confname) or die "cant open $basepath$confname\n"; my @lines = (<FILE>); close (FILE); my $hc = 0; my @cur_section = (); my $cur_section_name=""; for (my $i = 0; $i < scalar @lines; ++$i) { my $line = $lines[$i]; chomp $line; if (length($line) == 0 || substr($line, 0, 1) eq '#') {next;} if (substr($line, 0, 9) eq '.include ') { handle_include($line, $i, \@lines); next; } if ($hc == 0 && substr($line, 0, 1) eq '[') { if (length($cur_section_name) > 0) { $sections{$cur_section_name} = [@cur_section]; } $cur_section_name = lc substr($line, 1, length($line)-2); @cur_section = (); next; } if (substr($line, 0, 3) eq "!! ") { if ($line eq "!! hashcat logic ON") { $hc = 1; } if ($line eq "!! hashcat logic OFF") { $hc = 0; } } push @cur_section, "$line\n"; } $sections{$cur_section_name} = @cur_section; } # returns a section's data. sub getsection { my ($section) = @_; $section = lc $section; my @a = @{$sections{$section}}; return @a; } # returns the data for a param (i.e. from Options). sub getparam { my ($section, $param) = @_; $section = lc $section; my @a = @{$sections{$section}}; foreach my $s (@a) { my $pos = index($s, '='); if ($pos > -1) { my $p = substr($s, 0, $pos); $p =~ s/\s+$//g; if (lc $param eq lc $p) { $s = substr($s, $pos+1); $s =~ s/^\s+//g; return $s; } } } return ""; } 1; ```
Author
Owner

@magnumripper commented on GitHub (Apr 19, 2016):

See also #745

You want me to close this one, and move these messages to the prior?

Not really, just consider it. Fixing either may close both - or not, depending on what we end up doing and planning.

<!-- gh-comment-id:212118892 --> @magnumripper commented on GitHub (Apr 19, 2016): > > See also #745 > > You want me to close this one, and move these messages to the prior? Not really, just consider it. Fixing either may close both - or not, depending on what we end up doing and planning.
Author
Owner

@jfoug commented on GitHub (Apr 20, 2016):

there is still work to be done on the script. stacking wordlist on wordlist gives lots of lines that fail. Things like this are not happy in rpp:

-c >3 !?X l Q -c >3 !?X l Q

The 2nd -c is triggering a 'invalid length'. That may be a bug in my HC logic also, I have not checked. But this is easy to work around. All I need is to unique the - rules, and then probably pack them up against the start of the rule. But after fixing things like this, stacking wordlist on wordlist is 'working'. I am finding a couple of words, but it's pretty slow. Yes, there are MUCH better rule sets to stack, and things will likely work better (I am trying o1 and a num appender and it is working, BUT that rule can be done with a 1 liner using the RPP)

<!-- gh-comment-id:212578717 --> @jfoug commented on GitHub (Apr 20, 2016): there is still work to be done on the script. stacking wordlist on wordlist gives lots of lines that fail. Things like this are not happy in rpp: ``` -c >3 !?X l Q -c >3 !?X l Q ``` The 2nd -c is triggering a 'invalid length'. That may be a bug in my HC logic also, I have not checked. But this is easy to work around. All I need is to unique the - rules, and then probably pack them up against the start of the rule. But after fixing things like this, stacking wordlist on wordlist is 'working'. I am finding a couple of words, but it's pretty slow. Yes, there are MUCH better rule sets to stack, and things will likely work better (I am trying o1 and a num appender and it is working, BUT that rule can be done with a 1 liner using the RPP)
Author
Owner

@magnumripper commented on GitHub (Aug 14, 2016):

Perhaps we should do this at a smarter level, using 2 (or n) rule contexts (and rpp contexts). That should work perfectly but may not be the very fastest solution.

<!-- gh-comment-id:239683783 --> @magnumripper commented on GitHub (Aug 14, 2016): Perhaps we should do this at a smarter level, using 2 (or _n_) rule contexts (and rpp contexts). That should work perfectly but may not be the very fastest solution.
Author
Owner

@magnumripper commented on GitHub (Dec 14, 2018):

5be3ed49
Stacked rules. New option --rules-stack=(SECTION[,..]|:rule[;..]) adds a second pass of rules, applied after normal processing.

Example: --single --rules-stack=ShiftToggle will run single mode's normal rules fully through the first (no-op) rule in the toggle ruleset. Only then will the latter set move on to its second rule, the main rules are rewinded, and so on.

In all other cases, eg. --wordlist --rules --rules-stack=ShiftToggle, the stacked rules (ShiftToggle in this case) will do a full pass before the base rule moves to its second rule.

This option also works fine with ALL modes that normally don't support rules at all, eg. --mask=password?d?d?d --rules-stack=shifttoggle although that will disable any GPU-side mask accleration.

You can even use --wordlist --rules-stack=SomeRule without using any base rule. The difference is in how it iterates: Normal --rules will run all words through the first rule before moving the second rule, while --rules-stack will apply all rules to the first word before moving on to the second word from the list.


My plan was doing a PR for the above commit but I accidentally pushed it to main branch. No biggie, it got past the bots and I tested it a lot already over last week. Beware of teething problems though.

This new functionality has no extra resume support so if the 2nd ruleset is huge, resuming may re-do a good deal of work before catching up. OTOH it shouldn't miss anything (that would be worse).

A known problem (by design) is the stacked rules are applied LAST, so they are applied after external mode and/or filter too. Shouldn't be a problem as long as you know it (perhaps a warning notice should be printed when used with external filters). Also, if used with a GPU-side mask capable format, the internal mask generation will be disabled, no way to work around that really. Though I think with some work I can make it work with hybrid mask - as of now it doesn't.

<!-- gh-comment-id:447508799 --> @magnumripper commented on GitHub (Dec 14, 2018): 5be3ed49 Stacked rules. New option `--rules-stack=(SECTION[,..]|:rule[;..])` adds a second pass of rules, applied after normal processing. Example: `--single --rules-stack=ShiftToggle` will run single mode's normal rules fully through the first (no-op) rule in the toggle ruleset. Only then will the latter set move on to its second rule, the main rules are rewinded, and so on. In all other cases, eg. `--wordlist --rules --rules-stack=ShiftToggle`, the stacked rules (ShiftToggle in this case) will do a full pass before the base rule moves to its second rule. This option also works fine with ALL modes that normally don't support rules at all, eg. `--mask=password?d?d?d --rules-stack=shifttoggle` although that will disable any GPU-side mask accleration. You can even use `--wordlist --rules-stack=SomeRule` without using any base rule. The difference is in how it iterates: Normal `--rules` will run all words through the first rule before moving the second rule, while `--rules-stack` will apply all rules to the first word before moving on to the second word from the list. --- My plan was doing a PR for the above commit but I accidentally pushed it to main branch. No biggie, it got past the bots and I tested it a lot already over last week. Beware of teething problems though. This new functionality has *no* extra resume support so if the 2nd ruleset is huge, resuming may re-do a good deal of work before catching up. OTOH it shouldn't miss anything (that would be worse). A known problem (by design) is the stacked rules are applied **LAST**, so they are applied *after* external mode and/or filter too. Shouldn't be a problem as long as you know it (perhaps a warning notice should be printed when used with external filters). Also, if used with a GPU-side mask capable format, the internal mask generation will be disabled, no way to work around that really. Though I think with some work I can make it work with hybrid mask - as of now it doesn't.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
openwall/john#1417
No description provided.