obgm / libcoap

A CoAP (RFC 7252) implementation in C
Other
794 stars 423 forks source link

Command Injection in coap-client - libCoap 4.3.5 #1534

Open grumpzsux opened 1 week ago

grumpzsux commented 1 week ago

Environment]

Problem Description

I noticed that I able to achieve OS Command Injection by using backticks and the associated shell command in the URI inside the coap:// scheme when issuing GET requests using the coap-client, where I am using the latest version of libCoap 4.3.5.

Expected Behavior

I was expecting the client to return 4.04 which it did, but further confirmation shows that the shell commands are executing based on the coap server logs.

Actual Behavior

I am seeing the output of the shell command appended in the URI inside the coap:// scheme when issuing GET requests which is executing the command.

Steps to reproduce

For a proof of concept, I spun up a californium CoAP hello world server locally, and proceeded to use the coap-client to issue a request to the server:

coap-client -m get coap://127.0.0.1/id

As expected, the coap-client returned a 4.04 response, but when we check the server logs of the CoAP server, which returned the logs:

17:50:58.079 DEBUG [UDPConnector]: UDPConnector (127.0.0.1:5683) received 29 bytes from 127.0.0.1:46300
17:50:58.095 INFO [ServerMessageDeliverer]: **did not find resource /uid=1000(grumpz)** requested by 127.0.0.1:46300

We are able to confirm that we are successfully executing shell commands as a result. Further testing, we tried to execute another coap-client request using the command:

coap-client -m get coap://127.0.0.1/cat /etc/passwd

As expected, the coap-client returned a 4.04 response, but when we check the server logs of the CoAP server, which returned the logs:

17:52:49.468 INFO [ServerMessageDeliverer]: **did not find resource /root:x:0:0:root:/root:/usr/bin/zsh** requested by 127.0.0.1:47443
17:52:49.470 DEBUG [UDPConnector]: UDPConnector (Thread[UDP-Sender-/127.0.0.1:5683[1],5,Californium/Elements]) sent 4 bytes to 127.0.0.1:47443

coapRCE5 coapRCE4 coapRCE3 coapRCE2 coapRCE

mrdeep1 commented 6 days ago

The shell that you are using to run the coap-client is parsing the input parameters and updating them as is appropriate before passing to the program (coap-client) to act upon.

So

coap-client -m get coap://127.0.0.1/`id`

becomes (which is what the coap-client executable sees)

coap-client -m get coap://127.0.0.1/uid=1000(grumpz)

ant then sends uid=1000(grumpz) over the wire to the server as the Uri=Path.

No issues here with command line injection - it is the shell's interpretation of what you passed to it on the command line.

If you use the -v7 option for coap-client, you will actually see what the coap-client is sending over the wire