-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonparser.php
84 lines (84 loc) · 2.93 KB
/
jsonparser.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
<?php
ini_set('memory_limit', '4000M');
if (count($argv) >= 2) {
$filename = $argv[1];
@$fp = fopen($filename, "r");
if (false === $fp) {
die("Failed to open file : {$filename}");
}
//read file.
$issuer_map = [];
while(!feof($fp)) {
$line = fgets($fp);
if (false === $line) {
break;
}
$json_info = json_decode($line, true);
if (!is_null($json_info) and count($json_info) > 0) {
if (isset($json_info['parsed']['issuer'])) {
$issuer_dn = 'Unknown';
$discard_flag = false;
if (isset($json_info['parsed']['issuer_dn'])) {
$issuer_dn = $json_info['parsed']['issuer_dn'];
$issuer_dn_arr = preg_split('/,\s/', $issuer_dn);
$keyname = [];
foreach ($issuer_dn_arr as $name) {
if (false !== strpos($name, 'CN=')) {
$ip_address = explode('CN=', $name)[1];
if (filter_var($ip_address, FILTER_VALIDATE_IP)) {
$discard_flag = true;
unset($ip_address);
}
}
if (false !== strpos($name, 'O=')
or false !== strpos($name, 'OU=')
or false !== strpos($name, 'CN=')
or false !== strpos($name, 'C=')
) {
$keyname[] = $name;
}
}
asort($keyname);
$issuer_dn = implode(',', $keyname);
unset($keyname);
unset($issuer_dn_arr);
unset($name);
}
if (!$discard_flag) {
if (isset($issuer_map[$issuer_dn])) {
$issuer_map[$issuer_dn] ;
} else {
$issuer_map[$issuer_dn] = 1;
}
}
unset($issuer_dn);
}
}
unset($json_info);
unset($line);
}
if ($fp) {
fclose($fp);
}
print "Sorting the output..\n";
arsort($issuer_map);
print "Going to dump static data:\n";
$filename = tempnam('./', 'output_');
print $filename."\n";
@$outfp = fopen($filename, "w");
if (false === $outfp) {
print "Failed to write to output file. Dump to stdout.\n";
foreach ($issuer_map as $key => $value) {
print "{$key}, {$value}\n";
}
} else {
fwrite($outfp, "Issuer Common Name, Number of Certificates\n");
foreach ($issuer_map as $key => $value) {
fwrite($outfp, "{$key}, {$value}\n");
fflush($outfp);
}
fclose($outfp);
}
} else {
die("Please specify the filepath");
}