Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
C
CUREX Distr
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alberto Blázquez Herranz
CUREX Distr
Commits
9a9cc6ad
Commit
9a9cc6ad
authored
Oct 06, 2020
by
David Fernandez Lobon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Algoritmo con Ventanas finalizado.
parent
9b84612f
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
145 additions
and
73 deletions
+145
-73
src/main/java/adt/newportdetection/NewPortDetectionThread.java
...ain/java/adt/newportdetection/NewPortDetectionThread.java
+11
-7
src/main/java/adt/newportdetection/runner/NewPortDetection.java
...in/java/adt/newportdetection/runner/NewPortDetection.java
+8
-26
src/main/java/adt/newportdetection/runner/PortDetection.java
src/main/java/adt/newportdetection/runner/PortDetection.java
+1
-1
src/main/java/adt/newportdetection/runner/ProcessWindows.java
...main/java/adt/newportdetection/runner/ProcessWindows.java
+106
-21
src/main/java/adt/service/OnStartupService.java
src/main/java/adt/service/OnStartupService.java
+19
-18
No files found.
src/main/java/adt/newportdetection/NewPortDetectionThread.java
View file @
9a9cc6ad
...
...
@@ -8,6 +8,7 @@ import org.springframework.stereotype.Service;
import
java.io.BufferedReader
;
import
java.io.IOException
;
import
java.util.concurrent.Semaphore
;
import
java.util.concurrent.atomic.AtomicBoolean
;
...
...
@@ -16,22 +17,25 @@ import java.util.concurrent.atomic.AtomicBoolean;
public
class
NewPortDetectionThread
extends
Thread
{
private
ListPortDetection
listPortDetection
;
private
NewPortDetection
newPortDetection
;
private
static
int
N_THREADS
=
1
;
private
final
AtomicBoolean
running
=
new
AtomicBoolean
(
true
);
private
static
Semaphore
mutex
;
public
NewPortDetectionThread
(
ListPortDetection
listPortDetection
){
this
.
listPortDetection
=
listPortDetection
;
this
.
newPortDetection
=
new
NewPortDetection
(
this
.
listPortDetection
);
this
.
newPortDetection
=
new
NewPortDetection
(
this
.
listPortDetection
,
N_THREADS
);
mutex
=
new
Semaphore
(
1
);
}
public
void
terminate
()
{
running
.
set
(
false
);
public
void
parar
()
throws
InterruptedException
{
mutex
.
wait
();
}
public
void
activar
()
{
mutex
.
notify
();
}
@Override
public
void
run
(){
try
{
while
(
this
.
running
.
get
()){
newPortDetection
.
readLinesSnif
();
log
.
info
(
"==========================================="
);
}
newPortDetection
.
readLinesSnif
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
...
...
src/main/java/adt/newportdetection/runner/NewPortDetection.java
View file @
9a9cc6ad
...
...
@@ -13,40 +13,22 @@ import java.util.regex.Pattern;
@Slf4j
public
class
NewPortDetection
{
private
ListPortDetection
listPortDetection
;
private
final
int
n_threads
;
//Constructor
public
NewPortDetection
(
ListPortDetection
listPortDetection
){
public
NewPortDetection
(
ListPortDetection
listPortDetection
,
int
n_threads
){
this
.
n_threads
=
n_threads
;
this
.
listPortDetection
=
listPortDetection
;
}
public
void
readLinesSnif
()
throws
IOException
{
Process
process
=
new
ProcessBuilder
(
"tcpdump"
,
"-l"
,
"-nn"
,
"-i"
,
"any"
,
"-c10"
,
"ip"
).
start
();
Process
process
=
new
ProcessBuilder
(
"tcpdump"
,
"-l"
,
"-nn"
,
"-i"
,
"any"
,
"ip"
).
start
();
BufferedReader
br
=
null
;
try
{
//tried different numbers for BufferedReader's last parameter
br
=
new
BufferedReader
(
new
InputStreamReader
(
process
.
getInputStream
()));
String
line
=
null
;
Pattern
pattern
=
Pattern
.
compile
(
"([0-9]+:[0-9]+:[0-9]+.[0-9]+)\\sIP\\s([0-9]+.[0-9]+.[0-9]+.[0-9]+).([0-9]+) > ([0-9]+.[0-9]+.[0-9]+.[0-9]+).([0-9]+): .* length ([0-9]+)"
);
while
(((
line
=
br
.
readLine
())
!=
null
))
{
Matcher
matcher
=
pattern
.
matcher
(
line
);
if
(
matcher
.
find
()){
String
time
=
matcher
.
group
(
1
);
String
source
=
matcher
.
group
(
2
);
String
destination
=
matcher
.
group
(
4
);
int
portS
=
Integer
.
parseInt
(
matcher
.
group
(
3
));
int
portD
=
Integer
.
parseInt
(
matcher
.
group
(
5
));
int
length
=
Integer
.
parseInt
(
matcher
.
group
(
6
));
if
(
localhosts
.
containsKey
(
source
)){
listPortDetection
.
addNewPort
(
new
PortDetection
(
time
,
source
,
portS
,
length
));
log
.
info
(
"Ip: "
+
source
+
" port: "
+
portS
);
// log.info(listPortDetection.toString());
}
if
(
localhosts
.
containsKey
(
destination
)){
listPortDetection
.
addNewPort
(
new
PortDetection
(
time
,
destination
,
portD
,
length
));
log
.
info
(
"Ip: "
+
destination
+
" port: "
+
portD
);
// log.info(listPortDetection.toString());
}
}
ProcessWindows
[]
threads
=
new
ProcessWindows
[
n_threads
];
for
(
int
i
=
0
;
i
<
n_threads
;
i
++){
threads
[
i
]
=
new
ProcessWindows
(
br
,
listPortDetection
);
threads
[
i
].
start
();
}
}
catch
(
Exception
e
){
e
.
printStackTrace
();
...
...
src/main/java/adt/newportdetection/runner/PortDetection.java
View file @
9a9cc6ad
...
...
@@ -43,7 +43,7 @@ public class PortDetection {
"time='"
+
time
+
'\''
+
", ip='"
+
ip
+
'\''
+
", port="
+
port
+
", length="
+
length
+
", length="
+
length
+
'}'
;
}
}
...
...
src/main/java/adt/newportdetection/runner/ProcessWindows.java
View file @
9a9cc6ad
package
adt
.
newportdetection
.
runner
;
import
javafx.util.Pair
;
import
sun.dc.pr.PRError
;
import
lombok.extern.slf4j.Slf4j
;
import
java.io.BufferedReader
;
import
java.io.IOException
;
import
java.io.InputStreamReader
;
import
java.util.*
;
import
java.util.concurrent.Semaphore
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
@Slf4j
public
class
ProcessWindows
extends
Thread
{
private
static
int
N
=
4
;
private
static
double
width_window
=
0.5
;
private
static
int
porcentaje
=
75
;
private
static
ArrayList
<
Map
<
Pair
<
String
,
Integer
>,
PortDetection
>>
windows
=
new
ArrayList
<>(
N
)
;
private
static
ArrayList
<
Map
<
Pair
<
String
,
Integer
>,
PortDetection
>>
windows
;
private
static
double
[]
tiempos_mayores
;
private
static
BufferedReader
buffer
;
private
static
double
tiempo_limite_superior
;
...
...
@@ -23,10 +25,20 @@ public class ProcessWindows extends Thread{
private
static
Pattern
pattern
;
private
static
Map
<
String
,
String
>
localhosts
;
private
static
ListPortDetection
listPortDetection
;
private
static
Semaphore
mutex_windows
;
private
static
Semaphore
mutex_buffer
;
/* ====================================
* Constructor
* ====================================
* */
public
ProcessWindows
(
BufferedReader
buffer
,
ListPortDetection
listPortDetection
)
{
ProcessWindows
.
mutex_windows
=
new
Semaphore
(
1
);
ProcessWindows
.
mutex_buffer
=
new
Semaphore
(
1
);
ProcessWindows
.
tiempos_mayores
=
new
double
[
N
];
for
(
int
i
=
0
;
i
<
windows
.
size
();
i
++){
ProcessWindows
.
windows
=
new
ArrayList
<>(
N
);
for
(
int
i
=
0
;
i
<
N
;
i
++){
ProcessWindows
.
windows
.
add
(
i
,
new
HashMap
<>());
}
ProcessWindows
.
buffer
=
buffer
;
...
...
@@ -38,6 +50,11 @@ public class ProcessWindows extends Thread{
ProcessWindows
.
ventana_primera
=
0
;
}
/* ====================================
* Metodo que obtiene las ips de las diferentes maquinas
* ====================================
* */
private
Map
<
String
,
String
>
getIpHost
()
{
Map
<
String
,
String
>
resultado
=
new
HashMap
<>();
try
{
...
...
@@ -61,10 +78,26 @@ public class ProcessWindows extends Thread{
return
resultado
;
}
private
String
readLine
()
throws
IOException
{
return
buffer
.
readLine
();
/* ====================================
* Metodo que lee una linea del buffer
* ====================================
* */
private
String
readLine
()
throws
IOException
,
InterruptedException
{
String
line
;
mutex_buffer
.
acquire
();
line
=
buffer
.
readLine
();
mutex_buffer
.
release
();
return
line
;
}
private
void
addWindows
(
PortDetection
portDetection
){
/* ====================================
* Metodo Principal del algoritmo el cual llama a todas las funciones
* intermedias del Algoritmo de Ventanas
* ====================================
* */
private
void
addWindows
(
PortDetection
portDetection
)
throws
InterruptedException
{
// Inicializacion
double
time
=
this
.
getSeconds
(
portDetection
);
if
(
tiempo_limite_superior
==
-
1
){
...
...
@@ -86,21 +119,32 @@ public class ProcessWindows extends Thread{
}
else
{
if
(
tiempos_mayores
[
ventana_actual
]
<
time
){
// Pasa a meterse en la ventana siguiente
ventana_actual
++;
if
(
ventana_actual
==
N
-
1
){
ventana_actual
=
0
;
}
else
{
ventana_actual
++;
}
}
//TODO Falta hacer que Arraylist sea syncronizado
mutex_windows
.
acquire
();
windows
.
get
(
ventana_actual
).
put
(
new
Pair
<>(
portDetection
.
getIp
(),
portDetection
.
getPort
()),
portDetection
);
mutex_windows
.
release
();
}
}
private
void
desbordamiento
(
PortDetection
portDetection
){
/* ====================================
* Metodo que se encarga de realizar las accciones
* cuando llega un paquete que no esta en las actuales
* ventanas y resolver este problema
* ====================================
* */
private
void
desbordamiento
(
PortDetection
portDetection
)
throws
InterruptedException
{
int
count
=
0
;
int
i
=
ventana_primera
;
HashSet
<
Pair
<
String
,
Integer
>>
claves_primera
=
(
HashSet
<
Pair
<
String
,
Integer
>>)
windows
.
get
(
ventana_primera
).
keySet
();
Set
<
Pair
<
String
,
Integer
>>
claves_primera
=
windows
.
get
(
ventana_primera
).
keySet
();
for
(
Pair
<
String
,
Integer
>
clave
:
claves_primera
)
{
int
contador
=
0
;
while
(
count
<
N
-
1
)
{
if
(
i
==
N
)
i
=
0
;
HashSet
<
Pair
<
String
,
Integer
>>
claves
=
(
HashSet
<
Pair
<
String
,
Integer
>>)
windows
.
get
(
i
).
keySet
();
Set
<
Pair
<
String
,
Integer
>>
claves
=
windows
.
get
(
i
).
keySet
();
if
(
claves
.
contains
(
clave
))
{
contador
++;
}
...
...
@@ -110,27 +154,62 @@ public class ProcessWindows extends Thread{
if
((
contador
*
100
/
N
)
>=
porcentaje
){
// Si puerto supera el porcentaje, se añade a la lista de PortDetection
listPortDetection
.
addNewPort
(
windows
.
get
(
ventana_primera
).
get
(
clave
));
// log.info("=====================================================");
// log.info("windows: " + windows.toString());
// log.info("ventana_primera: " + ventana_primera + "");
// log.info("ventana_actual: " + ventana_actual + "");
// log.info("tiempos_mayores: " + Arrays.toString(tiempos_mayores));
// log.info("tiempo_limite_superior: " + tiempo_limite_superior +"");
log
.
info
(
listPortDetection
.
toString
());
// log.info("===========================
// ==========================");
}
// log.info("=====================================================");
// log.info("windows: " + windows.toString());
// log.info("ventana_primera: " + ventana_primera + "");
// log.info("ventana_actual: " + ventana_actual + "");
// log.info("tiempos_mayores: " + Arrays.toString(tiempos_mayores));
// log.info("tiempo_limite_superior: " + tiempo_limite_superior +"");
// log.info(listPortDetection.toString());
// log.info("=====================================================");
}
// Vaciar la ventana_primera
windows
.
get
(
ventana_primera
).
clear
();
windows
.
remove
(
ventana_primera
);
windows
.
add
(
ventana_primera
,
new
HashMap
<>());
if
(
ventana_primera
==
0
)
{
tiempos_mayores
[
ventana_primera
]
=
tiempos_mayores
[
N
-
1
]
+
width_window
;
}
else
{
tiempos_mayores
[
ventana_primera
]
=
tiempos_mayores
[
ventana_primera
-
1
]
+
width_window
;
}
ventana_primera
++;
if
(
ventana_primera
==
N
-
1
){
ventana_primera
=
0
;
}
else
{
ventana_primera
++;
}
tiempo_limite_superior
+=
width_window
;
if
(
ventana_actual
==
N
-
1
){
ventana_actual
=
0
;
}
else
ventana_actual
=
ventana_actual
++;
}
else
ventana_actual
++;
mutex_windows
.
acquire
();
windows
.
get
(
ventana_actual
).
put
(
new
Pair
<>(
portDetection
.
getIp
(),
portDetection
.
getPort
()),
portDetection
);
mutex_windows
.
release
();
}
/* ====================================
* Metodo que obtiene los segundos a partir del un objeto Portdetection
* ====================================
* */
private
double
getSeconds
(
PortDetection
portDetection
){
String
[]
time
=
portDetection
.
getTime
().
split
(
":"
);
return
Integer
.
parseInt
(
time
[
0
])
*
3600
+
Integer
.
parseInt
(
time
[
1
])
*
60
+
Double
.
parseDouble
(
time
[
2
]);
}
/* ====================================
* Metodo que realiza la comprobacion de si la linea leida del buffer
* es una linea que tiene las caracteristicas propias para ser añadida
* a las ventanas
* ====================================
* */
private
ArrayList
<
PortDetection
>
match
(
String
line
)
{
Matcher
matcher
=
pattern
.
matcher
(
line
);
ArrayList
<
PortDetection
>
resultado
=
new
ArrayList
<>();
...
...
@@ -151,6 +230,11 @@ public class ProcessWindows extends Thread{
}
return
resultado
;
}
/* ====================================
* Metodo que se llama cuando se inicia el Thread
* ====================================
* */
@Override
public
void
run
()
{
try
{
...
...
@@ -158,19 +242,20 @@ public class ProcessWindows extends Thread{
while
(
(
line
=
readLine
())
!=
null
){
ArrayList
<
PortDetection
>
meter
=
match
(
line
);
PortDetection
aux
=
null
;
if
(
(
aux
=
meter
.
get
(
0
))
!=
null
)
addWindows
(
aux
);
if
(
(
aux
=
meter
.
get
(
1
))
!=
null
)
addWindows
(
aux
);
for
(
PortDetection
portDetection
:
meter
)
{
addWindows
(
portDetection
);
}
}
}
catch
(
IOException
e
){
}
catch
(
IOException
|
InterruptedException
e
){
e
.
printStackTrace
();
}
}
/*
*
/* ====================================
* GETTERS AND SETTERS
*
*
====================================
* */
public
static
int
getN
()
{
return
N
;
...
...
src/main/java/adt/service/OnStartupService.java
View file @
9a9cc6ad
...
...
@@ -91,26 +91,26 @@ public class OnStartupService implements ApplicationListener<ApplicationReadyEve
Node
node
=
initialize
();
List
<
String
[]>
vlanIps
=
new
ArrayList
<
String
[]>();
for
(
Vlan
vlan:
node
.
getVlan
()){
String
[]
toAdd
=
{
vlan
.
getNetAddress
(),
vlan
.
getVlanName
()};
vlanIps
.
add
(
toAdd
);
}
//
List<String[]> vlanIps = new ArrayList<String[]>();
//
//
for(Vlan vlan: node.getVlan()){
//
//
String [] toAdd = {vlan.getNetAddress(), vlan.getVlanName()};
//
vlanIps.add(toAdd);
//
}
try
{
launchNewPortsDetection
();
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
launchDHCPDiscovery
(
vlanIps
,
node
.
getIp
());
vlanRepository
.
saveAll
(
node
.
getVlan
());
sendAssetService
.
sendAssets
(
true
,
null
);
//, "scan");
scheduleService
.
scheduleSetUp
(
node
);
//
launchDHCPDiscovery(vlanIps, node.getIp());
//
//
vlanRepository.saveAll(node.getVlan());
//
//
sendAssetService.sendAssets(true, null);//, "scan");
//
//
scheduleService.scheduleSetUp(node);
}
...
...
@@ -144,10 +144,11 @@ public class OnStartupService implements ApplicationListener<ApplicationReadyEve
private
void
launchNewPortsDetection
()
throws
InterruptedException
{
NewPortDetectionThread
newPortDetectionThread
=
new
NewPortDetectionThread
(
this
.
listPortDetection
);
newPortDetectionThread
.
start
();
Thread
.
sleep
(
10000
);
newPortDetectionThread
.
terminate
();
newPortDetectionThread
.
join
();
log
.
info
(
this
.
listPortDetection
.
toString
());
newPortDetectionThread
.
parar
();
// Thread.sleep(10000);
// newPortDetectionThread.terminate();
// newPortDetectionThread.join();
// log.info(this.listPortDetection.toString());
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment