2016-04-15 21:01:16 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
# Copyright (c) 2014-present, 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.
|
|
|
|
|
2016-05-31 16:10:53 +00:00
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import division
|
|
|
|
from __future__ import print_function
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2016-04-15 21:01:16 +00:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
import argparse
|
|
|
|
import platform
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
ORACLE_RELEASE = "/etc/oracle-release"
|
|
|
|
SYSTEM_RELEASE = "/etc/system-release"
|
2016-11-10 16:14:26 +00:00
|
|
|
LSB_RELEASE = "/etc/lsb-release"
|
2016-04-15 21:01:16 +00:00
|
|
|
DEBIAN_VERSION = "/etc/debian_version"
|
2016-11-22 22:55:30 +00:00
|
|
|
GENTOO_RELEASE = "/etc/gentoo-release"
|
2016-04-15 21:01:16 +00:00
|
|
|
|
|
|
|
def _platform():
|
|
|
|
osType, _, _, _, _, _ = platform.uname()
|
2016-05-31 16:10:53 +00:00
|
|
|
|
2016-04-15 21:01:16 +00:00
|
|
|
if osType == "Windows":
|
|
|
|
return ("windows", "windows")
|
|
|
|
elif osType == "Linux":
|
|
|
|
if os.path.exists(ORACLE_RELEASE):
|
|
|
|
return ("redhat", "oracle")
|
2016-05-31 16:10:53 +00:00
|
|
|
|
2016-04-15 21:01:16 +00:00
|
|
|
if os.path.exists(SYSTEM_RELEASE):
|
|
|
|
with open(SYSTEM_RELEASE, "r") as fd:
|
|
|
|
fileContents = fd.read()
|
2016-05-31 16:10:53 +00:00
|
|
|
|
2016-04-15 21:01:16 +00:00
|
|
|
if fileContents.find("CentOS") != -1:
|
|
|
|
return ("redhat", "centos")
|
2016-05-31 16:10:53 +00:00
|
|
|
|
2016-06-01 23:59:55 +00:00
|
|
|
if fileContents.find("Scientific Linux") != -1:
|
|
|
|
return ("redhat", "scientific")
|
|
|
|
|
2016-04-15 21:01:16 +00:00
|
|
|
if fileContents.find("Red Hat Enterprise") != -1:
|
|
|
|
return ("redhat", "rhel")
|
2016-05-31 16:10:53 +00:00
|
|
|
|
2016-04-15 21:01:16 +00:00
|
|
|
if fileContents.find("Amazon Linux") != -1:
|
|
|
|
return ("redhat", "amazon")
|
2016-05-31 16:10:53 +00:00
|
|
|
|
2016-04-15 21:01:16 +00:00
|
|
|
if fileContents.find("Fedora") != -1:
|
|
|
|
return ("redhat", "fedora")
|
2016-05-31 16:10:53 +00:00
|
|
|
|
2016-04-15 21:01:16 +00:00
|
|
|
if os.path.exists(LSB_RELEASE):
|
|
|
|
with open(LSB_RELEASE, "r") as fd:
|
|
|
|
fileContents = fd.read()
|
2016-05-31 16:10:53 +00:00
|
|
|
|
2016-04-15 21:01:16 +00:00
|
|
|
if fileContents.find("DISTRIB_ID=Ubuntu") != -1:
|
|
|
|
return ("debian", "ubuntu")
|
2016-05-31 16:10:53 +00:00
|
|
|
|
2016-07-06 22:02:58 +00:00
|
|
|
if fileContents.find("DISTRIB_ID=Arch") != -1:
|
|
|
|
return ("arch", "arch")
|
|
|
|
|
2016-08-03 19:57:09 +00:00
|
|
|
if fileContents.find("DISTRIB_ID=ManjaroLinux") != -1:
|
|
|
|
return ("arch", "manjaro")
|
|
|
|
|
2016-04-15 21:01:16 +00:00
|
|
|
if os.path.exists(DEBIAN_VERSION):
|
|
|
|
return ("debian", "debian")
|
2016-11-22 22:55:30 +00:00
|
|
|
|
|
|
|
if os.path.exists(GENTOO_RELEASE):
|
|
|
|
return ("gentoo", "gentoo")
|
2016-04-15 21:01:16 +00:00
|
|
|
else:
|
|
|
|
return (None, osType.lower())
|
2016-05-31 16:10:53 +00:00
|
|
|
|
2016-04-15 21:01:16 +00:00
|
|
|
def _distro(osType):
|
|
|
|
def getRedhatDistroVersion(pattern):
|
|
|
|
with open(SYSTEM_RELEASE, "r") as fd:
|
|
|
|
contents = fd.read()
|
2016-05-31 16:10:53 +00:00
|
|
|
|
2016-04-15 21:01:16 +00:00
|
|
|
result = re.findall(pattern, contents)
|
|
|
|
if result and len(result) == 1:
|
|
|
|
return result[0].replace("release ", osType)
|
|
|
|
return None
|
2016-05-31 16:10:53 +00:00
|
|
|
|
2016-04-15 21:01:16 +00:00
|
|
|
def commandOutput(cmd):
|
|
|
|
try:
|
|
|
|
output = subprocess.check_output(cmd)
|
|
|
|
return output
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
return None
|
2016-06-07 19:52:51 +00:00
|
|
|
except OSError:
|
|
|
|
return None
|
2016-04-15 21:01:16 +00:00
|
|
|
except WindowsError:
|
|
|
|
return None
|
2016-05-31 16:10:53 +00:00
|
|
|
|
2016-04-15 21:01:16 +00:00
|
|
|
_, _, osVersion, _, _, _ = platform.uname()
|
2016-05-31 16:10:53 +00:00
|
|
|
|
2016-04-15 21:01:16 +00:00
|
|
|
if osType == "oracle":
|
|
|
|
result = getRedhatDistroVersion(r'release [5-7]')
|
|
|
|
if result is not None:
|
|
|
|
return result
|
2016-06-01 23:59:55 +00:00
|
|
|
elif osType in ["centos", "scientific", "rhel"]:
|
2016-04-15 21:01:16 +00:00
|
|
|
result = getRedhatDistroVersion(r'release [6-7]')
|
|
|
|
if result is not None:
|
|
|
|
return result
|
|
|
|
elif osType == "amazon":
|
|
|
|
result = getRedhatDistroVersion(r'release 20[12][0-9]\.[0-9][0-9]')
|
|
|
|
if result is not None:
|
|
|
|
return result
|
|
|
|
elif osType == "ubuntu":
|
|
|
|
with open(LSB_RELEASE, "r") as fd:
|
|
|
|
contents = fd.read()
|
|
|
|
results = re.findall(r'DISTRIB_CODENAME=(.*)', contents)
|
|
|
|
if len(results) == 1:
|
|
|
|
return results[0]
|
|
|
|
elif osType == "darwin":
|
|
|
|
rawResult = commandOutput(["sw_vers", "-productVersion"])
|
|
|
|
if rawResult is not None:
|
|
|
|
results = re.findall(r'[0-9]+\.[0-9]+', rawResult)
|
|
|
|
if len(results) == 1:
|
|
|
|
return results[0]
|
|
|
|
elif osType == "fedora":
|
|
|
|
with open(SYSTEM_RELEASE, "r") as fd:
|
|
|
|
contents = fd.read()
|
|
|
|
results = contents.split()
|
|
|
|
if len(results) > 2:
|
|
|
|
return results[2]
|
2016-07-06 22:02:58 +00:00
|
|
|
elif osType == "arch":
|
|
|
|
with open("/etc/arch-release", "r") as fd:
|
|
|
|
contents = fd.read()
|
|
|
|
results = contents.split()
|
|
|
|
if len(results) > 2:
|
|
|
|
return results[2]
|
2016-08-03 19:57:09 +00:00
|
|
|
elif osType == "manjaro":
|
|
|
|
with open(LSB_RELEASE, "r") as fd:
|
|
|
|
contents = fd.read()
|
|
|
|
results = re.findall(r'DISTRIB_CODENAME=(.*)', contents)
|
|
|
|
if len(results) == 1:
|
|
|
|
return results[0]
|
2016-04-15 21:01:16 +00:00
|
|
|
elif osType == "debian":
|
|
|
|
result = commandOutput(["lsb_release", "-cs"])
|
|
|
|
if result is not None:
|
|
|
|
return result
|
|
|
|
elif osType == "freebsd":
|
|
|
|
rawResult = commandOutput(["uname", "-r"])
|
|
|
|
results = rawResult.split("-")
|
|
|
|
if len(results) > 0:
|
|
|
|
return results[0]
|
2016-11-22 22:55:30 +00:00
|
|
|
elif osType == "gentoo":
|
|
|
|
with open(GENTOO_RELEASE, "r") as fd:
|
|
|
|
contents = fd.read()
|
|
|
|
results = contents.split()
|
|
|
|
if len(results) > 0:
|
|
|
|
return results[len(results) -1]
|
2016-04-15 21:01:16 +00:00
|
|
|
elif osType == "windows":
|
|
|
|
return "windows%s" % osVersion
|
2016-05-31 16:10:53 +00:00
|
|
|
|
2016-04-15 21:01:16 +00:00
|
|
|
return "unknown_version"
|
|
|
|
|
|
|
|
def platformAction():
|
|
|
|
family, osType = _platform()
|
2016-05-31 16:10:53 +00:00
|
|
|
print(osType)
|
|
|
|
|
2016-04-15 21:01:16 +00:00
|
|
|
def distroAction():
|
|
|
|
family, osType = _platform()
|
2016-05-31 16:10:53 +00:00
|
|
|
print(_distro(osType))
|
|
|
|
|
2016-04-15 21:01:16 +00:00
|
|
|
def familyAction():
|
|
|
|
family, osType = _platform()
|
|
|
|
if family:
|
2016-05-31 16:10:53 +00:00
|
|
|
print(family)
|
2016-04-15 21:01:16 +00:00
|
|
|
|
|
|
|
def defaultAction():
|
|
|
|
family, osType = _platform()
|
|
|
|
distro = _distro(osType)
|
2016-05-31 16:10:53 +00:00
|
|
|
print("%s;%s" % (osType, distro))
|
|
|
|
|
2016-04-15 21:01:16 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = argparse.ArgumentParser(description="Platform detection script for osquery")
|
|
|
|
parser.add_argument("--platform", action="store_true", help="Outputs the detected platform")
|
|
|
|
parser.add_argument("--distro", action="store_true", help="Outputs the detected distribution")
|
|
|
|
parser.add_argument("--family", action="store_true", help="Outputs the detected family")
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
2016-05-31 16:10:53 +00:00
|
|
|
|
2016-04-15 21:01:16 +00:00
|
|
|
if args.platform and \
|
|
|
|
not args.distro and \
|
|
|
|
not args.family:
|
|
|
|
platformAction()
|
|
|
|
elif not args.platform and \
|
|
|
|
args.distro and \
|
|
|
|
not args.family:
|
|
|
|
distroAction()
|
|
|
|
elif not args.platform and \
|
|
|
|
not args.distro and \
|
|
|
|
args.family:
|
|
|
|
familyAction()
|
|
|
|
else:
|
|
|
|
defaultAction()
|
2016-05-31 16:10:53 +00:00
|
|
|
|
2016-04-15 21:01:16 +00:00
|
|
|
sys.exit(0)
|