from Spitzer import command
from Spitzer.scanner import nmapper
from Spitzer.result.result import add

category = ['safe']

def exploit(host,nmap, port):
    if port == 111:
        __nfs(host, nmap)
    elif port == 445: #or port == 139
        __smb(host)
    

def __smb(host):
    smb_result = command.run(['smbmap', '-H ' + host], capture_output=True)
    print(smb_result)
    if 'STATUS_ACCESS_DENIED' in smb_result or len(smb_result.splitlines()) <= 1:
        smb_result = command.run(['smbmap', '-u anonymous', '-H ' + host], capture_output=True)

    if 'STATUS_ACCESS_DENIED' in smb_result or 'STATUS_LOGON_FAILURE' in smb_result or len(smb_result.splitlines()) <= 1:
        return

    print('\n smb open acces:')

    result  = ''
    for line in iter(smb_result.splitlines()):
            if not line.startswith('[+]'):
                result += line + '\n'
                print(line)
    add(host, 'open smb share', result)
        
        
def __nfs(host, nmap):
    if 'script' not in nmap: 
        return

    print(nmap['script']['nfs-showmount']) #TO DO fix metasploitable so this can be tested and tweaked