2014-08-02 03:46:22 +00:00
|
|
|
// Copyright 2004-present Facebook. All Rights Reserved.
|
|
|
|
|
2014-08-06 01:10:18 +00:00
|
|
|
#include "osquery/tables/networking/etc_hosts.h"
|
2014-08-02 03:46:22 +00:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
|
2014-08-06 22:55:46 +00:00
|
|
|
#include <boost/algorithm/string/join.hpp>
|
2014-08-04 21:12:06 +00:00
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
|
|
|
|
|
|
|
#include <glog/logging.h>
|
|
|
|
|
|
|
|
#include "osquery/core.h"
|
|
|
|
#include "osquery/filesystem.h"
|
|
|
|
|
|
|
|
using namespace osquery::core;
|
2014-08-02 03:46:22 +00:00
|
|
|
using namespace osquery::db;
|
2014-08-04 21:12:06 +00:00
|
|
|
using namespace osquery::fs;
|
2014-08-02 03:46:22 +00:00
|
|
|
|
|
|
|
namespace osquery { namespace tables {
|
|
|
|
|
|
|
|
QueryData genEtcHosts() {
|
2014-08-04 21:12:06 +00:00
|
|
|
std::string content;
|
|
|
|
auto s = readFile("/etc/hosts", content);
|
|
|
|
if (s.ok()) {
|
|
|
|
return parseEtcHostsContent(content);
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "Error reading /etc/hosts: " << s.toString();
|
|
|
|
return {};
|
|
|
|
}
|
2014-08-02 03:46:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QueryData parseEtcHostsContent(const std::string& content) {
|
|
|
|
QueryData results;
|
2014-08-04 21:12:06 +00:00
|
|
|
|
2014-08-04 23:08:49 +00:00
|
|
|
for (const auto& i : split(content, "\n")) {
|
|
|
|
auto line = split(i);
|
2014-08-04 21:12:06 +00:00
|
|
|
if (line.size() == 0 || boost::starts_with(line[0], "#")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Row r;
|
|
|
|
r["address"] = line[0];
|
2014-08-04 23:08:49 +00:00
|
|
|
if (line.size() > 1) {
|
|
|
|
std::vector<std::string> hostnames;
|
|
|
|
for (int i = 1; i < line.size(); ++i) {
|
|
|
|
hostnames.push_back(line[i]);
|
|
|
|
}
|
2014-08-06 22:55:46 +00:00
|
|
|
r["hostnames"] = boost::algorithm::join(hostnames, " ");
|
2014-08-04 23:08:49 +00:00
|
|
|
}
|
2014-08-04 21:12:06 +00:00
|
|
|
results.push_back(r);
|
|
|
|
}
|
2014-08-02 03:46:22 +00:00
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
}}
|