A Python package to search & delete emails using Microsoft Graph API
Project description
graphish
A Python package to search & delete messages from mailboxes in Office 365 using Microsoft Graph API
Current Features
* Create new Search
* Update a Search
* Get Search Folder
* Get Search messages
* Delete Search
* Delete a Message
Installation
To use, please install the package locally:
Installation
Locally:
bash git clone git@github.com:swimlane/graphish.git cd graphish pip install setup.py
OS X & Linux:
pip install graphish
Windows:
pip install graphish
Usage
To use, you will need to have created a new application in Azure AD. Follow these instructions to obtain the appropriate secrets:
https://docs.microsoft.com/en-us/graph/auth-register-app-v2
Once you have this information, then you can do the following:
GraphConnector
To use graphish
you must first create a GraphConnector
object that contains all your authentication information. Once you have created this connector object then a user will provide this object as a mandatory parameter to other classes within this package.
Here are the different ways to generate a GraphConnector
object and are dependent on which authentication workflow method you have choose for your application.
Delegated Authentication
To use graphish
with delegated permissions and a username and password you will need to supply the clientId, clientSecret, tenantId, as well as your accounts username and password.
By using the delegated authentication (Single-Page, Web Apps, Mobile & Native Apps - Grant Auth Flow) you can search your own mailbox by not passing a userPrincipalName
or if you would like to search another mailbox then provide the userPrincipalName
(e-mail address):
Creating connector for your account using a (Single-Page, Web Apps, Mobile & Native Apps) authentication flow:
from graphish import GraphConnector
connector = GraphConnector(
clientId='14b8e5asd-c5a2-4ee7-af26-53461f121eed', # you applications clientId
clientSecret='OdhG1hXb*UB/ho]A?0ZCci13KMflsHDy', # your applications clientSecret
tenantId='c1141d00-072f-1eb9-2526-12802571dd41', # your applications Azure Tenant ID
userPrincipalName='first.last@myorg.onmicrosoft.com', # the user's mailbox you want to search
password='somepassword' # password of your normal or admin account
)
Creating connector for another users mailbox using a (Single-Page, Web Apps, Mobile & Native Apps) authentication flow:
from graphish import GraphConnector
# For legacy app grant flow provide a username and password
connector = GraphConnector(
clientId='14b8e5asd-c5a2-4ee7-af26-53461f121eed', # you applications clientId
clientSecret='OdhG1hXb*UB/ho]A?0ZCci13KMflsHDy', # your applications clientSecret
tenantId='c1141d00-072f-1eb9-2526-12802571dd41', # your applications Azure Tenant ID
userPrincipalName='first.last@myorg.onmicrosoft.com', # the user's mailbox you want to search
password='somepassword' # password of your normal or admin account
userPrincipalName='some.account@myorg.onmicrosoft.com' # the user's mailbox you want to search
)
Application Authentication
To use graphish
with application permissions you will need to supply the clientId, clientSecret, and tenantId.
By using the application authentication (Client Credentials Grant Auth Flow) you can search your own mailbox by not passing a userPrincipalName
or if you would like to search another mailbox then provide the userPrincipalName
(e-mail address):
Creating a connector for your account using a service/daemon authentication flow:
from graphish import GraphConnector
# For backend / client_credential auth flow just supply the following
connector = GraphConnector(
clientId='14b8e5asd-c5a2-4ee7-af26-53461f121eed', # you applications clientId
clientSecret='OdhG1hXb*UB/ho]A?0ZCci13KMflsHDy', # your applications clientSecret
tenantId='c1141d00-072f-1eb9-2526-12802571dd41', # your applications Azure Tenant ID
)
Creating a connector for another users mailbox using a service/daemon authentication flow:
from graphish import GraphConnector
# For backend / client_credential auth flow just supply the following
connector = GraphConnector(
clientId='14b8e5asd-c5a2-4ee7-af26-53461f121eed', # you applications clientId
clientSecret='OdhG1hXb*UB/ho]A?0ZCci13KMflsHDy', # your applications clientSecret
tenantId='c1141d00-072f-1eb9-2526-12802571dd41', # your applications Azure Tenant ID
userPrincipalName='some.account@myorg.onmicrosoft.com' # the user's mailbox you want to search
scopes=['https://graph.microsoft.com/Mail.ReadWrite'] # the scopes (default value of https://graph.microsoft.com/.default)
)
Creating a new Search
Once you have determined your appropriate authentication and have created a GraphConnector
object, then you can create a new Search
Object. Once you have your Search
Object then you can create a new search. This will create a hidden folder in the users mailbox (that the user is unable to see) and it will populate based on your search filterQuery.
from graphish import Search
search = Search(connector)
new_search = search.create(
searchFolderName='Phishing Search',
sourceFolder='inbox',
filterQuery="contains(subject, 'EXPIRES')"
)
Getting messages from a Search
You can retrieve messages identified during your search by using the same instance of your Search object and using the messages
method:
# get all the messages in your search folder
print(search.messages())
Getting a list of mail folders
If you are needing a list of mail folders in a mailbox you can use the folders
method to retrieve them:
# get a list of search folders
search.folders()
Updating a search
If you wanted to make changes to a search performed you can update the search folder and individual criteria like the name of the search folder, the sourceFolder (root to search), or the filterQuery itself:
# update your search folder property's
search.update(
searchFolderName='Some Phishing Search',
sourceFolder='inbox',
filterQuery="contains(subject, 'EXPIRES!')"
)
Deleting a search
You can also delete a search performed by using the delete
method:
# delete the current search folder
search.delete()
Additional Examples
You can find additional examples here
Release History
- 1.0.0
- Initial release of graphish to PyPi
Meta
Josh Rickard – @MSAdministrator – rickardja@live.com
Distributed under the MIT license. See LICENSE
for more information.
Contributing
- Fork it (https://github.com/swimlane/graphish/fork)
- Create your feature branch (
git checkout -b feature/fooBar
) - Commit your changes (
git commit -am 'Add some fooBar'
) - Push to the branch (
git push origin feature/fooBar
) - Create a new Pull Request
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file graphish-1.0.0.tar.gz
.
File metadata
- Download URL: graphish-1.0.0.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2d0876f91ef4df7d887ae6b668dc70bbcbf36e613a5aa98a09a72cf07a969bb8 |
|
MD5 | 6f20bbcfd582a648ac912ed0f3d0b6ba |
|
BLAKE2b-256 | baf602be1cb6b256567045fa30dc4f56dc134a737b784d930d7ae36d4250415d |
File details
Details for the file graphish-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: graphish-1.0.0-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7574c1956f6ae2cf3b75ff93b94d9c3b2474d348dcb2690617a6cbd1f19fd1e7 |
|
MD5 | 5524f82945bd002a687a1eddd2061aac |
|
BLAKE2b-256 | e3066833465b87b37d71e33aca4a85d4ddd94aa924a1b6ff1ed67057eace470c |
File details
Details for the file graphish-1.0.0-py2-none-any.whl
.
File metadata
- Download URL: graphish-1.0.0-py2-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 210ecf3cdc3cfa9adb901fd7fb1e3e9be2bf7ea041b58c437d95987b1f63b604 |
|
MD5 | 32ca59ed44b90c963d660858836debd9 |
|
BLAKE2b-256 | 15a874b5b228c6503a32bc8cc1458b9053582be57fe5b9e445ce2ebcbf9eb624 |