Skip to content

Commit

Permalink
new habu.asydns command
Browse files Browse the repository at this point in the history
  • Loading branch information
fportantier committed May 21, 2018
1 parent e453b04 commit d87d7d8
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 1 deletion.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,25 @@ Columns: Seconds from last packet | IP | MAC | Vendor
6 192.168.0.7 54:f2:01:db:35:58 Samsung Electronics Co.,Ltd
```

## habu.asydns: AsyDNS protocol client

This command requests DNS domain names based on public and private
RSA keys using the AsyDNS protocol (<https://github.com/portantier/asydns>).

``` {.sourceCode .bash}
$ habu.asydns -v
Generating RSA key ...
Loading RSA key ...
{
"ip": "181.31.41.231",
"name": "07286e90fd6e7e6be61d6a7919967c7cf3bbfb23a36edbc72b6d7c53.a.asydns.org"
}
$ dig +short 07286e90fd6e7e6be61d6a7919967c7cf3bbfb23a36edbc72b6d7c53.a.asydns.org
181.31.41.231
```


## habu.contest: Check your connection capabilities

This command tries to connect to various services and check if you can
Expand Down
85 changes: 85 additions & 0 deletions habu/cli/cmd_asydns.py
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()

73 changes: 73 additions & 0 deletions habu/cli/cmd_mhr.py
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()
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='habu',
version='0.0.68',
version='0.0.70',
description='Python Network Hacking Toolkit',
long_description=readme,
long_description_content_type='text/markdown',
Expand All @@ -18,6 +18,7 @@
'click',
'lxml',
'prompt_toolkit',
'pycrypto',
'pygments',
'regex',
'requests',
Expand All @@ -34,6 +35,7 @@
habu.arping=habu.cli.cmd_arping:cmd_arping
habu.arpoison=habu.cli.cmd_arpoison:cmd_arpoison
habu.arpsniff=habu.cli.cmd_arpsniff:cmd_arpsniff
habu.asydns=habu.cli.cmd_asydns:cmd_asydns
habu.b64=habu.cli.cmd_b64:cmd_b64
habu.contest=habu.cli.cmd_contest:cmd_contest
habu.ctfr=habu.cli.cmd_ctfr:cmd_ctfr
Expand All @@ -52,6 +54,7 @@
habu.jshell=habu.cli.cmd_jshell:cmd_jshell
habu.karma=habu.cli.cmd_karma:cmd_karma
habu.land=habu.cli.cmd_land:cmd_land
habu.mhr=habu.cli.cmd_mhr:cmd_mhr
habu.ping=habu.cli.cmd_ping:cmd_ping
habu.shodan=habu.cli.cmd_shodan:cmd_shodan
habu.snmp_crack=habu.cli.cmd_snmp_crack:cmd_snmp_crack
Expand Down

0 comments on commit d87d7d8

Please sign in to comment.