Skip to content

Commit 409af88

Browse files
committed
formatting update
1 parent bbc9d00 commit 409af88

7 files changed

Lines changed: 1172 additions & 1168 deletions

File tree

bin/abuse_imap_downloader.php

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
1-
<?php
2-
/**
3-
* Abuse IMAP Downloader
4-
* Checks the various configured IMAP accounts and mailboxes for abuse complaints or spam
5-
* and registers them with our system and after matching them up to a specific service
6-
* through its ip, we contact the clients letting them know of the abuse complaint.
7-
*
8-
* @author Joe Huss <detain@interserver.net>
9-
* @copyright 2019
10-
* @package Abuse
11-
* @category Crontab
12-
*/
13-
14-
include_once __DIR__.'/../../../../include/functions.inc.php';
15-
include_once __DIR__.'/../src/ImapAbuseCheck.php';
16-
$GLOBALS['tf']->session->create(160307, 'services', false, 0, false, substr(basename($_SERVER['argv'][0], '.php'), 0, 32));
17-
$db = $GLOBALS['tf']->db;
18-
$db->query('select abuse_ip,count(abuse_lid) as count from abuse where Year(abuse_time)=Year(now()) and Month(abuse_time)=Month(now()) and Day(abuse_time)=Day(now()) group by abuse_ip');
19-
$abuse_ips = [];
20-
$total_sent = 0;
21-
while ($db->next_record(MYSQL_ASSOC)) {
22-
$total_sent += $db->Record['count'];
23-
$abuse_ips[$db->Record['abuse_ip']] = $db->Record['count'];
24-
}
25-
echo 'Loaded '.$total_sent.' Abuse Records From Today for '.count($abuse_ips).' Unique Email Addresses'.PHP_EOL;
26-
$checks = json_decode(file_get_contents(INCLUDE_ROOT.'/config/abuse.json'), true);
27-
foreach ($checks as $check) {
28-
$abuse = new ImapAbuseCheck('{'.$check['host'].':'.$check['port'].'/imap/ssl}'.$check['mailbox'], ABUSE_IMAP_USER, ABUSE_IMAP_PASS, $db, $check['delete_attachments'], $check['mail_limit']);
29-
foreach ($check['patterns'] as $pattern) {
30-
if ($pattern['type'] == 'match') {
31-
$abuse->register_preg_match($pattern['pattern'], $pattern['what']);
32-
} elseif ($pattern['type'] == 'match_all') {
33-
$abuse->register_preg_match_all($pattern['pattern'], $pattern['what']);
34-
}
35-
}
36-
$abuse->process($check['type'], $check['limit']);
37-
}
1+
<?php
2+
/**
3+
* Abuse IMAP Downloader
4+
* Checks the various configured IMAP accounts and mailboxes for abuse complaints or spam
5+
* and registers them with our system and after matching them up to a specific service
6+
* through its ip, we contact the clients letting them know of the abuse complaint.
7+
*
8+
* @author Joe Huss <detain@interserver.net>
9+
* @copyright 2019
10+
* @package Abuse
11+
* @category Crontab
12+
*/
13+
14+
include_once __DIR__.'/../../../../include/functions.inc.php';
15+
include_once __DIR__.'/../src/ImapAbuseCheck.php';
16+
$GLOBALS['tf']->session->create(160307, 'services', false, 0, false, substr(basename($_SERVER['argv'][0], '.php'), 0, 32));
17+
$db = $GLOBALS['tf']->db;
18+
$db->query('select abuse_ip,count(abuse_lid) as count from abuse where Year(abuse_time)=Year(now()) and Month(abuse_time)=Month(now()) and Day(abuse_time)=Day(now()) group by abuse_ip');
19+
$abuse_ips = [];
20+
$total_sent = 0;
21+
while ($db->next_record(MYSQL_ASSOC)) {
22+
$total_sent += $db->Record['count'];
23+
$abuse_ips[$db->Record['abuse_ip']] = $db->Record['count'];
24+
}
25+
echo 'Loaded '.$total_sent.' Abuse Records From Today for '.count($abuse_ips).' Unique Email Addresses'.PHP_EOL;
26+
$checks = json_decode(file_get_contents(INCLUDE_ROOT.'/config/abuse.json'), true);
27+
foreach ($checks as $check) {
28+
$abuse = new ImapAbuseCheck('{'.$check['host'].':'.$check['port'].'/imap/ssl}'.$check['mailbox'], ABUSE_IMAP_USER, ABUSE_IMAP_PASS, $db, $check['delete_attachments'], $check['mail_limit']);
29+
foreach ($check['patterns'] as $pattern) {
30+
if ($pattern['type'] == 'match') {
31+
$abuse->register_preg_match($pattern['pattern'], $pattern['what']);
32+
} elseif ($pattern['type'] == 'match_all') {
33+
$abuse->register_preg_match_all($pattern['pattern'], $pattern['what']);
34+
}
35+
}
36+
$abuse->process($check['type'], $check['limit']);
37+
}

bin/clean_up_abuse_headers.php

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
include __DIR__.'/../../../../include/functions.inc.php';
34
include_once __DIR__.'/../src/ImapAbuseCheck.php';
45
$db = get_module_db('default');
@@ -7,21 +8,21 @@
78
$offset = 0;
89
$continue = true;
910
while ($continue == true) {
10-
$start = time();
11-
echo "Grabbing Rows $offset - ".($offset+$limit);
12-
$db->query("select * from abuse left join abuse_data using (abuse_id) where abuse_headers != '' limit $offset, $limit;");
13-
echo " got ".$db->num_rows()." rows";
14-
if ($db->num_rows() < $limit) {
15-
$continue = false;
16-
}
17-
$offset += $limit;
18-
while ($db->next_record(MYSQL_ASSOC)) {
19-
$out = ImapAbuseCheck::fix_headers($db->Record['abuse_headers']);
20-
if ($out != $db->Record['abuse_headers']) {
21-
$db2->query("update abuse_data set abuse_headers='".$db->real_escape($out)."' where abuse_id={$db->Record['abuse_id']}");
22-
//echo ".";
23-
}
24-
}
25-
$end = time();
26-
echo " processed in ".($end-$start)." seconds\n";
11+
$start = time();
12+
echo "Grabbing Rows $offset - ".($offset+$limit);
13+
$db->query("select * from abuse left join abuse_data using (abuse_id) where abuse_headers != '' limit $offset, $limit;");
14+
echo " got ".$db->num_rows()." rows";
15+
if ($db->num_rows() < $limit) {
16+
$continue = false;
17+
}
18+
$offset += $limit;
19+
while ($db->next_record(MYSQL_ASSOC)) {
20+
$out = ImapAbuseCheck::fix_headers($db->Record['abuse_headers']);
21+
if ($out != $db->Record['abuse_headers']) {
22+
$db2->query("update abuse_data set abuse_headers='".$db->real_escape($out)."' where abuse_id={$db->Record['abuse_id']}");
23+
//echo ".";
24+
}
25+
}
26+
$end = time();
27+
echo " processed in ".($end-$start)." seconds\n";
2728
}

bin/match_abuse.php

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,52 @@
11
<?php
22

3-
use \MyDb\Mysqli\Db;
3+
use MyDb\Mysqli\Db;
44

55
include __DIR__.'/../../../../include/functions.inc.php';
66
$mb_db = new Db(ZONEMTA_MYSQL_DB, ZONEMTA_MYSQL_USERNAME, ZONEMTA_MYSQL_PASSWORD, ZONEMTA_MYSQL_HOST);
77
$mongo_client= new \MongoDB\Client('mongodb://'.ZONEMTA_USERNAME.':'.rawurlencode(ZONEMTA_PASSWORD).'@'.ZONEMTA_HOST.':27017/');
88
$mongo_users = $mongo_client->selectDatabase('zone-mta')->selectCollection('users');
99
$mb_users = [];
1010
$result = $mongo_users->find();
11-
foreach ($result as $user)
12-
$mb_users[] = $user->username;
11+
foreach ($result as $user) {
12+
$mb_users[] = $user->username;
13+
}
1314
$ips = explode("\n", trim(`grep address /home/sites/zone-mta/config/pools.js |cut -d\" -f4`));
1415
$db = get_module_db('mail');
1516
$db2 = get_module_db('mail');
16-
$db->query("select abuse.*, abuse_plainmsg from abuse left join abuse_data using (abuse_id) where abuse_ip in ('".implode("','",$ips)."') and (abuse_plainmsg like '%Authenticated sender: %' or abuse_plainmsg like '%smtp.auth=%');");
17+
$db->query("select abuse.*, abuse_plainmsg from abuse left join abuse_data using (abuse_id) where abuse_ip in ('".implode("','", $ips)."') and (abuse_plainmsg like '%Authenticated sender: %' or abuse_plainmsg like '%smtp.auth=%');");
1718
while ($db->next_record(MYSQL_ASSOC)) {
18-
$mbUser = null;
19-
$mbId = null;
20-
if (preg_match_all('/Authenticated sender: (?P<user>[^\)]*)\)/ms', $db->Record['abuse_plainmsg'], $matches) ||
21-
preg_match_all('/smtp.auth=(?P<user>\S*)\s/ms', $db->Record['abuse_plainmsg'], $matches)) {
22-
foreach ($matches['user'] as $user) {
23-
if (in_array($user, $mb_users)) {
24-
$mbUser = $user;
25-
echo 'Abuse ID '.$db->Record['abuse_id'].' found MailBaby user '.$mbUser.PHP_EOL;
26-
}
27-
}
28-
}
29-
if (preg_match_all('/^ by (\S+|\S+ \(\S+\)) with (LMP|SMTP|ESMTP|ESMTPA|ESMTPS|ESMTPSA|HTTP) id (\S+)\.(\d{3})\s*$/mU', $db->Record['abuse_plainmsg'], $matches)) {
30-
$ids = $matches[3];
31-
foreach ($ids as $id) {
32-
$mb_db->query("select * from mail_messagestore where id='{$id}'");
33-
if ($mb_db->num_rows() > 0) {
34-
$mb_db->next_record(MYSQL_ASSOC);
35-
$mbId = $id;
36-
$mbUser = $mb_db->Record['user'];
37-
echo 'Abuse ID '.$db->Record['abuse_id'].' found MailBaby mail id '.$mbId.' user '.$mbUser.PHP_EOL;
38-
}
39-
}
40-
}
41-
$updates = [];
42-
if (!is_null($mbUser))
43-
$updates[] = "abuse_mb_user='{$mbUser}'";
44-
if (!is_null($mbId))
45-
$updates[] = "abuse_mb_id='{$mbId}'";
46-
if (count($updates) > 0) {
47-
$db2->query("update abuse set ".implode(', ', $updates)." where abuse_id='{$db->Record['abuse_id']}'");
48-
}
19+
$mbUser = null;
20+
$mbId = null;
21+
if (preg_match_all('/Authenticated sender: (?P<user>[^\)]*)\)/ms', $db->Record['abuse_plainmsg'], $matches) ||
22+
preg_match_all('/smtp.auth=(?P<user>\S*)\s/ms', $db->Record['abuse_plainmsg'], $matches)) {
23+
foreach ($matches['user'] as $user) {
24+
if (in_array($user, $mb_users)) {
25+
$mbUser = $user;
26+
echo 'Abuse ID '.$db->Record['abuse_id'].' found MailBaby user '.$mbUser.PHP_EOL;
27+
}
28+
}
29+
}
30+
if (preg_match_all('/^ by (\S+|\S+ \(\S+\)) with (LMP|SMTP|ESMTP|ESMTPA|ESMTPS|ESMTPSA|HTTP) id (\S+)\.(\d{3})\s*$/mU', $db->Record['abuse_plainmsg'], $matches)) {
31+
$ids = $matches[3];
32+
foreach ($ids as $id) {
33+
$mb_db->query("select * from mail_messagestore where id='{$id}'");
34+
if ($mb_db->num_rows() > 0) {
35+
$mb_db->next_record(MYSQL_ASSOC);
36+
$mbId = $id;
37+
$mbUser = $mb_db->Record['user'];
38+
echo 'Abuse ID '.$db->Record['abuse_id'].' found MailBaby mail id '.$mbId.' user '.$mbUser.PHP_EOL;
39+
}
40+
}
41+
}
42+
$updates = [];
43+
if (!is_null($mbUser)) {
44+
$updates[] = "abuse_mb_user='{$mbUser}'";
45+
}
46+
if (!is_null($mbId)) {
47+
$updates[] = "abuse_mb_id='{$mbId}'";
48+
}
49+
if (count($updates) > 0) {
50+
$db2->query("update abuse set ".implode(', ', $updates)." where abuse_id='{$db->Record['abuse_id']}'");
51+
}
4952
}
50-

0 commit comments

Comments
 (0)