Exemple #1
0
    def call_api(self):
        outlook = Dispatch("Outlook.Application")
        mapi = outlook.GetNamespace("MAPI")
        Accounts = mapi.Folders

        #解析邮件内容
        if self.mail_title != "":
            try:
                for Account in Accounts:
                    if Account.Name == self.account_name:
                        Folders = Account.Folders  #读取该账户下的文件夹列表
                        for Folder in Folders:  #第一层目录
                            if len(self.folder_name[0]) == 0:  #输入文件夹为空
                                if Folder.Name == "收件箱":
                                    mail_info = self.save_attachments(
                                        Folder)  #调用邮件解析和附件下载函数
                            elif len(self.folder_name) == 1:
                                if Folder.Name == self.folder_name[0]:
                                    mail_info = self.save_attachments(
                                        Folder)  #调用邮件解析和附件下载函数
                            else:
                                if Folder.Name == self.folder_name[0]:
                                    for i in range(1, len(self.folder_name)):
                                        for Folder2 in Folder.Folders:
                                            if Folder2.Name == self.folder_name[
                                                    i]:
                                                Folder = Folder2
                                    mail_info = self.save_attachments(
                                        Folder)  #调用邮件解析和附件下载函数
                return (mail_info)
            except:
                traceback.print_exc()
                sys.exit()
        else:
            return ("邮件title不能为空!~")
def read_outlook_mailbox():
    """连接Outlook邮箱,读取收件箱内的邮件内容"""
    # 使用MAPI协议连接Outlook
    account = Dispatch('Outlook.Application').GetNamespace('MAPI')

    # 获取收件箱所在位置
    inbox = account.GetDefaultFolder(6)  # 数字6代表收件箱
    # 获取收件箱下的所有邮件
    mails = inbox.Items
    mails.Sort('[ReceivedTime]', True)  # 邮件按时间排序

    # 读取收件箱内前3封邮件的所有信息(下标从1开始)
    for index in range(1, 4):
        print('正在读取第[{}]封邮件...'.format(index))
        mail = mails.Item(index)
        print('接收时间:{}'.format(str(mail.ReceivedTime)[:-6]))
        print('发件人:{}'.format(mail.SenderName))
        print('收件人:{}'.format(mail.To))
        print('抄送人:{}'.format(mail.CC))
        print('主题:{}'.format(mail.Subject))
        print('邮件正文内容:{}'.format(mail.Body))
        print('邮件附件数量:{}'.format(mail.Attachments.Count))
        print('邮件MessageID:{}'.format(mail.EntryID))
        print('会话主题:{}'.format(mail.ConversationTopic))
        print('会话ID:{}'.format(mail.ConversationID))
        print('会话记录相对位置:{}'.format(mail.ConversationIndex))

        # 保存邮件中的附件,如果没有附件不会执行也不会产生异常
        attachment = mail.Attachments
        for each in attachment:
            save_attachment_path = os.getcwd()  # 保存附件到当前路径
            each.SaveAsFile(r'{}\{}'.format(save_attachment_path,
                                            each.FileName))
            print('附件({})保存完毕'.format(each.FileName))
Exemple #3
0
def send_email(to, subject, body):
    '''
    docstring
    '''
    try:
        outlook = Dispatch("Outlook.Application")
        mailitem = outlook.CreateItem(0)
        tostr = ''
        if type(to) is list:
            tostr = ';'.join(to).strip(';').strip().strip(';')
        else:
            tostr = to.strip()
        mailitem.To = tostr
        mailitem.Subject = subject
        mailitem.Body = body
        mailitem.Send()
    except Exception as e:
        print(e)
        traceback.print_exc()
Exemple #4
0
from win32com.client.gencache import EnsureDispatch as Dispatch
import os
import sys

outlook = Dispatch("Outlook.Application")
mapi = outlook.GetNamespace("MAPI")

class Oli():
    def __init__(self, outlook_object):
        self._obj = outlook_object

    def items(self):
        array_size = self._obj.Count
        for item_index in range(1,array_size+1):
            yield (item_index, self._obj[item_index])

    def prop(self):
        return sorted( self._obj._prop_map_get_.keys() )

# for inx, folder in Oli(mapi.Folders).items():
#     # iterate all Outlook folders (top level)
#     #print "-"*70
#     print(folder.Name)

#     for inx,subfolder in Oli(folder.Folders).items():
#         print("({}) {} => {}".format(inx, subfolder.Name, subfolder))

def mkdir_p(path):
    try:
        os.makedirs(path)
    except:
Exemple #5
0
def scan_inbox():
    '''
    docstring
    '''
    global CONFIG
    foldertree = CONFIG["foldertree"]
    outlook = Dispatch("Outlook.Application")
    mapi = outlook.GetNamespace("MAPI")
    cwd = os.getcwd()
    processed = []
    matchfound = False

    class Oli():
        def __init__(self, outlook_object):
            self._obj = outlook_object

        def items(self):
            array_size = self._obj.Count
            for item_index in xrange(1, array_size + 1):
                yield (item_index, self._obj[item_index])

        def prop(self):
            return sorted(self._obj._prop_map_get_.keys())

    rules = None

    def loadrules():
        '''
    docstring
    '''
        rules = load_yara("rules")
        rulecount = 0
        for r in rules:
            rulecount += 1
        print("Loaded " + str(rulecount) + " YARA rules.")
        folderindex = 0

    loadrules()
    loadioc()
    #this needs fixing :/
    outlookfolder = mapi.Folders
    for inx, folder in Oli(outlookfolder).items():
        if folder.Name == foldertree[0]:
            outlookfolder = folder
            print(">" + folder.Name + ":")
            break
    for currentfolder in foldertree[1:]:
        for inx, folder in Oli(outlookfolder.Folders).items():
            if folder.Name == currentfolder:
                print("\t>> " + folder.Name)
                outlookfolder = folder
                folderindex = inx
            # break

    try:
        os.mkdir(cwd + "\\workdir")
    except Exception:
        pass
    # https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.outlook._mailitem?view = outlook-pia
    for msg in outlookfolder.Items:
        try:
            for attachment in msg.Attachments:
                if attachment.FileName.startswith(
                        "Scan_results") and hashlib.sha256(
                            msg.Body).hexdigest() not in processed:
                    print("Removed Scan_results")
                    msg.Attachments.Remove(attachment.Index)
                    msg.Save()
        except Exception as e:
            print(e)
    #raw_input("Press any key to start scanning, Outlook will ask you for grant permission to access the Inbox...")
    while True:
        print(
            "[" + datetime.datetime.fromtimestamp(
                time.time()).strftime('%Y-%m-%d %H:%M:%S') +
            "] Inbox Scan started for " + "/".join(foldertree).strip("/"))
        print("Scanning folder: /".join(foldertree).strip("/"))
        for msg in outlookfolder.Items:
            try:

                msgsha256 = hashlib.sha256(msg.Body).hexdigest()
                if msgsha256 in processed:
                    continue
                else:
                    processed.append(msgsha256)

                yarascan = ''
                external_scan_result = ''
                msgmeta = ''
                yaramatches = None
                senderdomain = sendertxt = senderspf = ''
                try:
                    msgmeta += "SenderEmailAddress:\t" + \
                        unidecode.unidecode(msg.SenderEmailAddress)+"\n"
                    msgmeta += "To:\t" + unidecode.unidecode(msg.To) + "\n"
                    msgmeta += "Subject:\t" + \
                        unidecode.unidecode(msg.Subject)+"\n"
                    msgmeta += "CC:\t" + unidecode.unidecode(msg.CC) + "\n"
                    msgmeta += "Categories:\t" + \
                        unidecode.unidecode(msg.Categories)+"\n"
                except AttributeError:
                    pass

                print("-" * 80)
                print(msgmeta)

                if msg.Attachments.Count > 0:
                    #print "."
                    for attachment in msg.Attachments:
                        # print attachment
                        #print attachment.GetTemporaryFilePath()
                        #print attachment.FileName
                        if attachment.FileName:
                            attachment.SaveAsFile(cwd + "\\workdir\\" +
                                                  attachment.FileName)
                            try:
                                yaramatches = rules.match(cwd + "\\workdir\\" +
                                                          attachment.FileName)
                            except Exception as e:
                                pass
                            if yaramatches:
                                matchfound = True
                                yarascan += print_yara(
                                    yaramatches,
                                    context="Attachment match:" +
                                    str(attachment.FileName)[:40],
                                    msg=msgmeta)
                                with open(
                                        cwd + "\\workdir\\" +
                                        attachment.FileName, "rb") as f:
                                    scan_result, matchfound = external_scans(
                                        f.read(), binary=True)
                                    external_scan_result += scan_result
                            if attachment.FileName.lower().endswith(".msg"):
                                msgdata = ExtractMsg.process_msg(
                                    cwd + "\\workdir\\" + attachment.FileName)
                                m = "MSG found:"+cwd+"\\workdir\\"+attachment.FileName+"\n\tSubject:" + \
                                    str(msgdata["subject"])+"\n\tTo:"+str(msgdata["to"])+"\n\tFrom:"+str(
                                        msgdata["from"])+"\n\tDate:"+str(msgdata["date"])
                                msgmeta += m + "\n"

                                print("MSG attachment:")
                                print(m)
                                # print(msgdata["body"])
                                try:
                                    yaramatches = rules.match(
                                        data=msgdata["body"])
                                except Exception as e:
                                    pass
                                    # traceback.print_exc()
                                if yaramatches:
                                    yarascan += print_yara(
                                        yaramatches,
                                        context="MSG attachment body match",
                                        msg=msgmeta)
                                    matchfound = True
                                scan_result, matchfound = external_scans(
                                    msgdata["body"], vt=False)
                                external_scan_result += scan_result
                                if not None is msgdata:
                                    for msgattachment in msgdata[
                                            'attachments']:
                                        # print("Attachment:"+msgattachment["filename"])
                                        with open(
                                                cwd + "\\workdir\\__" +
                                                attachment.FileName + "__" +
                                                msgattachment["filename"],
                                                "wb+") as f:
                                            f.write(
                                                base64.b64decode(
                                                    msgattachment['data']))
                                        try:
                                            yaramatches = rules.match(
                                                cwd + "\\workdir\\__" +
                                                attachment.FileName + "__" +
                                                msgattachment["filename"])
                                        except Exception as e:
                                            pass
                                        if yaramatches:
                                            matchfound = True
                                            yarascan += print_yara(
                                                yaramatches,
                                                msg=msgmeta,
                                                context=
                                                "Attachment extracted from MSG attachment matched: "
                                                +
                                                str(attachment.FileName +
                                                    "__" +
                                                    msgattachment["filename"])
                                                [:64])
                                            with open(
                                                    cwd + "\\workdir\\__" +
                                                    attachment.FileName +
                                                    "__" +
                                                    msgattachment["filename"],
                                                    "rb") as f:
                                                scan_result, matchfound = external_scans(
                                                    f.read(), binary=True)
                                                external_scan_result += scan_result
                                else:
                                    print("MSGdata is none")
                hbody = unidecode.unidecode(msg.HTMLBody)
                body = unidecode.unidecode(msg.Body)
                for line in hbody.splitlines():
                    if "x-originating-ip" in line.lower():
                        print line

                try:
                    yaramatches = rules.match(data=hbody)
                    yaramatches = rules.match(data=body)
                except Exception as e:
                    pass

                if yaramatches:
                    print '-' * 80
                    matchfound = True
                    yarascan += print_yara(yaramatches,
                                           context="HTMLBody matched",
                                           msg=msgmeta,
                                           showstrings=True)
                    print '-' * 80

            # if yaramatches:
            #  print '-'*80
            #yarascan += print_yara(yaramatches,context = "Plain body matched",msg = msgmeta,showstrings = True)
            # print '-'*80

                scan_result, matchfound = external_scans(hbody, vt=False)
                external_scan_result += scan_result
                #print (external_scan_result)
                header = '''
                <html>
                <body>
                '''
                footer = '''
                </body>
                </html>
                '''
                if senderspf:
                    header += "<h1>SPF records</h1></b>"
                    header += "<h3>Domain</h3>:" + senderdomain
                    header += "</br>SPF records:" + senderspf
                if yarascan.strip():
                    yarascan = "<h1>Yara matches</h1></br><pre>" + yarascan + "</pre><hr>"
                scanres = header + yarascan + "</br>\n" + external_scan_result
                resfile = ''
                if matchfound:
                    resfile = "Scan_results_matchfound.htm"
                else:
                    resfile = "Scan_results_nomatches.htm"

                if scanres:
                    with open(cwd + "\\" + resfile, "w+") as f:
                        f.write(scanres)
                    msg.Attachments.Add(cwd + "\\" + resfile, 1, 1, resfile)
                    msg.Save()
            except Exception as e:
                print e
                traceback.print_exc()
        print(
            "[" + datetime.datetime.fromtimestamp(
                time.time()).strftime('%Y-%m-%d %H:%M:%S') +
            "] Done scanning,sleeping for " + str(CONFIG["scan_interval"]) +
            " seconds...")
        time.sleep(int(CONFIG["scan_interval"]))
        loadconfig()
        loadioc()

"""Open the grid combination csv file, skip the header with the .next method, and split out each column by the comma delimiter"""
combofileloc = "c:/temp/dog.csv" 
combofile = open(combofileloc, 'r')
combofile.next()
print 'connecting to db'

DATABASE_FILEPATH = r"c:\temp\test.accdb"
CONNECTION_STRING = "Provider=Microsoft.Jet.OLEDB.4.0; data Source=%s" % \
DATABASE_FILEPATH

if os.path.exists (DATABASE_FILEPATH):
    os.remove (DATABASE_FILEPATH)

adox = Dispatch ("ADOX.Catalog")
adox.Create (CONNECTION_STRING)
adox = None
db = Dispatch ('ADODB.Connection')
db.Open (CONNECTION_STRING)
print 'made it to here'   
try:
##    db.Execute ('CREATE TABLE dtest (id INT, data INT)')
##    db.Execute ('INSERT INTO dtest (id, data) VALUES (1, 2)')
    db.Execute ('CREATE TABLE combo(rowid INT, value2 INT, count2 INT, evt INT, evc INT, evh INT)')
##    db.Execute ('INSERT INTO combo(rowid, value2, count2, evt, evc, evh) VALUES (1, 2, 3, 4, 5, 6)')
##    print' it worked'
     
    for line in combofile:
        print 'iterating'
        items = line.split(',')
Exemple #7
0
from win32com.client import constants
from win32com.client.gencache import EnsureDispatch as Dispatch


def list_folders(mapi_object, indent=''):
    for _, folder in enumerate(mapi_object.Folders, 1):  # 1-index
        if folder.DefaultItemType == constants.olMailItem:
            print('{}{}'.format(indent, folder.Name))
            list_folders(folder, indent=indent + ' ')


#def to_folder(mapi, folder):

mapi = Dispatch("Outlook.Application").GetNamespace("MAPI")
#list_folders( mapi )
#mapi.Logon("Outlook")
#inbox = mapi.GetDefaultFolder()
inbox = mapi.Folders.Item("production")
print(inbox)
'''
messages = inbox.Items
message = messages.GetLast()
body = message.Body
print(body)
'''
def read_email(date, my_account, sent_account):
    # 读取邮箱
    outlook = Dispatch("Outlook.Application")
    mapi = outlook.GetNamespace("MAPI")
    Accounts = mapi.Folders

    # 读取的员工账号
    names = []
    # 短信内容
    contents = []
    # 读取邮件存入pandas
    c = ['Root_Directory_Name_1', 'Level_1_FolderName_1', 'Level_2_FolderName_1', 'ReceivedTime_1', 'SenderName_1',
         'to_to_1', 'cc_cc_1', 'Subject_1', 'MessageID_1', 'ConversationTopic_1', 'ConversationID_1',
         'ConversationIndex_1', 'EmailBody_1']
    df = pd.DataFrame(columns=c)

    for Account_Name in Accounts:
        # 只查找需要的邮箱账号信息

        if Account_Name.Name == my_account:
            print(' >> 正在查询的帐户名称:', Account_Name.Name, '\n')
            Level_1_Names = Account_Name.Folders
            for Level_1_Name in Level_1_Names:
                #             只需要收件箱的邮件
                if Level_1_Name.Name == '收件箱':
                    print(' - 正在查询一级目录:', Level_1_Name.Name)
                    Mail_1_Messages = Level_1_Name.Items
                    #                 将邮件按日期排序,可减少遍历内容
                    Mail_1_Messages.Sort("[ReceivedTime]", True)

                    for xx in Mail_1_Messages:  # xx = 'mail'  # 开始查看单个邮件的信息
                        print(xx.Subject)
                        Root_Directory_Name_1 = Account_Name.Name
                        Level_1_FolderName_1 = Level_1_Name.Name
                        Level_2_FolderName_1 = ''
                        ReceivedTime_1 = str(xx.ReceivedTime)[:10]  # 接收日期

                        if ReceivedTime_1 != date:  # 只要特定日期的邮件
                            continue

                        SenderName_1 = xx.SenderName  # 发件人

                        if SenderName_1 != sent_account:  # 只要特定发件人的邮件
                            continue

                        Subject_1 = xx.Subject  # 主题
                        EmailBody_1 = xx.Body  # 邮件内容
                        to_to_1 = xx.To  # 收件人
                        cc_cc_1 = xx.CC  # 抄送人
                        MessageID_1 = xx.EntryID  # 邮件MessageID
                        ConversationTopic_1 = xx.ConversationTopic  # 会话主题
                        ConversationID_1 = xx.ConversationID  # 会话ID
                        ConversationIndex_1 = xx.ConversationIndex  # 会话记录相对位置

                        data = [Root_Directory_Name_1, Level_1_FolderName_1, Level_2_FolderName_1, ReceivedTime_1,
                                SenderName_1, to_to_1, cc_cc_1, Subject_1, MessageID_1, ConversationTopic_1,
                                ConversationID_1, ConversationIndex_1, EmailBody_1]

                        dataf = dict(zip(c, data))
                        df = df.append(dataf, ignore_index=True)

    return df
Exemple #9
0
    for i in range(result.Size):
        document = result.GetDocument(i)
        try:
            sep = document.rindex(':')
            line = int(document[sep + 1:])
            document = document[:sep]
            print document.decode('iso-8859-1'), line, result.GetScore(
                i), linecache.getline(document, line + 1).decode('iso-8859-1'),
        except ValueError:
            print document.encode('iso-8859-1', 'ignore'), result.GetScore(i)


try:
    try:
        ti = Dispatch('tstlib.TextIndex')

        print "Version :", ti.Version

        # ---------------- petit test sans conséquence

        ti.AddText('bonjour, je suis nicolas', '1')
        ti.AddText('bonjour, je suis alfred', '2')
        ti.AddText('alfred hitchcock', '3')
        ti.AddText('bonjour comment allez-vous ?', '3')

        dump(ti, 'bonjour')
        dump(ti, 'bonjour comment alfred', 0)
        dump(ti, 'bonjour comment alfred', 1)

        print "Saved %s !" % ti.Save(r'c:\temp\test.ti')
Exemple #10
0
import requests, os, sys
from bs4 import BeautifulSoup
from win32com.client.gencache import EnsureDispatch as Dispatch

URL = "https://community.articulate.com/series/storyline-3"
HOME_URL = "https://community.articulate.com"

with requests.Session() as s:
    r = s.get(URL)
    html = BeautifulSoup(r.text, "html.parser")
    sections = html.find("div", {
        "class": "post__body series__body"
    }).find_all("section", {"class": "series__section"})
    message = Dispatch("CDO.Message")
    message.CreateMHTMLBody(URL)
    stream = Dispatch(message.GetStream())
    stream.SaveToFile("main page.mht", 2)
    stream.Close()
    for sec in sections:
        ftag = sec.find("h2")
        links = sec.find("nav").find("ol").find_all("li")
        fpath = ""
        if ftag is not None:
            fpath = ftag.string
        else:
            fpath = "Getting Started"
        os.mkdir(fpath)
        for link in links:
            u = link.find("a")["href"]
            fname = u.split("/")[len(u.split("/")) - 1]
            message = Dispatch("CDO.Message")