Is this script vulnerable to command injection?











up vote
0
down vote

favorite












I have the following script that I can run as root in a server with sudo:



#!/bin/bash
export filename=`echo $1`
grep -i "word" $filename


I suspect that this script is vulnerable to command injection, is this the case?



However, when I put something as "test ;id" as an argument of the script with sudo (e.g. sudo myScript.sh test; id). The "id" command is running but with my user access right not as root.










share|improve this question






















  • Double quote the $filename in the last line.
    – choroba
    Nov 8 at 10:51










  • Try using '-r /' as the argument.
    – choroba
    Nov 8 at 10:55












  • The echo $1 is the vulnerable point for me. As it stands that is pointless, so I assume it is something more complex in reality. Put quotes round the "$1" as suggested.
    – Gem Taylor
    Nov 8 at 11:28










  • Use Shellcheck to find many code problems, including command injection vulnerabilities. It finds several problems with the example code. One problem that it does not find is the inability to handle filenames that begin with '-'. A fully safe 'grep command is grep -i -- word "filename". See Bash Pitfalls #3 (Filenames with leading dashes).
    – pjh
    Nov 8 at 20:00

















up vote
0
down vote

favorite












I have the following script that I can run as root in a server with sudo:



#!/bin/bash
export filename=`echo $1`
grep -i "word" $filename


I suspect that this script is vulnerable to command injection, is this the case?



However, when I put something as "test ;id" as an argument of the script with sudo (e.g. sudo myScript.sh test; id). The "id" command is running but with my user access right not as root.










share|improve this question






















  • Double quote the $filename in the last line.
    – choroba
    Nov 8 at 10:51










  • Try using '-r /' as the argument.
    – choroba
    Nov 8 at 10:55












  • The echo $1 is the vulnerable point for me. As it stands that is pointless, so I assume it is something more complex in reality. Put quotes round the "$1" as suggested.
    – Gem Taylor
    Nov 8 at 11:28










  • Use Shellcheck to find many code problems, including command injection vulnerabilities. It finds several problems with the example code. One problem that it does not find is the inability to handle filenames that begin with '-'. A fully safe 'grep command is grep -i -- word "filename". See Bash Pitfalls #3 (Filenames with leading dashes).
    – pjh
    Nov 8 at 20:00















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have the following script that I can run as root in a server with sudo:



#!/bin/bash
export filename=`echo $1`
grep -i "word" $filename


I suspect that this script is vulnerable to command injection, is this the case?



However, when I put something as "test ;id" as an argument of the script with sudo (e.g. sudo myScript.sh test; id). The "id" command is running but with my user access right not as root.










share|improve this question













I have the following script that I can run as root in a server with sudo:



#!/bin/bash
export filename=`echo $1`
grep -i "word" $filename


I suspect that this script is vulnerable to command injection, is this the case?



However, when I put something as "test ;id" as an argument of the script with sudo (e.g. sudo myScript.sh test; id). The "id" command is running but with my user access right not as root.







bash security code-injection






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 8 at 10:40









user1528760

82211




82211












  • Double quote the $filename in the last line.
    – choroba
    Nov 8 at 10:51










  • Try using '-r /' as the argument.
    – choroba
    Nov 8 at 10:55












  • The echo $1 is the vulnerable point for me. As it stands that is pointless, so I assume it is something more complex in reality. Put quotes round the "$1" as suggested.
    – Gem Taylor
    Nov 8 at 11:28










  • Use Shellcheck to find many code problems, including command injection vulnerabilities. It finds several problems with the example code. One problem that it does not find is the inability to handle filenames that begin with '-'. A fully safe 'grep command is grep -i -- word "filename". See Bash Pitfalls #3 (Filenames with leading dashes).
    – pjh
    Nov 8 at 20:00




















  • Double quote the $filename in the last line.
    – choroba
    Nov 8 at 10:51










  • Try using '-r /' as the argument.
    – choroba
    Nov 8 at 10:55












  • The echo $1 is the vulnerable point for me. As it stands that is pointless, so I assume it is something more complex in reality. Put quotes round the "$1" as suggested.
    – Gem Taylor
    Nov 8 at 11:28










  • Use Shellcheck to find many code problems, including command injection vulnerabilities. It finds several problems with the example code. One problem that it does not find is the inability to handle filenames that begin with '-'. A fully safe 'grep command is grep -i -- word "filename". See Bash Pitfalls #3 (Filenames with leading dashes).
    – pjh
    Nov 8 at 20:00


















Double quote the $filename in the last line.
– choroba
Nov 8 at 10:51




Double quote the $filename in the last line.
– choroba
Nov 8 at 10:51












Try using '-r /' as the argument.
– choroba
Nov 8 at 10:55






Try using '-r /' as the argument.
– choroba
Nov 8 at 10:55














The echo $1 is the vulnerable point for me. As it stands that is pointless, so I assume it is something more complex in reality. Put quotes round the "$1" as suggested.
– Gem Taylor
Nov 8 at 11:28




The echo $1 is the vulnerable point for me. As it stands that is pointless, so I assume it is something more complex in reality. Put quotes round the "$1" as suggested.
– Gem Taylor
Nov 8 at 11:28












Use Shellcheck to find many code problems, including command injection vulnerabilities. It finds several problems with the example code. One problem that it does not find is the inability to handle filenames that begin with '-'. A fully safe 'grep command is grep -i -- word "filename". See Bash Pitfalls #3 (Filenames with leading dashes).
– pjh
Nov 8 at 20:00






Use Shellcheck to find many code problems, including command injection vulnerabilities. It finds several problems with the example code. One problem that it does not find is the inability to handle filenames that begin with '-'. A fully safe 'grep command is grep -i -- word "filename". See Bash Pitfalls #3 (Filenames with leading dashes).
– pjh
Nov 8 at 20:00



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206016%2fis-this-script-vulnerable-to-command-injection%23new-answer', 'question_page');
}
);

Post as a guest





































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206016%2fis-this-script-vulnerable-to-command-injection%23new-answer', 'question_page');
}
);

Post as a guest




















































































Popular posts from this blog

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff