__author__ = 'ASUS'

import os,pefile,sys,shutil
from creatAsm.getFolderName import getFilename
from collections import defaultdict
def get_header_api(exePath):
    api_list1 = []
    exeList = getFilename(exePath)
    root_path = 'E:\\malware_detector\\web\\creatAsm'
    for exeName in exeList:
        header_list2 = []
        #root_path = root_path + '\\exeFile\\2018-9-5'
        root_path=exePath
        each_file = os.path.join(root_path, exeName)
        pe = pefile.PE(each_file)
        header_list2.append(exeName)
        header_list2.append(str(pe.DOS_HEADER.e_magic))
        header_list2.append(str(pe.DOS_HEADER.e_cblp))
        header_list2.append(str(pe.DOS_HEADER.e_cp))
        header_list2.append(str(pe.DOS_HEADER.e_crlc))
        header_list2.append(str(pe.DOS_HEADER.e_cparhdr))
        header_list2.append(str(pe.DOS_HEADER.e_minalloc))
        header_list2.append(str(pe.DOS_HEADER.e_maxalloc))
        header_list2.append(str(pe.DOS_HEADER.e_ss))
        header_list2.append(str(pe.DOS_HEADER.e_sp))
        header_list2.append(str(pe.DOS_HEADER.e_csum))
        header_list2.append(str(pe.DOS_HEADER.e_ip))
        header_list2.append(str(pe.DOS_HEADER.e_cs))
        header_list2.append(str(pe.DOS_HEADER.e_lfarlc))
        header_list2.append(str(pe.DOS_HEADER.e_ovno))
        # if not pe.DOS_HEADER.e_res:
        #     header_list2.append("null")
        # else:
        #     header_list2.append(str(pe.DOS_HEADER.e_res,encoding='utf-8'))
        # header_list2.append(str(pe.DOS_HEADER.e_res))
        header_list2.append(str(pe.DOS_HEADER.e_oemid))
        header_list2.append(str(pe.DOS_HEADER.e_oeminfo))
        # if not pe.DOS_HEADER.e_res2:
        #     header_list2.append("null")
        # else:
        #     header_list2.append(str(pe.DOS_HEADER.e_res2,encoding='utf-8'))
        # header_list2.append(str(pe.DOS_HEADER.e_res2))
        header_list2.append(str(pe.DOS_HEADER.e_lfanew))
        header_list2.append(str(pe.NT_HEADERS.Signature))
        header_list2.append(str(pe.FILE_HEADER.Machine))
        header_list2.append(str(pe.FILE_HEADER.NumberOfSections))
        header_list2.append(str(pe.FILE_HEADER.TimeDateStamp))
        header_list2.append(str(pe.FILE_HEADER.PointerToSymbolTable))
        header_list2.append(str(pe.FILE_HEADER.NumberOfSymbols))
        header_list2.append(str(pe.FILE_HEADER.SizeOfOptionalHeader))
        header_list2.append(str(pe.FILE_HEADER.Characteristics))
        header_list2.append(str(pe.OPTIONAL_HEADER.Magic))
        header_list2.append(str(pe.OPTIONAL_HEADER.MajorLinkerVersion))
        header_list2.append(str(pe.OPTIONAL_HEADER.MinorLinkerVersion))
        header_list2.append(str(pe.OPTIONAL_HEADER.SizeOfCode))
        header_list2.append(str(pe.OPTIONAL_HEADER.SizeOfInitializedData))
        header_list2.append(str(pe.OPTIONAL_HEADER.SizeOfUninitializedData))
        header_list2.append(str(pe.OPTIONAL_HEADER.AddressOfEntryPoint))
        header_list2.append(str(pe.OPTIONAL_HEADER.BaseOfCode))
        header_list2.append(str(pe.OPTIONAL_HEADER.BaseOfData))
        header_list2.append(str(pe.OPTIONAL_HEADER.ImageBase))
        header_list2.append(str(pe.OPTIONAL_HEADER.SectionAlignment))
        header_list2.append(str(pe.OPTIONAL_HEADER.FileAlignment))
        header_list2.append(str(pe.OPTIONAL_HEADER.MajorOperatingSystemVersion))
        header_list2.append(str(pe.OPTIONAL_HEADER.MinorOperatingSystemVersion))
        header_list2.append(str(pe.OPTIONAL_HEADER.MajorImageVersion))
        header_list2.append(str(pe.OPTIONAL_HEADER.MinorImageVersion))
        header_list2.append(str(pe.OPTIONAL_HEADER.MajorSubsystemVersion))
        header_list2.append(str(pe.OPTIONAL_HEADER.MinorSubsystemVersion))
        # header_list2.append(str(pe.OPTIONAL_HEADER.Win32VersionValue))
        header_list2.append(str(pe.OPTIONAL_HEADER.SizeOfImage))
        header_list2.append(str(pe.OPTIONAL_HEADER.SizeOfHeaders))
        header_list2.append(str(pe.OPTIONAL_HEADER.CheckSum))
        header_list2.append(str(pe.OPTIONAL_HEADER.Subsystem))
        header_list2.append(str(pe.OPTIONAL_HEADER.DllCharacteristics))
        header_list2.append(str(pe.OPTIONAL_HEADER.SizeOfStackReserve))
        header_list2.append(str(pe.OPTIONAL_HEADER.SizeOfStackCommit))
        header_list2.append(str(pe.OPTIONAL_HEADER.SizeOfHeapReserve))
        header_list2.append(str(pe.OPTIONAL_HEADER.SizeOfHeapCommit))
        header_list2.append(str(pe.OPTIONAL_HEADER.LoaderFlags))
        header_list2.append(str(pe.OPTIONAL_HEADER.NumberOfRvaAndSizes))


        try:
            for importeddll in pe.DIRECTORY_ENTRY_IMPORT:
                api_list2 = []
                # print(importeddll.dll)
                api_list2.append(exeName)
                if importeddll.dll:
                    api_list2.append(str(importeddll.dll,encoding='utf=8'))
                else:
                    api_list2.append(str(importeddll.dll))
                for importedapi in importeddll.imports:
                    # print(importedapi.name)
                    if importedapi.name:
                        api_list2.append(str(importedapi.name, encoding='utf=8'))
                    else:
                        api_list2.append(str(importedapi.name))
                    #api_list2.append(str(importedapi.name))
                    # print pe.DIRECTORY_ENTRY_IMPORT[0].imports[0].name
                    # j = j + 1
                # print(j)
                # apiNum = j
                api_list1.append(api_list2)
        except Exception as e:
            print(e)
            continue
        header_list3=[]
        api_list3=[]
        # for item in header_list2:
        #     header_list3.append(item.decode('utf-8'))
        # for item in api_list2:
        #     api_list3.append(item.decode('utf-8'))
        return (header_list2,api_list1)
