echo the echo

Hi all, just a quick one that I’m sure is simple.
I can use this to update the suoders file:

echo myuser ALL=(ALL) ALL >> /etc/sudoers

How do I echo the exact command into another file

example:
echo “echo myuser ALL=(ALL) ALL >> /etc/sudoers” >> MOTD

I have tried escaping characters but I am struggling! Help would be very much appreciated. For what it’s worth, would like to add above into a MOTD for priv escalation.

What happens when you try this? I’ve just tried it and it appends the string as shown to the target file.

Are you sure you have quotes in the right place for this to execute?

You could try echo 'echo "myuser ALL=(ALL) ALL" >> /etc/sudoers' >> MOTD

Also, if this is for an HTB box there is a much better way to get a root shell.

It was for a box, I simply cat the flag and it worked. Could shell back with NC for a root shell.
When I tried the command, the terminal dropped to the next line with a >

I thought I’d ask here as my mind is fried right now and thought this would be a nice one to add to the arsenal.

I’ll try again later and do some local testing.

Keep safe and try harder!

@codespiller said:

It was for a box, I simply cat the flag and it worked. Could shell back with NC for a root shell.
When I tried the command, the terminal dropped to the next line with a >

That implies that some quotes or similar weren’t closed.

I thought I’d ask here as my mind is fried right now and thought this would be a nice one to add to the arsenal.

It is - although this particular exploitation path is fairly unusual.

As far as I can see, all the variations echo the required string to the file.