Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
C
CTF
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
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
Grzegorz
CTF
Commits
b7a9072c
Commit
b7a9072c
authored
Mar 19, 2016
by
Grzegorz Pietrusza
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add flag finding by hash
parent
b9e30363
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
107 additions
and
45 deletions
+107
-45
configuration.yml
service/configuration.yml
+5
-4
Team.java
...in/java/com/telephoners/krakyournet/ctf/objects/Team.java
+25
-3
Task.java
...a/com/telephoners/krakyournet/ctf/objects/tasks/Task.java
+1
-0
SolutionsRepository.java
...ers/krakyournet/ctf/repositories/SolutionsRepository.java
+20
-1
TasksRepository.java
...phoners/krakyournet/ctf/repositories/TasksRepository.java
+54
-35
SolutionsResource.java
...ephoners/krakyournet/ctf/resources/SolutionsResource.java
+2
-2
No files found.
service/configuration.yml
View file @
b7a9072c
...
@@ -5,6 +5,7 @@ server:
...
@@ -5,6 +5,7 @@ server:
port
:
8080
port
:
8080
dbHost
:
46.4.242.141
dbHost
:
46.4.242.141
#dbHost: 127.0.0.1
dbPort
:
27017
dbPort
:
27017
dbName
:
db
dbName
:
db
...
@@ -66,7 +67,7 @@ teams:
...
@@ -66,7 +67,7 @@ teams:
textTasks
:
textTasks
:
-
name
:
"
Szyfro1"
-
name
:
"
Szyfro1"
text
:
"
Odszyfruj1"
text
:
"
Odszyfruj1"
level
:
2
level
:
1
flags
:
flags
:
-
value
:
"
sdfg1f"
-
value
:
"
sdfg1f"
description
:
"
flaga
1"
description
:
"
flaga
1"
...
@@ -100,9 +101,9 @@ webTasks:
...
@@ -100,9 +101,9 @@ webTasks:
url
:
"
http://wikipedia.pl"
url
:
"
http://wikipedia.pl"
level
:
4
level
:
4
flags
:
flags
:
-
value
:
"
sdfg1fs"
-
value
:
"
sdf
ff
g1fs"
description
:
"
flaga
11"
description
:
"
flaga
11"
-
value
:
"
sdfdg1f"
-
value
:
"
s
ff
dfdg1f"
description
:
"
flaga
22"
description
:
"
flaga
22"
-
value
:
"
sfdfgf1"
-
value
:
"
s
dfass
fdfgf1"
description
:
"
flaga
33"
description
:
"
flaga
33"
service/src/main/java/com/telephoners/krakyournet/ctf/objects/Team.java
View file @
b7a9072c
...
@@ -9,9 +9,6 @@ import org.mongodb.morphia.annotations.Reference;
...
@@ -9,9 +9,6 @@ import org.mongodb.morphia.annotations.Reference;
import
java.util.List
;
import
java.util.List
;
/**
* Created by gpietrus on 20.02.2016.
*/
@Entity
(
"teams"
)
@Entity
(
"teams"
)
public
class
Team
{
public
class
Team
{
@Id
@Id
...
@@ -53,4 +50,29 @@ public class Team {
...
@@ -53,4 +50,29 @@ public class Team {
public
List
<
User
>
getMembers
()
{
public
List
<
User
>
getMembers
()
{
return
members
;
return
members
;
}
}
@Override
public
boolean
equals
(
Object
o
)
{
if
(
this
==
o
)
return
true
;
if
(
o
==
null
||
getClass
()
!=
o
.
getClass
())
return
false
;
Team
team
=
(
Team
)
o
;
if
(
id
!=
null
?
!
id
.
equals
(
team
.
id
)
:
team
.
id
!=
null
)
return
false
;
if
(
name
!=
null
?
!
name
.
equals
(
team
.
name
)
:
team
.
name
!=
null
)
return
false
;
if
(
description
!=
null
?
!
description
.
equals
(
team
.
description
)
:
team
.
description
!=
null
)
return
false
;
return
members
!=
null
?
members
.
equals
(
team
.
members
)
:
team
.
members
==
null
;
}
@Override
public
int
hashCode
()
{
int
result
=
id
!=
null
?
id
.
hashCode
()
:
0
;
result
=
31
*
result
+
(
name
!=
null
?
name
.
hashCode
()
:
0
);
result
=
31
*
result
+
(
description
!=
null
?
description
.
hashCode
()
:
0
);
result
=
31
*
result
+
(
members
!=
null
?
members
.
hashCode
()
:
0
);
return
result
;
}
}
}
service/src/main/java/com/telephoners/krakyournet/ctf/objects/tasks/Task.java
View file @
b7a9072c
...
@@ -15,6 +15,7 @@ import java.util.List;
...
@@ -15,6 +15,7 @@ import java.util.List;
public
abstract
class
Task
public
abstract
class
Task
{
{
@Id
@Id
//todo: use id to identify task?
protected
ObjectId
id
;
protected
ObjectId
id
;
@PublicProperty
@PublicProperty
protected
String
name
;
protected
String
name
;
...
...
service/src/main/java/com/telephoners/krakyournet/ctf/repositories/SolutionsRepository.java
View file @
b7a9072c
...
@@ -7,6 +7,8 @@ import org.mongodb.morphia.Datastore;
...
@@ -7,6 +7,8 @@ import org.mongodb.morphia.Datastore;
import
javax.inject.Inject
;
import
javax.inject.Inject
;
import
javax.inject.Singleton
;
import
javax.inject.Singleton
;
import
java.util.List
;
import
java.util.List
;
import
java.util.function.Predicate
;
import
java.util.stream.Collectors
;
@Singleton
@Singleton
public
class
SolutionsRepository
implements
Repository
public
class
SolutionsRepository
implements
Repository
...
@@ -33,6 +35,23 @@ public class SolutionsRepository implements Repository
...
@@ -33,6 +35,23 @@ public class SolutionsRepository implements Repository
public
List
<
Solution
>
getByTeam
(
Team
team
)
public
List
<
Solution
>
getByTeam
(
Team
team
)
{
{
//todo: merge with upper
//todo: merge with upper
return
datastore
.
createQuery
(
Solution
.
class
).
filter
(
"team"
,
team
).
asList
();
//todo: use morphia filter
// List<Solution> solutions = datastore.createQuery(Solution.class).asList();
List
<
Solution
>
solutions
=
datastore
.
createQuery
(
Solution
.
class
)
// .filter("team", team)
.
asList
();
List
<
Solution
>
collect
=
solutions
.
stream
()
.
filter
(
new
Predicate
<
Solution
>()
{
@Override
public
boolean
test
(
Solution
solution
)
{
return
solution
.
getTeam
().
equals
(
team
);
}
})
.
collect
(
Collectors
.
toList
());
return
collect
;
// return datastore.createQuery(Solution.class).filter("team", team).asList();
//todo: group by task
}
}
}
}
\ No newline at end of file
service/src/main/java/com/telephoners/krakyournet/ctf/repositories/TasksRepository.java
View file @
b7a9072c
...
@@ -7,6 +7,7 @@ import com.telephoners.krakyournet.ctf.objects.Solution;
...
@@ -7,6 +7,7 @@ import com.telephoners.krakyournet.ctf.objects.Solution;
import
com.telephoners.krakyournet.ctf.objects.Team
;
import
com.telephoners.krakyournet.ctf.objects.Team
;
import
com.telephoners.krakyournet.ctf.objects.User
;
import
com.telephoners.krakyournet.ctf.objects.User
;
import
com.telephoners.krakyournet.ctf.objects.tasks.Task
;
import
com.telephoners.krakyournet.ctf.objects.tasks.Task
;
import
javafx.util.Pair
;
import
org.apache.commons.codec.binary.Hex
;
import
org.apache.commons.codec.binary.Hex
;
import
org.mongodb.morphia.Datastore
;
import
org.mongodb.morphia.Datastore
;
...
@@ -80,49 +81,61 @@ public class TasksRepository implements Repository
...
@@ -80,49 +81,61 @@ public class TasksRepository implements Repository
{
{
return
this
.
getAll
().
stream
()
return
this
.
getAll
().
stream
()
.
collect
(
Collectors
.
toMap
(
.
collect
(
Collectors
.
toMap
(
task
->
task
.
getFlags
().
stream
()
task
->
{
List
<
String
>
collect
=
task
.
getFlags
().
stream
()
.
map
(
flag
->
calculateHashValue
(
username
,
flag
.
getValue
()))
.
map
(
flag
->
calculateHashValue
(
username
,
flag
.
getValue
()))
.
collect
(
Collectors
.
toList
()),
.
collect
(
Collectors
.
toList
());
return
collect
;
},
Task:
:
getLevel
Task:
:
getLevel
));
));
}
}
private
Optional
<
Task
>
getTaskFlagByHashValue
(
User
user
,
String
userHash
)
//todo: refactor with the function below
private
Optional
<
Pair
<
Task
,
Flag
>>
getTaskFlagPairByHashValue
(
User
user
,
String
userHash
)
{
{
String
username
=
user
.
getName
();
String
username
=
user
.
getName
();
//todo: inline
//todo: collapse lambdas
//todo: collapse lambdas
List
<
Task
>
tasks
=
this
.
getAll
();
Optional
<
Pair
<
Task
,
Flag
>>
matched
=
this
.
getAll
().
stream
()
Optional
<
Task
>
task
=
tasks
.
stream
()
.
collect
(
Collectors
.
toMap
(
.
filter
(
new
Predicate
<
Task
>()
task
->
task
,
{
Task:
:
getFlags
@Override
))
public
boolean
test
(
Task
task
)
.
entrySet
()
{
return
task
.
getFlags
()
.
stream
()
.
stream
()
.
map
(
new
Function
<
Flag
,
String
>()
.
map
((
Function
<
Map
.
Entry
<
Task
,
List
<
Flag
>>,
Pair
<
Task
,
Optional
<
Flag
>>>)
taskFlagsEntry
->
{
Task
task
=
taskFlagsEntry
.
getKey
();
Optional
<
Flag
>
matchedFlag
=
taskFlagsEntry
.
getValue
().
stream
()
.
filter
(
new
Predicate
<
Flag
>()
{
{
@Override
@Override
public
String
apply
(
Flag
flag
)
public
boolean
test
(
Flag
flag1
)
{
{
return
calculateHashValue
(
username
,
flag
.
getValue
()
);
return
calculateHashValue
(
username
,
flag1
.
getValue
()).
equals
(
userHash
);
}
}
})
})
.
filter
(
new
Predicate
<
String
>()
.
findFirst
();
return
new
Pair
<
Task
,
Optional
<
Flag
>>(
task
,
matchedFlag
);
})
.
filter
(
new
Predicate
<
Pair
<
Task
,
Optional
<
Flag
>>>()
{
{
@Override
@Override
public
boolean
test
(
String
flagValue
)
public
boolean
test
(
Pair
<
Task
,
Optional
<
Flag
>>
taskOptionalPair
)
{
{
return
flagValue
.
equals
(
userHash
);
return
taskOptionalPair
.
getValue
().
isPresent
(
);
}
}
})
})
.
findFirst
()
.
map
(
new
Function
<
Pair
<
Task
,
Optional
<
Flag
>>,
Pair
<
Task
,
Flag
>>()
.
isPresent
();
{
@Override
public
Pair
<
Task
,
Flag
>
apply
(
Pair
<
Task
,
Optional
<
Flag
>>
taskOptionalPair
)
{
return
new
Pair
<
Task
,
Flag
>(
taskOptionalPair
.
getKey
(),
taskOptionalPair
.
getValue
().
get
());
}
}
})
})
.
findFirst
();
.
findFirst
();
return
task
;
return
matched
;
//todo: refactor
}
}
public
String
calculateHashValue
(
String
username
,
String
flagValue
)
public
String
calculateHashValue
(
String
username
,
String
flagValue
)
...
@@ -138,14 +151,20 @@ public class TasksRepository implements Repository
...
@@ -138,14 +151,20 @@ public class TasksRepository implements Repository
return
encodedHash
;
return
encodedHash
;
}
}
public
boolean
check
Flag
(
User
user
,
String
hashValue
)
public
boolean
check
Hash
(
User
user
,
String
hashValue
)
{
{
//todo: refactor
Optional
<
Pair
<
Task
,
Flag
>>
taskFlagPairOptional
=
getTaskFlagPairByHashValue
(
user
,
hashValue
);
if
(!
taskFlagPairOptional
.
isPresent
())
{
return
false
;
}
Optional
<
Task
>
task
=
getTaskFlagByHashValue
(
user
,
hashValue
);
Pair
<
Task
,
Flag
>
taskFlagPair
=
taskFlagPairOptional
.
get
();
// Optional<Task> task = getByUserFlag(username, hashValue);
Task
task
=
taskFlagPair
.
getKey
();
Flag
flag
=
taskFlagPair
.
getValue
();
Optional
<
Team
>
team
=
teamsRepository
.
getTeamByUser
(
user
);
Optional
<
Team
>
team
=
teamsRepository
.
getTeamByUser
(
user
);
if
(
t
ask
.
isPresent
()
&&
t
eam
.
isPresent
())
{
if
(
team
.
isPresent
())
{
solutionsRepository
.
add
(
new
Solution
(
team
.
get
(),
task
.
get
(),
null
));
//todo
solutionsRepository
.
add
(
new
Solution
(
team
.
get
(),
task
,
flag
));
//todo
return
true
;
return
true
;
}
}
return
false
;
return
false
;
...
...
service/src/main/java/com/telephoners/krakyournet/ctf/resources/SolutionsResource.java
View file @
b7a9072c
...
@@ -42,9 +42,9 @@ public class SolutionsResource
...
@@ -42,9 +42,9 @@ public class SolutionsResource
@POST
@POST
public
Response
submitSolution
(
@Auth
User
user
,
public
Response
submitSolution
(
@Auth
User
user
,
String
flag
)
throws
Exception
String
hash
)
throws
Exception
{
{
if
(
tasksRepository
.
check
Flag
(
user
,
flag
))
{
if
(
tasksRepository
.
check
Hash
(
user
,
hash
))
{
return
Response
.
ok
().
build
();
return
Response
.
ok
().
build
();
}
}
return
Response
.
status
(
Response
.
Status
.
NOT_ACCEPTABLE
).
build
();
return
Response
.
status
(
Response
.
Status
.
NOT_ACCEPTABLE
).
build
();
...
...
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