-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add protection index, update attack models and backend logic
- Loading branch information
1 parent
2a4de4b
commit 0220746
Showing
13 changed files
with
645 additions
and
19 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
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,62 @@ | ||
import yargs from "yargs" | ||
import { randomBytes } from "crypto" | ||
import { AppDataSource } from "data-source" | ||
import { ApiEndpoint, Attack } from "models" | ||
import { AttackType } from "@common/enums" | ||
import { ATTACK_TYPE_TO_RISK_SCORE } from "@common/maps" | ||
import { DateTime } from "luxon" | ||
|
||
const randomDate = (start?: boolean) => { | ||
const startTime = start | ||
? DateTime.now().minus({ hours: 5 }).toJSDate().getTime() | ||
: DateTime.now().minus({ minutes: 50 }).toJSDate().getTime() | ||
const endTime = start | ||
? DateTime.now().minus({ hours: 1 }).toJSDate().getTime() | ||
: DateTime.now().toJSDate().getTime() | ||
return new Date(startTime + Math.random() * (endTime - startTime)) | ||
} | ||
|
||
const generateAttacks = async (numAttacks: number) => { | ||
const queryRunner = AppDataSource.createQueryRunner() | ||
await queryRunner.connect() | ||
try { | ||
const endpoints = await queryRunner.manager.find(ApiEndpoint, { | ||
select: { uuid: true, host: true }, | ||
}) | ||
const attackTypes = Object.keys(AttackType) | ||
const insertAttacks: Attack[] = [] | ||
for (let i = 0; i < numAttacks; i++) { | ||
const newAttack = new Attack() | ||
const randTypeNum = Math.floor(Math.random() * attackTypes.length) | ||
const randEndpointNum = Math.floor(Math.random() * endpoints.length) | ||
newAttack.attackType = AttackType[attackTypes[randTypeNum]] | ||
newAttack.riskScore = ATTACK_TYPE_TO_RISK_SCORE[newAttack.attackType] | ||
newAttack.description = `${newAttack.attackType} detected.` | ||
newAttack.startTime = randomDate(true) | ||
newAttack.endTime = randomDate() | ||
newAttack.uniqueSessionKey = randomBytes(16).toString("hex") | ||
newAttack.apiEndpointUuid = endpoints[randEndpointNum].uuid | ||
newAttack.host = endpoints[randEndpointNum].host | ||
insertAttacks.push(newAttack) | ||
} | ||
await queryRunner.manager.insert(Attack, insertAttacks) | ||
} catch (err) { | ||
console.error(`Encountered error while generating sample attacks: ${err}`) | ||
} finally { | ||
await queryRunner.release() | ||
} | ||
} | ||
|
||
const main = async () => { | ||
const datasource = await AppDataSource.initialize() | ||
if (!datasource.isInitialized) { | ||
console.error("Couldn't initialize datasource...") | ||
return | ||
} | ||
console.log("AppDataSource Initialized...") | ||
const args = yargs.argv | ||
const numAttacks = args["numAttacks"] ?? 20 | ||
await generateAttacks(numAttacks) | ||
} | ||
|
||
main() |
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
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,129 @@ | ||
import React from "react" | ||
import { | ||
Chart as ChartJS, | ||
ArcElement, | ||
Tooltip, | ||
Legend, | ||
ChartOptions, | ||
} from "chart.js" | ||
import { Doughnut } from "react-chartjs-2" | ||
import { | ||
HStack, | ||
StackProps, | ||
Grid, | ||
GridItem, | ||
Text, | ||
VStack, | ||
Box, | ||
StackDivider, | ||
} from "@chakra-ui/react" | ||
import { AttackType } from "@common/enums" | ||
import { PIE_BACKGROUND_COLORS, PIE_BORDER_COLORS } from "~/constants" | ||
|
||
ChartJS.register(ArcElement, Tooltip, Legend) | ||
|
||
interface AggAttackChartProps extends StackProps { | ||
attackTypeCount: Record<AttackType, number> | ||
totalAttacks: number | ||
totalEndpoints: number | ||
} | ||
|
||
export const AggAttackChart: React.FC<AggAttackChartProps> = React.memo( | ||
({ totalAttacks, totalEndpoints, attackTypeCount, ...props }) => { | ||
const data = Object.values(attackTypeCount) | ||
const labels = Object.keys(attackTypeCount) | ||
const chartData = { | ||
labels, | ||
datasets: [ | ||
{ | ||
data, | ||
backgroundColor: PIE_BACKGROUND_COLORS, | ||
borderColor: PIE_BORDER_COLORS, | ||
borderWidth: 1, | ||
}, | ||
], | ||
} | ||
const options = { | ||
responsive: true, | ||
cutout: "60%", | ||
plugins: { | ||
legend: { | ||
display: false, | ||
}, | ||
tooltip: { | ||
caretSize: 0, | ||
bodyFont: { | ||
size: 11, | ||
}, | ||
}, | ||
}, | ||
} as ChartOptions | ||
return ( | ||
<HStack | ||
overflow="hidden" | ||
alignItems="flex-start" | ||
bg="cellBG" | ||
spacing="0" | ||
divider={<StackDivider />} | ||
w="full" | ||
h="60" | ||
> | ||
<VStack divider={<StackDivider />} spacing="0" h="full"> | ||
<VStack | ||
bg="cellBG" | ||
py="4" | ||
spacing="1" | ||
px="20" | ||
h="50%" | ||
justifyContent="center" | ||
> | ||
<Text fontSize="xl" fontWeight="semibold" rounded="md"> | ||
{totalAttacks} | ||
</Text> | ||
<Text fontSize="sm" fontWeight="medium"> | ||
Attacks | ||
</Text> | ||
</VStack> | ||
<VStack | ||
bg="cellBG" | ||
py="4" | ||
spacing="1" | ||
h="50%" | ||
justifyContent="center" | ||
> | ||
<Text fontSize="xl" fontWeight="semibold" rounded="md"> | ||
{totalEndpoints} | ||
</Text> | ||
<Text fontSize="sm" fontWeight="medium"> | ||
Endpoints | ||
</Text> | ||
</VStack> | ||
</VStack> | ||
<HStack spacing="12" flexGrow="1" alignItems="center" p="4" h="full"> | ||
<Box w="220px"> | ||
<Doughnut options={options} data={chartData} /> | ||
</Box> | ||
<VStack | ||
display={{ base: "none", md: "inherit" }} | ||
flexGrow="1" | ||
alignItems="flex-start" | ||
spacing="4" | ||
h="full" | ||
py="8" | ||
> | ||
<Grid templateColumns="repeat(2, 1fr)" gap="4"> | ||
{labels.map((e, i) => ( | ||
<GridItem key={i}> | ||
<HStack> | ||
<Box bg={PIE_BACKGROUND_COLORS[i]} px="2" py="1" /> | ||
<Text fontSize="sm">{e}</Text> | ||
</HStack> | ||
</GridItem> | ||
))} | ||
</Grid> | ||
</VStack> | ||
</HStack> | ||
</HStack> | ||
) | ||
}, | ||
) |
Oops, something went wrong.