<?php
// Includes
include_once("include/config.php");
include_once("include/functions.php");
include_once("include/opendb.php");
?>
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="robots" content="all" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="icon" href="/favicon.ico">
    <?php
    if(isset($_GET['sha1'])) {
      if(preg_match("/^[a-z0-9]{40}$/", $_GET['sha1'])) {
        $res = mysqli_query($connection, "SELECT sha1, subject_cn, subject, issuerdn_cn, issuerdn, version, firstseen, malicious, reason, listingdate, (SELECT count(distinct(fk_hash)) FROM ztbl_reports_tlslog AS z WHERE z.fk_sha1 = t.sha1) AS sample_cnt, (SELECT count(distinct(dstip)) FROM ztbl_reports_tlslog AS z WHERE z.fk_sha1 = t.sha1) AS ip_cnt FROM tbl_tlslog AS t WHERE sha1 = '".validateinput($_GET['sha1'])."' LIMIT 1");
        if(mysqli_num_rows($res)) {
          $row = mysqli_fetch_array($res);
          echo "<title>SSLBL | SSL Certificate: SHA1 ".$row['sha1']."</title>\n";
          if($row['malicious']) {
            echo "<meta name=\"description\" content=\"The SSL certificate with the SHA1 fingerprint ".$_GET['sha1']." has been identified to be associated with a ".$row['reason']."\">\n";
            echo "<meta name=\"keywords\" content=\"abuse.ch, malware, botnets, ssl, tls, blacklist, blocklist, ioc, list, fingerprint, sha1, certificate, ".$row['reason']."\"/>\n";
          }
          else {
            echo "<meta name=\"description\" content=\"Browse malicious SSL Certificates\">\n";
            echo "<meta name=\"keywords\" content=\"abuse.ch, malware, botnets, ssl, tls, blacklist, blocklist, ioc, list, fingerprint, sha1, certificate\"/>\n";
          }
        }
        else {
          echo "<title>SSLBL | Malicious SSL Certificates</title>\n";
          echo "<meta name=\"description\" content=\"Browse malicious SSL Certificates\">\n";
          echo "<meta name=\"keywords\" content=\"abuse.ch, malware, botnets, ssl, tls, blacklist, blocklist, ioc, list, fingerprint, sha1, certificate\"/>\n";
        }
      }
      else {
        echo "<title>SSLBL | Malicious SSL Certificates</title>\n";
        echo "<meta name=\"description\" content=\"Browse malicious SSL Certificates\">\n";
        echo "<meta name=\"keywords\" content=\"abuse.ch, malware, botnets, ssl, tls, blacklist, blocklist, ioc, list, fingerprint, sha1, certificate\"/>\n";
      }
    }
    else {
      echo "<title>SSLBL | Malicious SSL Certificates</title>\n";
      echo "<meta name=\"description\" content=\"Browse malicious SSL Certificates\">\n";
      echo "<meta name=\"keywords\" content=\"abuse.ch, malware, botnets, ssl, tls, blacklist, blocklist, ioc, list, fingerprint, sha1, certificate\"/>\n";
    }
    ?>

    <!-- Bootstrap core CSS -->
    <link href="/css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles -->
    <link href="/css/jumbotron.css" rel="stylesheet">
    <link href="/css/custom.css" rel="stylesheet">
    <!-- Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-HHH89MZ0FK"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());

      gtag('config', 'G-HHH89MZ0FK');
    </script>
  </head>

  <body>
    <header>
      <nav class="navbar navbar-expand-md navbar-dark fixed-top bg-blue">
        <div class="container">
        <a class="navbar-brand" href="/">
          <img src="/images/sslbl_logo.png" alt="SSLBL">
        </a>
        <button class="navbar-toggler custom-toggler" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
        </button>

        <div class="collapse navbar-collapse" id="navbarsExampleDefault">
          <ul class="navbar-nav ml-auto">
            <li class="nav-item">
              <a class="nav-link active" href="/ssl-certificates/" title="Browse maliciousSSL Certificates">SSL Certificates</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="/ja3-fingerprints/" title="Borwse malicious JA3 Fingerprints">JA3 Fingerprints</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="/blacklist/" title="Feeds">Blacklist</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="/statistics/" title="Statistics">Statistics</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="/about/" title="About">About</a>
            </li>
          </ul>
        </div>
      </div>
      </nav>
    </header>

    <!-- breadcrumb -->
    <div class="container">
      <nav aria-label="breadcrumb">
        <ol class="breadcrumb">
          <li class="breadcrumb-item"><a href="/ssl-certificates/">SSL Certificates</a></li>
          <li class="breadcrumb-item active" aria-current="page">SHA1 Fingerprint</li>
        </ol>
      </nav>
    </div>

    <!-- Main content -->
    <main class="container">
      <h1 class="mt-5">SSL Certificates</h1>
      <?php
      if(isset($_GET['sha1'])) {
        if(preg_match("/^[a-z0-9]{40}$/", $_GET['sha1'])) {
          if(mysqli_num_rows($res)) {
            $lastseen = 'never';
            $rr = mysqli_query($connection, "SELECT timestamp AS ts FROM ztbl_reports_tlslog WHERE fk_sha1 = '".$row['sha1']."' ORDER BY ts DESC LIMIT 1");
            $rr = mysqli_fetch_array($rr);
            if($rr['ts'] != $row['firstseen']) {
              $lastseen = $rr['ts']." UTC";
            }
            ?>
            <p>The following table shows further information as well as a list of malware samples including the corresponding botnet C&amp;C associated with the SSL certificate fingerprint <?php echo $row['sha1']; ?>.</p>
            <h2>Database Entry</h2>
            <hr>
            <table class="table table-sm table-bordered">
              <tr><th>SHA1 Fingerprint:</th><td><?php echo $row['sha1'];?></td></tr>
              <tr><th>Certificate Common Name (CN):</th><td><?php echo $row['subject_cn'];?></td></tr>
              <tr><th>Issuer Distinguished Name (DN):</th><td><?php echo $row['issuerdn_cn'];?></td></tr>
              <tr><th>TLS Version:</th><td><?php echo $row['version'];?></td></tr>
              <tr><th>First seen:</th><td><?php echo $row['firstseen'];?> UTC</td></tr>
              <tr><th>Last seen:</th><td><?php echo $lastseen;?></td></tr>
              <tr class="table-danger"><th>Status:</th><td>Blacklisted</td></tr>
              <tr><th>Listing reason:</th><td><?php echo $row['reason'];?></td></tr>
              <tr><th>Listing date:</th><td><?php echo $row['listingdate'];?></td></tr>
              <tr><th>Malware samples:</th><td><?php echo number_format($row['sample_cnt'],0,'',"'");?></td></tr>
              <tr><th>Botnet C&amp;Cs:</th><td><?php echo number_format($row['ip_cnt'],0,'',"'");?></td></tr>
            </table>

            <h2>Malware Samples</h2>
            <hr>
            <p>The table below documents all malware samples associated with this SSL certificate.</p>
            <table class="table table-sm table-hover table-bordered">
              <thead>
                <tr><th>Timestamp (UTC)</th><th>Malware Sample (MD5 hash)</th><th>VT</th><th>Signature</th><th>Botnet C&amp;C (IP:port)</th></tr>
              </thead>
              <tbody>
              <?php
              $res = mysqli_query($connection, "SELECT timestamp, z.fk_hash, dstip, dstport, vtresult, vtpercent, vtlink, f.signature, malpedia FROM ztbl_reports_tlslog AS z LEFT JOIN tbl_files AS f ON f.fk_hash = z.fk_hash LEFT JOIN tbl_signatures AS s ON s.fk_signature = f.signature WHERE z.fk_sha1 = '".$row['sha1']."' ORDER BY timestamp DESC LIMIT 100");
              while ($ss = $res->fetch_array()) {
                $vtlink = $ss['vtlink'];
                // Ensure that we have a proper VT link
                if($vtlink) {
                  if(!preg_match("/^https/", $vtlink)) {
                    $vtlink = "https://www.virustotal.com$vtlink";
                  }
                }
                $signature = '';
                if($ss['signature']) {
                  if($ss['malpedia']) {
                    $signature = "<span class=\"badge badge-primary\"><a href=\"/ssl-certificates/signature/".$ss['signature']."/\" target=\"_parent\" title=\"Get a full list of SSL certificate associated with ".$ss['signature']."\">".$ss['signature']."</a> <a href=\"".$ss['malpedia']."\" target=\"_blank\" title=\"Lookup ".$ss['signature']." on Malpedia\" class=\"malpedia\"></a></span>";
                  }
                  else {
                    $signature = "<a href=\"/ssl-certificates/signature/".$ss['signature']."/\" target=\"_parent\" title=\"Get a full list of SSL certificate associated with ".$ss['signature']."\"><span class=\"badge badge-primary\">".$ss['signature']."</span></a>";
                  }
                }
                if($vtlink != ''){$vt = "<img src=\"/images/icons/virustotal.png\" width=\"12\" height=\"12\" alt=\"Virustotal results\" /> <a href=\"$vtlink\" target=\"_blank\">".$ss['vtresult']." (".$ss['vtpercent']."%)</a>\n";}else{$vt = "n/a";}
                echo "<tr><td>".$ss['timestamp']."</td><td>".$ss['fk_hash']."</td><td>$vt</td><td>$signature</td><td>".$ss['dstip'].":".$ss['dstport']."</td></tr>\n";
              }
              ?>
              </tbody>
            </table>
            <p># of entries: <strong><?php echo mysqli_num_rows($res); ?></strong> (max: 100)</p>
          <?php
          } else {
            ?>
            <div class="alert alert-primary" role="alert">
              No records found
            </div>
            <?php
          }
        } else {
          ?>
          <div class="alert alert-primary" role="alert">
            No records found
          </div>
          <?php
        }
      } else {
        ?>
        <div class="alert alert-primary" role="alert">
          No records found
        </div>
        <?php
      }
      ?>
    </main>

    <footer class="container">
      <hr>
      <p>&copy; abuse.ch <?php echo date("Y"); ?></p>
    </footer>

    <!-- Bootstrap core JavaScript -->
    <script src="/js/jquery-3.3.1.min.js"></script>
    <script src="/js/bootstrap.bundle.min.js"></script>
  </body>
</html>
