-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from IntelligenceX/python-code
Adding Python implementation
- Loading branch information
Showing
4 changed files
with
194 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Intelligence X API Python client | ||
|
||
1. [ix_search.py to search and show results](ix_search.py) | ||
2. [ix_phonebook.py to make a phonebook lookup](ix_phonebook.py) | ||
|
||
## Usage | ||
|
||
First, make sure to download the dependencies: | ||
|
||
``` | ||
pip install termcolor | ||
pip install requests | ||
``` | ||
|
||
Using the Python files is straight forward: | ||
|
||
```shell | ||
ix_search.py <selector> | ||
ix_phonebook.py <selector> | ||
``` | ||
|
||
Examples: | ||
|
||
```shell | ||
ix_search.py test.com | ||
ix_phonebook.py test.com | ||
``` | ||
|
||
The results are currently displayed as raw JSON data. Contributions for a more user-friendly representation are welcome. | ||
|
||
## Notes | ||
|
||
If you have an account on intelx.io go to the developer tab at https://intelx.io/account?tab=developer to get your own API key and URL. | ||
|
||
In the `ix_search.py` change the default API host and key here: | ||
|
||
```python | ||
ix_search("public.intelx.io","9df61df0-84f7-4dc7-b34c-8ccfb8646ace",sys.argv[1]) | ||
``` | ||
|
||
The same for `ix_phonebook.py`: | ||
|
||
```python | ||
ixphonebook("public.intelx.io","9df61df0-84f7-4dc7-b34c-8ccfb8646ace",sys.argv[1]) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import re,sys,os,time | ||
import requests | ||
import json | ||
from termcolor import colored | ||
import urllib | ||
from datetime import date | ||
import datetime | ||
|
||
|
||
#Phonebook search that will return results for a given selector | ||
def ixphonebook(baseurl,apikey,term): | ||
|
||
headers = { | ||
"User-Agent": "ix-client/python", | ||
"x-key": apikey, | ||
} | ||
|
||
payload = { | ||
"term": term, | ||
"buckets": [], | ||
"lookuplevel": 0, | ||
"maxresults": 10, | ||
"timeout": 0, | ||
"datefrom": "", | ||
"dateto": "", | ||
"sort": 4, | ||
"media": 0, | ||
"terminate": [] | ||
} | ||
|
||
print(colored("[+] " + str(date.today()) + ": Kicking off phonebook query of " + str(term) + " with max results set at " + str(payload['maxresults']),"red")) | ||
|
||
#initial POST to intelligence X service to retrieve the results ID and do all the fun stuff | ||
getid = requests.post("https://" + baseurl + "/phonebook/search",data=json.dumps(payload),headers=headers) | ||
id_response = getid.json() | ||
|
||
if id_response['status'] == 0: | ||
print(colored("[+]Successful API Authentication. Searching available records...","green")) | ||
resulturl = "https://" + baseurl + "/phonebook/search/result?id=%s" %str(id_response['id']) | ||
|
||
status = 3 # status 3 = No results yet, keep trying. 0 = Success with results | ||
while status == 3 or status == 0: | ||
|
||
getresults = requests.get(resulturl,headers=headers) | ||
data = getresults.json() | ||
status = data['status'] | ||
|
||
if status == 0 or status == 1: | ||
print(data) | ||
elif status == 2: | ||
print("----------------------------------------------") | ||
print("[!] Error Code 2 Search ID Not Found ") | ||
print("----------------------------------------------") | ||
|
||
if id_response['status'] == 1: | ||
print("[!]Invalid term used.") | ||
|
||
if id_response['status'] == 2: | ||
print("[!] Reached the MAX number of concurrent connections for this API Key.") | ||
|
||
if __name__ == "__main__": | ||
|
||
start = time.time() | ||
#python ix_phonebook.py <selector> | ||
ixphonebook("public.intelx.io","9df61df0-84f7-4dc7-b34c-8ccfb8646ace",sys.argv[1]) | ||
end = time.time() | ||
print(colored("[*] The script executed in [" + str((end-start)) + " seconds|" + str(((end-start)/60)) + " minutes].","blue")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import re,sys,os,time | ||
import requests | ||
import json | ||
from termcolor import colored | ||
import urllib | ||
|
||
|
||
def ix_search(baseurl,apikey,term): | ||
|
||
headers = { | ||
"User-Agent": "ix-client/python", | ||
"x-key": apikey, | ||
} | ||
|
||
payload = { | ||
"term": term, | ||
"buckets": [], | ||
"lookuplevel": 0, | ||
"maxresults": 10, #change the limit of max results here | ||
"timeout": 0, | ||
"datefrom": "", | ||
"dateto": "", | ||
"sort": 4, | ||
"media": 0, | ||
"terminate": [] | ||
} | ||
|
||
searchurl = 'https://' + baseurl + '/intelligent/search' | ||
getid = requests.post("https://" + baseurl + "/intelligent/search",data=json.dumps(payload),headers=headers) | ||
|
||
if getid.status_code == 200: | ||
id_response = getid.json() | ||
|
||
#Authenticate to API | ||
if id_response['status'] == 0: | ||
print(colored("[+]Successful API Authentication. Starting records search.","green")) | ||
#Craft API URL with the id to return results | ||
resulturl = str(searchurl) + "/result?id=%s" %str(id_response['id']) | ||
|
||
status = 3 # status 3 = No results yet, keep trying. 0 = Success with results | ||
while status == 3 or status == 0: | ||
|
||
getresults = requests.get(resulturl,headers=headers) | ||
data = getresults.json() | ||
status = data['status'] | ||
|
||
if status == 0 or status == 1: | ||
#print data in json format to manipulate as desired | ||
print(data) | ||
elif status == 2: | ||
print("----------------------------------------------") | ||
print("[!] Error Code 2 Search ID Not Found ") | ||
print("----------------------------------------------") | ||
|
||
else: | ||
print("----------------------------------------------") | ||
print("[!] Error Code Status: <" + str(getid.status_code) + ">") | ||
print("----------------------------------------------") | ||
print("204 | No Content") | ||
print("400 | Bad Request. Invalid input.") | ||
print("401 | Unauthorized. Access not authorized.") | ||
print("402 | Payment Required. No credits available") | ||
print("404 | Not Found. Item or identifier not found.") | ||
print("----------------------------------------------") | ||
|
||
if __name__ == "__main__": | ||
#python ix_search.py <selector> | ||
ix_search("public.intelx.io","9df61df0-84f7-4dc7-b34c-8ccfb8646ace",sys.argv[1]) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters