[ SEA-GHOST MINI SHELL]

Path : /proc/2/cwd/proc/2/root/var/lib/zabbix/
FILE UPLOADER :
Current File : //proc/2/cwd/proc/2/root/var/lib/zabbix/ldap_ms_sync.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import ldap
import smtplib
from pyzabbix import ZabbixAPI

USERNAME = 'monitoring-ldap-read'
PASSWORD = 'asd@#4sd3S#s3S!d56'
ZBX_SERVER = 'https://zabbix.au.syrahost.com'
LDAP_SERVER = "ldap://dc01-au.internal.dreamscapenetworks.com"
ZBX_GROUPS = [64, 65, 66, 67, 68, 69, 70, 78, 79, 80, 98, 99]
SKIP_HOSTS = [
        'broker02-au.internal.dreamscapenetworks.com',
        'dag03.internal.dreamscapenetworks.com',
        'datacore-nfs.internal.dreamscapenetworks.com',
        'datacorefe01-au.internal.dreamscapenetworks.com',
        'datacorefe02-au.internal.dreamscapenetworks.com',
        'hvbroker01-au.internal.dreamscapenetworks.com',
        'hvbroker01-uk.internal.dreamscapenetworks.com',
        'hvcluster01-uk.internal.dreamscapenetworks.com',
        'nfscluster01-au.internal.dreamscapenetworks.com',
        'mysqlcust01-uk.internal.dreamscapenetworks.com',
        'nfsha01-au.internal.dreamscapenetworks.com',
        'mysqlcust01-au.internal.dreamscapenetworks.com'
        ]
SENDER = 'zabbix@dreamscapenetworks.com'
TO = 'mikhail.p@dreamscapenetworks.com'
DEBUG = False

def get_server_list(server, user, pw, base, list):
    Scope = ldap.SCOPE_SUBTREE
    Filter = 'objectclass=Computer'
    Attrs = ['dNSHostName']
    Base = base
    l = ldap.initialize(server)
    l.protocol_version = 3
    l.set_option(ldap.OPT_REFERRALS, 0)
    l.simple_bind_s(user, pw)
    r = l.search_s(Base, Scope, Filter, Attrs)
    l.unbind()    
    for item in r:
        server = item[1]['dNSHostName'][0].lower()
        list.append(server)
    return list

Base = "OU=Microsoft,OU=Computers,OU=Share objects,OU=_Dreamscapenetworks,DC=internal,DC=dreamscapenetworks,DC=com"
ldap_hosts = []
ldap_hosts = get_server_list(LDAP_SERVER, USERNAME, PASSWORD, Base, ldap_hosts)

Base = "OU=Domain Controllers,DC=internal,DC=dreamscapenetworks,DC=com"
ldap_hosts = get_server_list(LDAP_SERVER, USERNAME, PASSWORD, Base, ldap_hosts)

zapi = ZabbixAPI(ZBX_SERVER)    
zapi.login(USERNAME, PASSWORD)

zabbix_hosts = []

hosts = zapi.host.get(groupids=ZBX_GROUPS)

for host in hosts:
    hostname = host['host'].lower()
    if 'internal' in hostname:
        zabbix_hosts.append(hostname)

ldap_hosts = list(set(ldap_hosts) - set(SKIP_HOSTS))
count_ad = len(ldap_hosts)
count_zbx = len(zabbix_hosts)
count_ok = len(set(ldap_hosts).intersection(zabbix_hosts))
only_ad = sorted(list(set(ldap_hosts) - set(zabbix_hosts)))
only_zbx = sorted(list(set(zabbix_hosts) - set(ldap_hosts)))

if DEBUG:
    print "Total servers in AD: {}".format(count_ad)
    print "Total servers in Zabbix: {}".format(count_zbx)
    print "{} servers are present in both lists".format(count_ok)
    print "These hosts are NOT present in Zabbix:"
    for host in only_ad:
        print "\t", host
    print "These hosts are NOT present in AD:"
    for host in only_zbx:
        print "\t", host

# Send message if any host is missing in Zabbix
if only_ad:
    msg = 'From: %s\r\nTo: %s\r\nContent-Type: text/html; charset="utf-8"\r\n' % (SENDER, TO)
    msg += 'Subject: Zabbix missing Microsoft hosts\r\n\r\n'
    msg += 'List of missing hosts in Zabbix: {0}'.format(only_ad)
    smtpObj = smtplib.SMTP('localhost')
    smtpObj.sendmail(SENDER, TO, msg)

    # Add new host
    for host in only_ad:
        newhost = zapi.host.create(
            host=host,
            groups=[{"groupid": 80}],
            interfaces=[{"type": 1, "main": 1, "useip": 0, "ip": "", "dns": host, "port": "10050"}],
            templates=[{"templateid": "11116"}],
            proxy_hostid=10105
        )


SEA-GHOST - SHELL CODING BY SEA-GHOST