forked from openwall/john
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jtrconf.pm
143 lines (132 loc) · 3.54 KB
/
jtrconf.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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
# handles jtr .conf files (including the .include of files and sections)
package jtrconf;
use strict;
use warnings;
use Exporter;
use File::Basename;
my $basepath = dirname(__FILE__).'/';
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) {
#if (@cur_section < 2) {
# $sections{$cur_section_name} = $cur_section[0];
#} else {
$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 = ();
#if (@sections{$section} == 1) {
# print "$sections{$section}\n";
# print "@sections{$section}\n";
# print "@{$sections{$section}}\n";
# push @a, $sections{$section};
#} else {
# no strict 'refs';
@a = @{$sections{$section}};
# use strict;
#}
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;