-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e453b04
commit d87d7d8
Showing
4 changed files
with
181 additions
and
1 deletion.
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
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,85 @@ | ||
import base64 | ||
import json | ||
from pathlib import Path | ||
import logging | ||
import click | ||
import pwd | ||
import os | ||
import requests | ||
from Crypto import Random | ||
from Crypto.Hash import SHA224 | ||
from Crypto.PublicKey import RSA | ||
from Crypto.Signature import PKCS1_v1_5 | ||
|
||
|
||
@click.command() | ||
@click.option('-u', 'url', default='https://asydns.org', help='API URL') | ||
@click.option('-g', 'generate', is_flag=True, default=False, help='Force the generation of a new key pair') | ||
@click.option('-r', 'revoke', is_flag=True, default=False, help='Revoke the public key') | ||
@click.option('-v', 'verbose', is_flag=True, default=False, help='Verbose output') | ||
def cmd_asydns(url, generate, revoke, verbose): | ||
|
||
if verbose: | ||
logging.basicConfig(level=logging.INFO, format='%(message)s') | ||
|
||
homedir = Path(pwd.getpwuid(os.getuid()).pw_dir) | ||
|
||
dotdir = homedir / '.asydns' | ||
dotdir.mkdir(exist_ok=True) | ||
|
||
pub_file = dotdir / 'rsa.pub' | ||
key_file = dotdir / 'rsa.key' | ||
|
||
if generate or not key_file.is_file(): | ||
|
||
logging.info('Generating RSA key ...') | ||
random_generator = Random.new().read | ||
key = RSA.generate(2048, random_generator) | ||
pub = key.publickey() | ||
|
||
with key_file.open('w') as k: | ||
k.write(key.exportKey('PEM').decode()) | ||
|
||
with pub_file.open('w') as p: | ||
p.write(pub.exportKey('PEM').decode()) | ||
|
||
|
||
logging.info('Loading RSA key ...') | ||
with key_file.open() as k: | ||
key = RSA.importKey(k.read()) | ||
|
||
with pub_file.open() as p: | ||
pub = RSA.importKey(p.read()) | ||
|
||
|
||
r = requests.get(url + '/api') | ||
|
||
if r.status_code != 200: | ||
logging.error('Error') | ||
logging.error(r.content.decode()) | ||
return False | ||
|
||
j = r.json() | ||
|
||
challenge = base64.b64decode(j['challenge']) | ||
signer = PKCS1_v1_5.new(key) | ||
response = signer.sign(SHA224.new(challenge)) | ||
response = base64.b64encode(response).decode() | ||
|
||
if revoke: | ||
r = requests.delete(url + '/api', json={'pub': pub.exportKey('PEM').decode(), 'challenge' : j['challenge'], 'response': response}) | ||
else: | ||
r = requests.post(url + '/api', json={'pub': pub.exportKey('PEM').decode(), 'challenge' : j['challenge'], 'response': response}) | ||
|
||
if r.status_code != 200: | ||
logging.error('Error') | ||
logging.error(r.content.decode()) | ||
return False | ||
|
||
print(json.dumps(r.json(), indent=4)) | ||
|
||
return True | ||
|
||
if __name__ == '__main__': | ||
cmd_asydns() | ||
|
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,73 @@ | ||
import hashlib | ||
import json | ||
import logging | ||
import re | ||
import socket | ||
from pathlib import Path | ||
from time import gmtime, strftime | ||
|
||
import click | ||
|
||
from habu.lib.loadcfg import loadcfg | ||
|
||
|
||
def get_cymru(this_hash): | ||
""" | ||
Example Output:: | ||
{ | ||
'detected': '86', | ||
'last_seen': '01-06-2014T22:34:57Z' | ||
} | ||
source: http://code.google.com/p/malwarecookbook/ | ||
site : http://www.team-cymru.org/Services/MHR/ | ||
""" | ||
host = 'hash.cymru.com' | ||
request = '%s\r\n' % this_hash | ||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
try: | ||
s.connect((host, 43)) | ||
s.send('begin\r\n'.encode()) | ||
s.recv(1024) | ||
s.send(request.encode()) | ||
response = s.recv(1024) | ||
s.send('end\r\n'.encode()) | ||
s.close() | ||
if len(response) > 0: | ||
resp_re = re.compile('\S+ (\d+) (\S+)') | ||
match = resp_re.match(response.decode()) | ||
if 'NO_DATA' in match.group(2): | ||
return dict(last_seen_utc=strftime("%Y-%m-%dT%H:%M:%SZ", | ||
gmtime(int(match.group(1)))), | ||
detected=match.group(2), | ||
response_code=404) | ||
else: | ||
return dict(last_seen_utc=strftime("%Y-%m-%dT%H:%M:%SZ", | ||
gmtime(int(match.group(1)))), | ||
detected=match.group(2), | ||
response_code=200) | ||
except socket.error: | ||
return dict(error='socket error', response_code=500) | ||
|
||
|
||
@click.command() | ||
@click.argument('input', type=click.File('rb')) | ||
@click.option('-o', 'output', type=click.File('w'), default='-', help='Output file (default: stdout)') | ||
@click.option('-v', 'verbose', is_flag=True, default=False, help='Verbose output') | ||
def cmd_shodan(input, output, verbose): | ||
|
||
habucfg = loadcfg() | ||
|
||
if verbose: | ||
logging.basicConfig(level=logging.INFO, format='%(message)s') | ||
|
||
filename = Path(input.name).name | ||
|
||
data = input.read() | ||
|
||
sha1 = hashlib.sha1(data).hexdigest() | ||
|
||
output.write(json.dumps(get_cymru(sha1), indent=4, sort_keys=True)) | ||
output.write('\n') | ||
|
||
if __name__ == '__main__': | ||
cmd_shodan() |
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