Blame
Date:
Sun Oct 8 22:28:02 2023 UTC
Message:
Now prevents creating accounts that exist in database with a password
001
2023-04-14
jrmu
# Original author is izzyb@planetofnix.com
002
2023-02-10
jrmu
#!/usr/bin/perl
003
2023-02-10
jrmu
#
004
2023-02-10
jrmu
use strict;
005
2023-02-10
jrmu
#no strict 'refs';
006
2023-02-10
jrmu
use warnings;
007
2023-02-10
jrmu
use Data::Dumper;
008
2023-02-10
jrmu
# Bsd pledge/unveil security modules
009
2023-02-10
jrmu
use OpenBSD::Pledge;
010
2023-02-10
jrmu
use OpenBSD::Unveil;
011
2023-02-10
jrmu
012
2023-02-10
jrmu
# Database modules
013
2023-02-10
jrmu
use DBI;
014
2023-02-10
jrmu
use DBD::SQLite;
015
2023-02-10
jrmu
016
2023-02-10
jrmu
# setup log level constents
017
2023-02-10
jrmu
use constant {
018
2023-02-10
jrmu
NONE => 0,
019
2023-02-10
jrmu
ERRORS => 1,
020
2023-02-10
jrmu
WARNINGS => 2,
021
2023-02-10
jrmu
ALL => 3,
022
2023-02-10
jrmu
};
023
2023-02-10
jrmu
my $verbose = ERRORS;
024
2023-02-10
jrmu
sub debug {
025
2023-02-10
jrmu
my ($level, $msg) = @_;
026
2023-02-10
jrmu
if ($verbose >= $level) { print "$msg\n"; }
027
2023-02-10
jrmu
}
028
2023-02-10
jrmu
029
2023-02-12
jrmu
# location of local modules
030
2023-02-10
jrmu
use lib './';
031
2023-02-10
jrmu
032
2023-02-12
jrmu
# Date string to epock used in init_ip_xref
033
2023-02-12
jrmu
use Date::Parse;
034
2023-02-12
jrmu
035
2023-02-10
jrmu
my ($ipTable, $nameTable) = init_ip_xref();
036
2023-02-10
jrmu
037
2023-02-10
jrmu
while (my $username = shift) { #param 1 should be the name of a user to generate a report from.
038
2023-02-12
jrmu
my $dbFile = '/var/www/botnow/botnow.db';
039
2023-02-12
jrmu
my $dbh = connectdb($dbFile);
040
2023-02-12
jrmu
if (!defined($dbh)) {
041
2023-02-12
jrmu
die "failed to connect to $dbFile";
042
2023-02-12
jrmu
}
043
2023-02-12
jrmu
my $stmt=qq{select * from bnc join irc on (bnc.ircid = irc.id) where username is ?};
044
2023-02-12
jrmu
my $sth=$dbh->prepare($stmt);
045
2023-02-12
jrmu
$sth->execute($username) or die "execution failed: $dbh->errstr()";
046
2023-02-12
jrmu
while (my $row=$sth->fetchrow_hashref) {
047
2023-02-12
jrmu
my $dossier =qq{
048
2023-02-12
jrmu
Username: $row->{username}
049
2023-02-12
jrmu
Email Address: $row->{email}
050
2023-02-12
jrmu
$row->{hostmask}
051
2023-02-12
jrmu
$row->{ctcpversion}
052
2023-02-12
jrmu
$row->{ctcptime}
053
2023-02-12
jrmu
Registration Date: $row->{date}
054
2023-02-12
jrmu
};
055
2023-02-12
jrmu
print $dossier;
056
2023-02-12
jrmu
print "Same Email ["
057
2023-02-12
jrmu
. join(', ', @{$dbh->selectcol_arrayref(qq\Select username from bnc join irc on (bnc.ircid = irc.id) where email = ?\,undef,$row->{email})})
058
2023-02-12
jrmu
. "]\n";
059
2023-02-12
jrmu
print "Same Date ["
060
2023-02-12
jrmu
. join(', ', @{$dbh->selectcol_arrayref(qq\Select username from bnc join irc on (bnc.ircid = irc.id) where date = ?\,undef,$row->{date})})
061
2023-02-12
jrmu
. "]\n";
062
2023-02-12
jrmu
print "Same Hostmask ["
063
2023-02-12
jrmu
. join(', ', @{$dbh->selectcol_arrayref(qq\Select username from bnc join irc on (bnc.ircid = irc.id) where hostmask = ?\,undef,$row->{hostmask})})
064
2023-02-12
jrmu
. "]\n";
065
2023-02-12
jrmu
print Dumper($row);
066
2023-02-12
jrmu
print "Frequency of connections from: \n" . Dumper($nameTable->{$username});
067
2023-02-12
jrmu
print "Other Users connecting from: \n";
068
2023-02-12
jrmu
foreach my $ip (keys(%{$nameTable->{$username}})) {
069
2023-02-12
jrmu
my $thisLastConnect = @{ $nameTable->{ $row->{username} }->{$ip}->{epoch} }[-1];
070
2023-02-12
jrmu
print "$ip =>[";
071
2023-02-12
jrmu
foreach my $link (keys(%{ $ipTable->{$ip} })) {
072
2023-02-12
jrmu
my $linkLastConnect = @{ $nameTable->{$link}->{$ip}->{epoch} }[-1];
073
2023-02-12
jrmu
if (abs($thisLastConnect - $linkLastConnect) < 300) { # les then 5 min
074
2023-02-12
jrmu
print "**$link**, ";
075
2023-02-12
jrmu
} elsif (abs($thisLastConnect - $linkLastConnect) < 600) { # less then 10 min
076
2023-02-12
jrmu
print "*$link*, ";
077
2023-02-12
jrmu
} else {
078
2023-02-12
jrmu
print "$link, ";
079
2023-02-12
jrmu
}
080
2023-02-10
jrmu
}
081
2023-02-12
jrmu
print "]\n";
082
2023-02-10
jrmu
}
083
2023-02-12
jrmu
}
084
2023-02-10
jrmu
}
085
2023-02-10
jrmu
086
2023-02-10
jrmu
087
2023-02-10
jrmu
088
2023-02-10
jrmu
089
2023-02-10
jrmu
exit 0;
090
2023-02-10
jrmu
091
2023-02-10
jrmu
sub connectdb {
092
2023-02-10
jrmu
my $dbpath=shift;
093
2023-02-10
jrmu
my $dsn = "dbi:SQLite:dbname=$dbpath";
094
2023-02-10
jrmu
my $user = "";
095
2023-02-10
jrmu
my $password = "";
096
2023-02-10
jrmu
my $dbh = DBI->connect($dsn, $user, $password, {
097
2023-02-10
jrmu
PrintError => 1,
098
2023-02-10
jrmu
RaiseError => 1,
099
2023-02-10
jrmu
AutoCommit => 1,
100
2023-02-10
jrmu
FetchHashKeyName => 'NAME_lc',
101
2023-02-10
jrmu
}) or die "Couldn't connect to database: " . $DBI::errstr;
102
2023-02-10
jrmu
if (!(-s "$dbpath")) {
103
2023-02-10
jrmu
main::debug(ALL, "Cant locate $dbpath");
104
2023-02-10
jrmu
exit 1;
105
2023-02-10
jrmu
}
106
2023-02-10
jrmu
main::debug(ALL, "connected to $dbpath");
107
2023-02-10
jrmu
return $dbh;
108
2023-02-10
jrmu
}
109
2023-02-10
jrmu
# Read and index the znc log file.
110
2023-02-10
jrmu
sub init_ip_xref {
111
2023-02-10
jrmu
# Get IP addresses
112
2023-02-10
jrmu
my $ip2usernames={};
113
2023-02-10
jrmu
my $usernames2ip={};
114
2023-02-10
jrmu
open my $zncLog, '<', '/home/znc/home/znc/.znc/moddata/adminlog/znc.log' or die "Can't open znc log file";
115
2023-02-10
jrmu
while (my $line = <$zncLog>) {
116
2023-02-12
jrmu
if( $line =~/\[(.*)\].*\[(.*)\] connected to ZNC from (.*)/) {
117
2023-02-12
jrmu
my $timestamp=$1;
118
2023-02-12
jrmu
my $name=$2;
119
2023-02-12
jrmu
my $ip=$3;
120
2023-02-10
jrmu
if (!defined($ip2usernames->{$ip})) {
121
2023-02-10
jrmu
$ip2usernames->{$ip} = {};
122
2023-02-10
jrmu
}
123
2023-02-10
jrmu
if (!defined($ip2usernames->{$name})) {
124
2023-02-12
jrmu
$ip2usernames->{$ip}->{$name}={};
125
2023-02-12
jrmu
$ip2usernames->{$ip}->{$name}->{count}=0;
126
2023-02-12
jrmu
$ip2usernames->{$ip}->{$name}->{timestamps}=[];
127
2023-02-12
jrmu
$ip2usernames->{$ip}->{$name}->{epoch}=[];
128
2023-02-12
jrmu
129
2023-02-10
jrmu
}
130
2023-02-12
jrmu
$ip2usernames->{$ip}->{$name}->{count}++;
131
2023-02-12
jrmu
push (@{$ip2usernames->{$ip}->{$name}->{timestamps}}, $timestamp);
132
2023-02-12
jrmu
push (@{$ip2usernames->{$ip}->{$name}->{epoch}}, str2time($timestamp));
133
2023-02-12
jrmu
134
2023-02-10
jrmu
if (!defined($usernames2ip->{$name})) {
135
2023-02-10
jrmu
$usernames2ip->{$name}={};
136
2023-02-10
jrmu
}
137
2023-02-10
jrmu
if (!defined($usernames2ip->{$name}->{$ip})) {
138
2023-02-12
jrmu
$usernames2ip->{$name}->{$ip}={};
139
2023-02-12
jrmu
$usernames2ip->{$name}->{$ip}->{count}=0;
140
2023-02-12
jrmu
$usernames2ip->{$name}->{$ip}->{timestamps}=[];
141
2023-02-12
jrmu
$usernames2ip->{$name}->{$ip}->{epoch}=[];
142
2023-02-10
jrmu
}
143
2023-02-12
jrmu
$usernames2ip->{$name}->{$ip}->{count}++;
144
2023-02-12
jrmu
push (@{$usernames2ip->{$name}->{$ip}->{timestamps}}, $timestamp);
145
2023-02-12
jrmu
push (@{$usernames2ip->{$name}->{$ip}->{epoch}}, str2time($timestamp));
146
2023-02-10
jrmu
}
147
2023-02-10
jrmu
}
148
2023-02-10
jrmu
close $zncLog;
149
2023-02-10
jrmu
return $ip2usernames,$usernames2ip;
150
2023-02-10
jrmu
}
IRCNow