Skip to content
This repository was archived by the owner on Mar 5, 2020. It is now read-only.

Commit c9958fd

Browse files
committed
Adding the option of remote code execution in a container
1 parent 03c600f commit c9958fd

File tree

4 files changed

+86
-34
lines changed

4 files changed

+86
-34
lines changed

src/main/java/pl/simplemethod/codebin/srv/SrvRestController.java

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,53 @@ public class SrvRestController {
3939
@Autowired
4040
private UsersRepository usersRepository;
4141

42+
/**
43+
* Check if server port is taken and if so, change it to another port
44+
* @param hostPorts Original server port
45+
* @return Server port
46+
*/
47+
private Integer checkPort(Integer hostPorts)
48+
{
49+
Containers containers = containersRepository.getByHostPorts(hostPorts);
50+
Random rand = new Random();
51+
try
52+
{
53+
if(containers.getHostPorts()!=null)
54+
{
55+
if(containers.getHostPorts().equals(hostPorts))
56+
{
57+
return checkPort(hostPorts + rand.nextInt((9999 - 1) + 1) + 1);
58+
}
59+
else
60+
{
61+
return hostPorts;
62+
}
63+
}
64+
else
65+
{
66+
return hostPorts;
67+
}
68+
}
69+
catch (NullPointerException e)
70+
{
71+
return hostPorts;
72+
}
73+
}
74+
75+
4276
/**
4377
* Method is used to get data on the container from the database.
4478
* @param hostPort Server port
4579
* @return Object JSON with data
4680
*/
4781
@GetMapping("users/container/info/{hostPort}")
4882
public @ResponseBody
49-
ResponseEntity checkports(@PathVariable(value = "hostPort") String hostPort) {
83+
ResponseEntity checkPorts(@PathVariable(value = "hostPort") String hostPort) {
5084
HttpHeaders httpHeaders = new HttpHeaders();
5185
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
5286
org.json.JSONObject body = new org.json.JSONObject();
5387

54-
/* Containers containers = containersRepository.getByHostPorts(Integer.valueOf(hostPort));
88+
Containers containers = containersRepository.getByHostPorts(Integer.valueOf(hostPort));
5589
try{
5690
if(containers.getId()!=null)
5791
{
@@ -76,9 +110,6 @@ ResponseEntity checkports(@PathVariable(value = "hostPort") String hostPort) {
76110
body.put("error",e);
77111
return new ResponseEntity<>(body.toString(),httpHeaders,HttpStatus.valueOf(404));
78112
}
79-
*/
80-
return new ResponseEntity<>(checkPort(Integer.valueOf(hostPort)),httpHeaders,HttpStatus.valueOf(200));
81-
82113
}
83114

84115
/**
@@ -137,34 +168,7 @@ ResponseEntity userContainerCheck(@RequestParam("dockergithubid") String dockerG
137168
}
138169
}
139170

140-
private Integer checkPort(Integer hostPorts)
141-
{
142-
Containers containers = containersRepository.getByHostPorts(hostPorts);
143-
Random rand = new Random();
144-
try
145-
{
146-
if(containers.getHostPorts()!=null)
147-
{
148-
if(containers.getHostPorts().equals(hostPorts))
149-
{
150-
return checkPort(hostPorts + rand.nextInt((9999 - 1) + 1) + 1);
151-
}
152-
else
153-
{
154-
return hostPorts;
155-
}
156171

157-
}
158-
else
159-
{
160-
return hostPorts;
161-
}
162-
}
163-
catch (NullPointerException e)
164-
{
165-
return hostPorts;
166-
}
167-
}
168172

169173
/**
170174
* REST for container creation

src/main/resources/public/app.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,24 @@ app.controller('ContainersController', ['$filter', '$routeParams', '$scope', '$h
214214
}
215215
);
216216
};
217+
218+
219+
$scope.execCommands = function (path, args) {
220+
$http({
221+
url: 'http://127.0.0.1/srv/container/' + $scope.containersId + '/exec',
222+
method: 'POST',
223+
params: {
224+
path: path,
225+
argument: args
226+
}
227+
}).then(
228+
function () {
229+
},
230+
function () {
231+
}
232+
);
233+
};
234+
217235
$http({
218236
url: 'http://127.0.0.1/srv/container/' + $scope.containersId + '/logs',
219237
method: 'GET'
@@ -309,7 +327,7 @@ app.controller('CheckLoginStatus', function ($scope, $http, $cookies) {
309327
});
310328

311329

312-
app.controller('dashboardGithub', function ($scope, $http, $cookies, $window) {
330+
app.controller('dashboardGithub', function ($scope, $http, $cookies) {
313331
$scope.lastVal = $cookies.get('token');
314332
$http({
315333
url: 'http://127.0.0.1/github/user',
@@ -319,6 +337,8 @@ app.controller('dashboardGithub', function ($scope, $http, $cookies, $window) {
319337
$scope.passCheck = response;
320338
$scope.avatar = response.data.avatar_url;
321339
$scope.name = response.data.name;
340+
$scope.urlHtml = response.data.html_url;
341+
322342
},
323343
function () {
324344
$scope.passCheck = false;

src/main/resources/public/dashboard.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
<span class="d-none d-md-inline-block">{{name}}</span>
8989
</a>
9090
<div class="dropdown-menu dropdown-menu-small">
91-
<a class="dropdown-item" href="dashboard/dashboard-profile.html">
91+
<a class="dropdown-item" href="{{urlHtml}}">
9292
<i class="material-icons">&#xE7FD;</i> Profile</a>
9393
<div class="dropdown-divider"></div>
9494
<a class="dropdown-item text-danger" ng-click="logout()">

src/main/resources/public/dashboard/container.html

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,12 @@ <h6 class="m-0">General view</h6>
7474
<span class="d-flex mb-2"><i class="material-icons mr-1">calendar_today</i><strong
7575
class="mr-1">Creation time:</strong> <mydate>{{creationTime * 1000 | date:'dd-MM-yyyy HH:mm:ss'}}</mydate> </span>
7676

77+
7778
<span class="d-flex mb-2">
7879
<button type="button" class="mb-2 btn btn-danger mr-2" data-toggle="modal"
7980
data-target="#remove">Remove container</button>
81+
<button type="button" class="mb-2 btn btn-success mr-2" data-toggle="modal"
82+
data-target="#exec">Execute commands</button>
8083
<button type="button" class=" ml-auto mb-2 btn btn-warning mr-2" ng-click="dockerRestart()">Restart container</button>
8184
</span>
8285
</ul>
@@ -152,4 +155,29 @@ <h5 class="modal-title">Warning!</h5>
152155
</div>
153156
</div>
154157
</div>
158+
</div>
159+
160+
161+
<div class="modal" tabindex="-1" role="dialog" id="exec">
162+
<div class="modal-dialog modal-sm" role="document">
163+
<div class="modal-content">
164+
<div class="modal-header">
165+
<h5 class="modal-title">Execute commands</h5>
166+
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
167+
</button>
168+
</div>
169+
<div class="modal-body">
170+
<div class="form-group">
171+
<input type="text" class="form-control" id="path" placeholder="Path" ng-model="execPath" required>
172+
</div>
173+
<div class="form-group">
174+
<input type="text" class="form-control" id="args" placeholder="Arguments" ng-model="execArgs" required>
175+
</div>
176+
<div style="text-align: center"> <button type="button" class="mb-2 btn btn-success mr-2" ng-click="execCommands(execPath,execArgs)">Execute</button></div>
177+
</div>
178+
<div class="modal-footer">
179+
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
180+
</div>
181+
</div>
182+
</div>
155183
</div>

0 commit comments

Comments
 (0)