-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtarget_gen.py
198 lines (159 loc) · 5.71 KB
/
target_gen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env python
__author__ = 'Ben Finke'
"""
target_gen.py is a utility to automatically build the targets file for donuts.py
You can pass target_gen.py a file with DNS names, or use the interactive menu to build the list.
The rdataing file contains the DNS names of note and the correct IP address to monitor for.
March 2015
@benfinke
Usage: python target_gen.py -i <input_file> -o <output_file>
"""
import sqlite3
import sys
import dns.resolver
db_file = "donuts.db"
# Set up SQLite instance
def setup_db():
db_conn = sqlite3.connect(db_file)
with db_conn:
cur = db_conn.cursor()
cur.execute("DROP TABLE IF EXISTS targets")
cur.execute("""
CREATE TABLE targets(
id_num INTEGER PRIMARY KEY AUTOINCREMENT,
dns_address TEXT KEY,
a_ip TEXT,
aaaa_ip TEXT,
mx_ip TEXT,
soa_ip TEXT,
ns_ip TEXT,
txt_ip TEXT)
""")
# Display data in table
def display_entries():
db_conn = sqlite3.connect(db_file)
with db_conn:
db_conn.row_factory = sqlite3.Row
cur = db_conn.cursor()
cur.execute("SELECT * from targets")
rows = cur.fetchall()
for row in rows:
print ("%d -- %s -- %s -- %s -- %s -- %s -- %s" % (row['id_num'], row['dns_address'], row['a_ip'], row['aaaa_ip'], row['mx_ip'], row['ns_ip'], row['txt_ip']))
# Function to show contents of table
# function to populate the table
def print_menu():
print (30 * '-')
print (" Manage the target DB for donuts.")
print (30 * '-')
print ("1. Display the current entries.")
print ("2. Enter a new target address.")
print ("3. Upload a file of address to add.")
print ("4. Remove an entry.")
print ("5. Reset the table completely.")
print ("6. Exit this menu")
print (30 * '-')
def modify_table():
loop = True
while loop:
print_menu()
choice = raw_input("Enter your choice: ")
choice = int(choice)
if choice == 1:
print ("Current entries in donuts:")
display_entries()
elif choice == 2:
new_entry = raw_input ("Enter the DNS name you'd like to track:")
add_entry(new_entry)
elif choice == 3:
new_file = raw_input ("Enter the filename to upload: ")
# validate the file exists and it can be read
# loop through the file and add each DNS name to the db
elif choice == 4:
del_entry = raw_input("Enter the DNS name or ID number to remove: ")
#check to see if integer or dns name was entered
#find the entry that matches, present it to the user for confirmation
# if confirmed, remove from the table, loop back to main menu
# if not confirmed, loop back to deletion choice
elif choice == 5:
setup_db()
elif choice == 6:
loop=False
else:
print ("Invalid entry, try again using a number 1-6.")
# FUnction to validate that entry is a valid DNS name
def validate_dns(address):
#only allow alphanumeric, dash, and period. All other characters are illegal
return True
def add_entry(address):
# validate DNS name, then perform a full lookup for supported DNS types
if validate_dns(address):
a_ip = []
aaaa_ip = []
mx_ip = []
ns_ip = []
soa_ip = []
txt_ip = []
test_resolver = dns.resolver.Resolver()
try:
a_ip_answer = test_resolver.query(address, "A")
except dns.resolver.NoAnswer:
print ("No answer received for A.")
a_ip_answer = ""
try:
aaaa_ip_answer = test_resolver.query(address, "AAAA")
except dns.resolver.NoAnswer:
print ("No answer received for AAAA.")
aaaa_ip_answer = ""
try:
mx_ip_answer = test_resolver.query(address, "MX")
except dns.resolver.NoAnswer:
print ("No answer received for MX.")
mx_ip_answer = ""
try:
ns_ip_answer = test_resolver.query(address, "NS")
except dns.resolver.NoAnswer:
print ("No answer received for NS.")
ns_ip_answer = ""
try:
soa_ip_answer = test_resolver.query(address, "SOA")
except dns.resolver.NoAnswer:
print ("No answer received for SOA.")
soa_ip_answer = ""
try:
txt_ip_answer = test_resolver.query(address, "TXT")
except dns.resolver.NoAnswer:
print ("No answer received for TXT.")
txt_ip_answer = ""
for rdata in a_ip_answer:
a_ip.append(rdata.address)
#print a_ip
for rdata in aaaa_ip_answer:
aaaa_ip.append(rdata.address)
#print aaaa_ip
for rdata in mx_ip_answer:
mx_ip.append(rdata.exchange)
#print mx_ip
for rdata in ns_ip_answer:
ns_ip.append(rdata.target)
#print ns_ip
for rdata in soa_ip_answer:
soa_ip.append(rdata.rname)
#print soa_ip
for rdata in txt_ip_answer:
txt_ip.append(rdata.strings)
#print txt_ip
a = str(a_ip)
aaaa = str(aaaa_ip)
mx = str(mx_ip)
ns = str(ns_ip)
soa = str(soa_ip)
txt = str(txt_ip)
params = (address, a, aaaa, mx, ns, soa, txt)
db_conn = sqlite3.connect(db_file)
with db_conn:
cur = db_conn.cursor()
cur.execute("INSERT INTO targets VALUES(NULL,?,?,?,?,?,?,?)",params)
else:
print ("The address you provided is not a valid DNS name.")
# Main function
modify_table()