2015-10-29 19:01:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <osquery/core.h>
|
|
|
|
#include <osquery/tables.h>
|
|
|
|
#include <osquery/filesystem.h>
|
2015-11-24 20:29:03 +00:00
|
|
|
|
|
|
|
#include "osquery/tables/system/system_utils.h"
|
2015-10-29 19:01:42 +00:00
|
|
|
|
|
|
|
namespace osquery {
|
|
|
|
namespace tables {
|
|
|
|
|
2015-11-24 20:29:03 +00:00
|
|
|
const std::vector<std::string> kSSHAuthorizedkeys = {".ssh/authorized_keys",
|
|
|
|
".ssh/authorized_keys2"};
|
2015-10-29 19:01:42 +00:00
|
|
|
|
2015-11-24 20:29:03 +00:00
|
|
|
void genSSHkeysForUser(const std::string& uid,
|
|
|
|
const std::string& directory,
|
|
|
|
QueryData& results) {
|
2015-10-29 19:01:42 +00:00
|
|
|
for (const auto& kfile : kSSHAuthorizedkeys) {
|
|
|
|
boost::filesystem::path keys_file = directory;
|
|
|
|
keys_file /= kfile;
|
|
|
|
|
|
|
|
std::string keys_content;
|
|
|
|
if (!readFile(keys_file, keys_content).ok()) {
|
|
|
|
// Cannot read a specific keys file.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
//Protocol 1 public key consist of: options, bits, exponent, modulus, comment
|
|
|
|
//Protocol 2 public key consist of: options, keytype, base64-encoded key, comment.
|
|
|
|
for (const auto& line : split(keys_content, "\n")) {
|
|
|
|
if (!line.empty() && line[0] != '#') {
|
|
|
|
Row r;
|
2015-11-24 20:29:03 +00:00
|
|
|
r["uid"] = uid;
|
2015-10-29 19:01:42 +00:00
|
|
|
r["key"] = line;
|
|
|
|
r["key_file"] = keys_file.string();
|
|
|
|
results.push_back(r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QueryData getAuthorizedKeys(QueryContext& context) {
|
|
|
|
QueryData results;
|
|
|
|
|
|
|
|
// Iterate over each user
|
2015-11-24 20:29:03 +00:00
|
|
|
QueryData users = usersFromContext(context);
|
2015-10-29 19:01:42 +00:00
|
|
|
for (const auto& row : users) {
|
2015-11-24 20:29:03 +00:00
|
|
|
if (row.count("uid") > 0 && row.count("directory") > 0) {
|
|
|
|
genSSHkeysForUser(row.at("uid"), row.at("directory"), results);
|
2015-10-29 19:01:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|