Skip to content

Commit 197fa31

Browse files
committed
updates to the abuse downloader code adding in matching up with mailbaby users and mail ids .. and added a new script to match the existing data as well
1 parent f5254a5 commit 197fa31

2 files changed

Lines changed: 95 additions & 0 deletions

File tree

bin/match_abuse.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
use \MyDb\Mysqli\Db;
4+
5+
include __DIR__.'/../../../../include/functions.inc.php';
6+
$mb_db = new Db(ZONEMTA_MYSQL_DB, ZONEMTA_MYSQL_USERNAME, ZONEMTA_MYSQL_PASSWORD, ZONEMTA_MYSQL_HOST);
7+
$mongo_client= new \MongoDB\Client('mongodb://'.ZONEMTA_USERNAME.':'.rawurlencode(ZONEMTA_PASSWORD).'@'.ZONEMTA_HOST.':27017/');
8+
$mongo_users = $mongo_client->selectDatabase('zone-mta')->selectCollection('users');
9+
$mb_users = [];
10+
$result = $mongo_users->find();
11+
foreach ($result as $user)
12+
$mb_users[] = $user->username;
13+
$ips = explode("\n", trim(`grep address /home/sites/zone-mta/config/pools.js |cut -d\" -f4`));
14+
$db = get_module_db('mail');
15+
$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+
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+
}
49+
}
50+

src/ImapAbuseCheck.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ class ImapAbuseCheck
2727
public $MC;
2828
public $limit_ips = false;
2929
public $ips = [];
30+
public $mongo_client;
31+
public $mb_db;
32+
public $mb_users = [];
33+
public $mb_ips = [];
3034
public $emails = [];
3135
public $abused = 0;
3236
public $db;
@@ -74,6 +78,13 @@ public function __construct($imap_server, $username, $password, $db, $delete_att
7478
} else {
7579
$this->load_client_ips();
7680
}
81+
$this->mb_db = new Db(ZONEMTA_MYSQL_DB, ZONEMTA_MYSQL_USERNAME, ZONEMTA_MYSQL_PASSWORD, ZONEMTA_MYSQL_HOST);
82+
$this->mb_ips = explode("\n", trim(`grep address /home/sites/zone-mta/config/pools.js |cut -d\" -f4`));
83+
$this->mongo_client= new \MongoDB\Client('mongodb://'.ZONEMTA_USERNAME.':'.rawurlencode(ZONEMTA_PASSWORD).'@'.ZONEMTA_HOST.':27017/');
84+
$this->mongo_users = $this->mongo_client->selectDatabase('zone-mta')->selectCollection('users');
85+
$result = $this->mongo_users->find();
86+
foreach ($result as $user)
87+
$this->mb_users[] = $user->username;
7788
$this->connect();
7889
function_requirements('get_server_from_ip');
7990
}
@@ -249,11 +260,43 @@ public function process($type = 'spam', $limit = false)
249260
}
250261
}
251262
if ($ip !== false && validIp($ip, false) && (in_array($ip, $this->all_ips) || in_array($ip, $this->client_ips))) {
263+
$mbUser = null;
264+
$mbId = null;
252265
if (in_array($ip, $this->client_ips)) {
253266
$server_data = ['email' => 'sreekanth@nettlinxinc.com'];
254267
} else {
255268
$server_data = get_server_from_ip($ip);
256269
}
270+
if (in_array($ip, $this->mb_ips)) {
271+
if (preg_match_all('/Authenticated sender: (?P<user>[^\)]*)\)/ms', $this->plainmsg, $matches) ||
272+
preg_match_all('/smtp.auth=(?P<user>\S*)\s/ms', $this->plainmsg, $matches)) {
273+
foreach ($matches['user'] as $user) {
274+
if (in_array($user, $this->mb_users)) {
275+
$mbUser = $this->mb_db->real_escape($user);
276+
$this->mb_db->query("select * from mail where mail_username='{$mbUser}'");
277+
if ($this->mb_db->num_rows() > 0) {
278+
$this->mb_db->next_record(MYSQL_ASSOC);
279+
$data = $GLOBALS['tf']->accounts->read($this->mb_db->Record['mail_custid']);
280+
$email = (!isset($data['email_abuse']) || trim($data['email_abuse']) == '') ? $data['email'] : $data['email_abuse'];
281+
$server_data = [
282+
'email' => $data['emai'],
283+
'status' => $this->mb_db->Record['mail_status']
284+
];
285+
}
286+
}
287+
}
288+
}
289+
if (preg_match_all('/^ by (\S+|\S+ \(\S+\)) with (LMP|SMTP|ESMTP|ESMTPA|ESMTPS|ESMTPSA|HTTP) id (\S+)\.(\d{3})\s*$/mU', $this->plainmsg, $matches)) {
290+
$ids = $matches[3];
291+
foreach ($ids as $id) {
292+
$id = $this->mb_db->real_escape($id);
293+
$this->mb_db->query("select * from mail_messagestore where id='{$id}'");
294+
if ($this->mb_db->num_rows() > 0) {
295+
$mbId = $id;
296+
}
297+
}
298+
}
299+
}
257300
if (mb_substr($ip, 0, 10) == '66.45.228.' || (isset($server_data['email']) && $server_data['email'] != '')) {
258301
// if ($this->abused >= 5) exit;
259302
$email = (null === $server_data['email_abuse'] ? $server_data['email'] : $server_data['email_abuse']);
@@ -279,6 +322,8 @@ public function process($type = 'spam', $limit = false)
279322
->setAmount(1)
280323
->setLid($email)
281324
->setStatus('pending')
325+
->setMbUser($mbUser)
326+
->setMbId($mbId)
282327
->save();
283328
$id = $abuse->getId();
284329
$abuseData = new Abuse_Data($db);

0 commit comments

Comments
 (0)