A Login from Outside the Corporate Office


Description

A system has been logged into from a non approved IP space.

Query · python

import ipaddress

# This is only an example network, but you can set it to whatever you'd like
OFFICE_NETWORKS = [
    ipaddress.ip_network("192.168.1.100/32"),
    ipaddress.ip_network("192.168.1.200/32"),
]


def _login_from_non_office_network(host):
    host_ipaddr = ipaddress.IPv4Address(host)

    non_office_logins = []
    for office_network in OFFICE_NETWORKS:
        non_office_logins.append(host_ipaddr in office_network)

    return not any(non_office_logins)


def rule(event):
    if event.get("action") != "added":
        return False

    if "logged_in_users" in event.get("name"):
        # Only pay attention to users and not system-level accounts
        if event.deep_get("columns", "type") != "user":
            return False
    elif "last" in event.get("name"):
        pass
    else:
        # A query we don't care about
        return False

    host_ip = event.deep_get("columns", "host")
    return _login_from_non_office_network(host_ip)


def title(event):
    user = event.deep_get("columns", "user", default=event.deep_get("columns", "username"))

    return (
        f"User [{user if user else '<UNKNOWN_USER>'}"
        f" has logged into production from a non-office network"
    )

Analyst notes

Analyze the host IP, and if possible, update allowlist or fix ACL.

Raw source A Login from Outside the Corporate Office · Panther Python
Esc
Published by panther-labs/panther-analysis ↗, licensed under Apache 2.0 ↗. Reproduced here unmodified.
AnalysisType: rule
Filename: osquery_linux_logins_non_office.py
RuleID: "Osquery.Linux.LoginFromNonOffice"
DisplayName: "A Login from Outside the Corporate Office"
Enabled: false
LogTypes:
  - Osquery.Differential
Tags:
  - Configuration Required
  - Osquery
  - Linux
  - Initial Access:Valid Accounts
Reports:
  MITRE ATT&CK:
    - TA0001:T1078
Severity: High
Description: A system has been logged into from a non approved IP space.
Runbook: Analyze the host IP, and if possible, update allowlist or fix ACL.
Reference: https://attack.mitre.org/techniques/T1078/
SummaryAttributes:
  - name
  - action
  - p_any_ip_addresses
  - p_any_domain_names
Tests:
  - Name: Non-office network login (logged_in_users)
    ExpectedResult: true
    Log:
      {
        "name": "pack/incident_response/logged_in_users",
        "action": "added",
        "columns": { "host": "10.0.3.1", "type": "user", "user": "ubuntu" },
      }
  - Name: Non-office network login (last)
    ExpectedResult: true
    Log:
      {
        "name": "pack-incident_response-last",
        "action": "added",
        "columns":
          {
            "host": "10.0.3.1",
            "type": "8",
            "username": "ubuntu",
            "tty": "ttys008",
            "pid": "648",
            "time": "1587502574",
          },
      }
  - Name: Office network login
    ExpectedResult: false
    Log:
      {
        "name": "pack-logged_in_users",
        "action": "added",
        "columns": { "host": "192.168.1.200", "user": "ubuntu" },
      }


# ------ paired body: osquery_linux_logins_non_office.py ------

import ipaddress

# This is only an example network, but you can set it to whatever you'd like
OFFICE_NETWORKS = [
    ipaddress.ip_network("192.168.1.100/32"),
    ipaddress.ip_network("192.168.1.200/32"),
]


def _login_from_non_office_network(host):
    host_ipaddr = ipaddress.IPv4Address(host)

    non_office_logins = []
    for office_network in OFFICE_NETWORKS:
        non_office_logins.append(host_ipaddr in office_network)

    return not any(non_office_logins)


def rule(event):
    if event.get("action") != "added":
        return False

    if "logged_in_users" in event.get("name"):
        # Only pay attention to users and not system-level accounts
        if event.deep_get("columns", "type") != "user":
            return False
    elif "last" in event.get("name"):
        pass
    else:
        # A query we don't care about
        return False

    host_ip = event.deep_get("columns", "host")
    return _login_from_non_office_network(host_ip)


def title(event):
    user = event.deep_get("columns", "user", default=event.deep_get("columns", "username"))

    return (
        f"User [{user if user else '<UNKNOWN_USER>'}"
        f" has logged into production from a non-office network"
    )

Detection rules belong to the projects that publish them and remain under their own licenses. This site indexes and links to them; it claims no rights in them.