-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyse.php
executable file
·154 lines (142 loc) · 4.46 KB
/
analyse.php
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
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env php
<?php
$g_resolved=array();
function init_cache_resolved()
{
global $g_resolved;
$json=file_get_contents("resolved_cache.json");
$g_resolved=json_decode($json, true);
}
function save_cache_resolved()
{
global $g_resolved;
$json=json_encode($g_resolved);
file_put_contents("resolved_cache.json", $json);
}
init_cache_resolved();
function display_record($new_record, $max_ip_length=20)
{
global $g_resolved;
$dkim=$new_record->row->policy_evaluated->dkim;
if($dkim=="fail")
$dkim_color="\033[31m";
else
$dkim_color="\033[32m";
$spf=$new_record->row->policy_evaluated->spf;
if($spf=="fail")
$spf_color="\033[31m";
else
$spf_color="\033[32m";
$source_ip=sprintf("%s",$new_record->row->source_ip);
if(!isset($g_resolved[$source_ip]))
$g_resolved[$source_ip] = gethostbyaddr($source_ip);
printf("\t%-5s mails reported : DKIM: %s%s\033[39m SPF: %s%s\033[39m VIA %".$max_ip_length."s resolved as %s",
$new_record->row->count,
$dkim_color,$dkim,
$spf_color,$spf,
$source_ip,
$g_resolved[$source_ip],
);
if(strlen($new_record->identifiers->envelope_to)>1)
printf("\tTO %s\n",$new_record->identifiers->envelope_to);
else
printf("\n");
}
if ($handle = opendir('.'))
{
$results=array();
$max_ip_length=0;
while (false !== ($entry = readdir($handle)))
{
if ($entry != "." && $entry != "..")
{
echo "\n==> $entry\n";
$zip = new ZipArchive;
$res = $zip->open($entry);
// pour les fichiers ZIP
if ($res === TRUE)
{
$content = file_get_contents("zip://$entry#".$zip->statIndex(0)["name"]);
$zip->close();
}
else
{
// pour les fichiers GZ et non compressés
ob_start(); // Start output buffering
readgzfile($entry);
$content = ob_get_contents(); // Store buffer in variable
ob_end_clean(); // End buffering and clean up
}
$dataXML=@simplexml_load_string($content);
if($dataXML)
{
printf("OK\n");
$element["report_metadata"]=$dataXML->report_metadata;
$element["policy_published"]=$dataXML->policy_published;
foreach($dataXML->record as $record)
{
$element["records"][] = $record;
$ip_length = strlen($record->row->source_ip);
if($ip_length > $max_ip_length)
$max_ip_length = $ip_length;
}
$element["file"]=$entry;
$results[]=$element;
unset($element);
}
else
{
printf("CANNOT BE PARSED\n");
}
}
}
closedir($handle);
}
// tri par date :
uasort($results,function ($a, $b) {
return strnatcmp($a["report_metadata"]->date_range->begin,$b["report_metadata"]->date_range->begin);
}
);
$LAST="";
foreach($results as $element)
{
$NEW=gmdate("Y-m-d", ''.$element["report_metadata"]->date_range->begin);
if($LAST !== $NEW)
echo "\n";
$LAST = $NEW;
printf("%s report for \033[36m %s \033[39m from %s '%s' : \n",
$NEW,
$element["policy_published"]->domain, // FOR
$element["report_metadata"]->email, // FROM
$element["file"]
);
// tri par ip source :
usort($element["records"],function ($a, $b) {
return strnatcmp($a->row->source_ip,$b->row->source_ip);
}
);
$new_record=null;
foreach($element["records"] as $record)
{
// le premier coup
if($new_record === null)
{
$new_record = $record;
continue;
}
// cumul si besoin
if( strcmp($new_record->row->source_ip , $record->row->source_ip) === 0 )
{
$new_record->row->count += $record->row->count;
$new_record->identifiers->envelope_to .= ", ". $record->identifiers->envelope_to;
// echo "CNT".$new_record->row->count."\n";
continue;
}
// sinon on affiche
display_record($new_record, $max_ip_length);
// next
$new_record = $record;
}
display_record($new_record, $max_ip_length);
}
save_cache_resolved();