-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update DNSTwist to use the pe-reports module and run on all P&E orgs #1988
Open
aloftus23
wants to merge
5
commits into
master
Choose a base branch
from
1968-pe-dnstwist
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3b246cb
Update DNSTwist to use the pe-reports module and run on all P&E orgs
aloftus23 ff3999f
remove dnstwist since it's installed in pe-reports
aloftus23 f462e2d
Merge branch 'master' into 1968-pe-dnstwist
aloftus23 9758d80
Update dnstwist test
aloftus23 eba430d
Merge branch 'master' into 1968-pe-dnstwist
actualeyes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -1,111 +1,17 @@ | ||
import { connectToDatabase, Domain, Vulnerability } from '../models'; | ||
import getRootDomains from './helpers/getRootDomains'; | ||
import { CommandOptions } from './ecs-client'; | ||
import { plainToClass } from 'class-transformer'; | ||
import saveVulnerabilitiesToDb from './helpers/saveVulnerabilitiesToDb'; | ||
import { spawnSync } from 'child_process'; | ||
import saveDomainsToDb from './helpers/saveDomainsToDb'; | ||
import * as dns from 'dns'; | ||
|
||
async function runDNSTwist(domain: string) { | ||
const child = spawnSync( | ||
'dnstwist', | ||
['-r', '--tld', './worker/common_tlds.dict', '-f', 'json', domain], | ||
{ | ||
stdio: 'pipe', | ||
encoding: 'utf-8' | ||
} | ||
); | ||
const savedOutput = child.stdout; | ||
const finalResults = JSON.parse(savedOutput); | ||
console.log( | ||
`Got ${ | ||
Object.keys(finalResults).length | ||
} similar domains for root domain ${domain}` | ||
); | ||
return finalResults; | ||
} | ||
import { getPeEnv } from './helpers/getPeEnv'; | ||
|
||
export const handler = async (commandOptions: CommandOptions) => { | ||
const { organizationId, organizationName } = commandOptions; | ||
await connectToDatabase(); | ||
const dateNow = new Date(Date.now()); | ||
console.log('Running dnstwist on organization', organizationName); | ||
const root_domains = await getRootDomains(organizationId!); | ||
const vulns: Vulnerability[] = []; | ||
console.log('Root domains:', root_domains); | ||
for (const root_domain of root_domains) { | ||
try { | ||
const results = await runDNSTwist(root_domain); | ||
|
||
// Fetch existing domain object | ||
let domain = await Domain.findOne({ | ||
organization: { id: organizationId }, | ||
name: root_domain | ||
}); | ||
|
||
if (!domain) { | ||
let ipAddress; | ||
const new_domain: Domain[] = []; | ||
try { | ||
ipAddress = (await dns.promises.lookup(root_domain)).address; | ||
} catch (e) { | ||
ipAddress = null; | ||
} | ||
new_domain.push( | ||
plainToClass(Domain, { | ||
name: root_domain, | ||
ip: ipAddress, | ||
organization: { id: organizationId } | ||
}) | ||
); | ||
await saveDomainsToDb(new_domain); | ||
domain = await Domain.findOne({ | ||
organization: { id: organizationId }, | ||
name: root_domain | ||
}); | ||
} | ||
|
||
// Fetch existing dnstwist vulnerability | ||
const existingVuln = await Vulnerability.findOne({ | ||
domain: { id: domain?.id }, | ||
source: 'dnstwist' | ||
}); | ||
console.log('Running dnstwist on P&E organizations'); | ||
|
||
// Map date-first-observed to any domain-name that already exists | ||
const existingVulnsMap = {}; | ||
if (existingVuln) { | ||
for (const domain of existingVuln.structuredData['domains']) { | ||
existingVulnsMap[domain['domain']] = domain['date_first_observed']; | ||
} | ||
} | ||
// If an existing domain-name is in the new results, set date-first-observed to the mapped value | ||
// Else, set date-first-observed to today's date (dateNow) | ||
for (const domain of results) { | ||
domain['date_first_observed'] = | ||
existingVulnsMap[domain['domain']] || dateNow; | ||
} | ||
if (Object.keys(results).length !== 0) { | ||
vulns.push( | ||
plainToClass(Vulnerability, { | ||
domain: domain, | ||
lastSeen: new Date(Date.now()), | ||
title: 'DNS Twist Domains', | ||
state: 'open', | ||
source: 'dnstwist', | ||
severity: 'Low', | ||
needsPopulation: false, | ||
structuredData: { domains: results }, | ||
description: `Registered domains similar to ${root_domain}.` | ||
}) | ||
); | ||
await saveVulnerabilitiesToDb(vulns, false); | ||
} else { | ||
continue; | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
continue; | ||
} | ||
} | ||
const child = spawnSync('python', ['-m', 'pe_source', 'dnstwist'], { | ||
stdio: 'inherit', | ||
encoding: 'utf-8', | ||
env: { | ||
...getPeEnv() | ||
}, | ||
cwd: '/app/pe-reports' | ||
}); | ||
console.log('Done'); | ||
}; |
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,7 @@ | ||
// Gets PE environment variables to send to PE scripts. | ||
export const getPeEnv = () => ({ | ||
DB_HOST: process.env.DB_HOST!, | ||
PE_DB_NAME: process.env.PE_DB_NAME!, | ||
PE_DB_USERNAME: process.env.PE_DB_USERNAME!, | ||
PE_DB_PASSWORD: process.env.PE_DB_PASSWORD! | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should remove dnstwist from dockerfile