2014-09-09 05:19:59 +00:00
|
|
|
// Copyright 2004-present Facebook. All Rights Reserved.
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <fstream>
|
|
|
|
#include <streambuf>
|
2014-09-09 17:56:48 +00:00
|
|
|
#include <sstream>
|
2014-09-09 05:19:59 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
2014-09-09 17:56:48 +00:00
|
|
|
#include <unistd.h>
|
2014-09-09 05:19:59 +00:00
|
|
|
#include <proc/readproc.h>
|
|
|
|
|
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
|
|
|
|
#include "osquery/core.h"
|
|
|
|
#include "osquery/database.h"
|
2014-09-09 17:56:48 +00:00
|
|
|
#include "osquery/filesystem.h"
|
2014-09-09 05:19:59 +00:00
|
|
|
|
|
|
|
using namespace osquery::core;
|
|
|
|
using namespace osquery::db;
|
|
|
|
|
|
|
|
namespace osquery {
|
|
|
|
namespace tables {
|
|
|
|
|
|
|
|
#define PROC_SELECTS \
|
|
|
|
PROC_FILLCOM | PROC_EDITCMDLCVT | PROC_FILLMEM | PROC_FILLSTATUS | \
|
|
|
|
PROC_FILLSTAT
|
|
|
|
|
|
|
|
std::string proc_name(const proc_t* proc_info) {
|
2014-09-09 23:14:54 +00:00
|
|
|
char cmd[17]; // cmd is a 16 char buffer
|
2014-09-09 05:19:59 +00:00
|
|
|
|
2014-09-09 17:56:48 +00:00
|
|
|
memset(cmd, 0, 17);
|
|
|
|
memcpy(cmd, proc_info->cmd, 16);
|
2014-09-09 05:19:59 +00:00
|
|
|
return std::string(cmd);
|
|
|
|
}
|
|
|
|
|
2014-09-09 17:56:48 +00:00
|
|
|
std::string proc_attr(const std::string& attr, const proc_t* proc_info) {
|
|
|
|
std::stringstream filename;
|
2014-09-09 05:19:59 +00:00
|
|
|
|
2014-09-09 17:56:48 +00:00
|
|
|
filename << "/proc/" << proc_info->tid << "/" << attr;
|
|
|
|
return filename.str();
|
|
|
|
}
|
2014-09-09 05:19:59 +00:00
|
|
|
|
2014-09-09 17:56:48 +00:00
|
|
|
std::string proc_cmdline(const proc_t* proc_info) {
|
|
|
|
std::string attr;
|
|
|
|
std::string result;
|
2014-09-09 05:19:59 +00:00
|
|
|
|
2014-09-09 17:56:48 +00:00
|
|
|
attr = proc_attr("cmdline", proc_info);
|
|
|
|
std::ifstream fd(attr, std::ios::in | std::ios::binary);
|
2014-09-09 05:19:59 +00:00
|
|
|
if (fd) {
|
2014-09-09 17:56:48 +00:00
|
|
|
result = std::string(std::istreambuf_iterator<char>(fd),
|
|
|
|
std::istreambuf_iterator<char>());
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string proc_link(const proc_t* proc_info) {
|
|
|
|
std::string attr;
|
|
|
|
std::string result;
|
|
|
|
char* link_path;
|
|
|
|
long path_max;
|
|
|
|
int bytes;
|
|
|
|
|
|
|
|
// The exe is a symlink to the binary on-disk.
|
|
|
|
attr = proc_attr("exe", proc_info);
|
|
|
|
path_max = pathconf(attr.c_str(), _PC_PATH_MAX);
|
|
|
|
link_path = (char*)malloc(path_max);
|
|
|
|
|
|
|
|
memset(link_path, 0, path_max);
|
|
|
|
bytes = readlink(attr.c_str(), link_path, path_max);
|
|
|
|
if (bytes >= 0) {
|
|
|
|
result = std::string(link_path);
|
2014-09-09 05:19:59 +00:00
|
|
|
}
|
|
|
|
|
2014-09-09 17:56:48 +00:00
|
|
|
free(link_path);
|
|
|
|
return result;
|
2014-09-09 05:19:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QueryData genProcesses() {
|
|
|
|
QueryData results;
|
|
|
|
|
|
|
|
proc_t* proc_info;
|
|
|
|
PROCTAB* proc = openproc(PROC_SELECTS);
|
|
|
|
|
|
|
|
// Populate proc struc for each process.
|
|
|
|
while (proc_info = readproc(proc, NULL)) {
|
|
|
|
Row r;
|
|
|
|
|
|
|
|
r["pid"] = boost::lexical_cast<std::string>(proc_info->tid);
|
|
|
|
r["name"] = proc_name(proc_info);
|
2014-09-09 17:56:48 +00:00
|
|
|
r["cmdline"] = proc_cmdline(proc_info);
|
|
|
|
r["path"] = proc_link(proc_info);
|
|
|
|
r["on_disk"] = osquery::fs::pathExists(r["path"]).toString();
|
|
|
|
|
2014-09-09 05:19:59 +00:00
|
|
|
r["resident_size"] = boost::lexical_cast<std::string>(proc_info->vm_rss);
|
|
|
|
r["phys_footprint"] = boost::lexical_cast<std::string>(proc_info->vm_size);
|
|
|
|
r["user_time"] = boost::lexical_cast<std::string>(proc_info->utime);
|
|
|
|
r["system_time"] = boost::lexical_cast<std::string>(proc_info->stime);
|
|
|
|
r["start_time"] = boost::lexical_cast<std::string>(proc_info->start_time);
|
|
|
|
r["parent"] = boost::lexical_cast<std::string>(proc_info->ppid);
|
|
|
|
|
|
|
|
results.push_back(r);
|
|
|
|
freeproc(proc_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
closeproc(proc);
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|