LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-16-2022, 01:55 PM   #1
vidar567
LQ Newbie
 
Registered: May 2022
Posts: 6

Rep: Reputation: 0
Writing own script which will overcome another script .sh


In my system using cron.d there is a file which is repeatdly run. It generates an answer to my exercise.
I used ls /etc/cron.d I have access to this file.
It is alice.sh I use
Code:
 
sh /tmp/tmp.6vOX6HxEg1/alice.sh
my resposne
Code:
alice@ip-10-0-119-57:~$ sh /tmp/tmp.6vOX6HxEg1/alice.sh
cat: /root/archive/secret.txt: Permission denied
/opt/create_and_delete_file_from_args.sh: line 7: /tmp/tmp.6vOX6HxEg1/151504505d2d29ecad318671b5476560: Permission denied
rm: cannot remove '/tmp/tmp.6vOX6HxEg1/151504505d2d29ecad318671b5476560': No such file or directory
It generates a file that I need to read but it is as I read deleted.
I was thinking to run my own script which will listen and copy all kind of files when created to another directory. But I am green I do not know how to write own listener in bash linux.
Thanks in advance.


this is alice.sh

Code:
#!/usr/bin/env bash
  
secret_script_path="/opt/create_and_delete_file_from_args.sh"
secret_directory="/tmp/tmp.6vOX6HxEg1"
secret_file="TUApZ7WH3C7n"

bash "${secret_script_path}" "${secret_directory}" $(echo "${secret_file}" | md5sum | awk '{ print $1 }')
first edit:
this next .sh file has got a method that create and after 0,005s delete file with information.


EDIT:
This is my /opt/create_and_delete_file_from_args.sh file

Code:
#!/usr/bin/env bash

"secret_directory=$1
secret_file=$2
exercise_flag=$(cat "/root/archive/secret.txt")

echo "${exercise_flag}" > "${secret_directory}/${secret_file}"

sleep 0.005"

rm "${secret_directory}/${secret_file}"
I can't edit this file it is only readable I wanted to delete rm and add cat after creating.
There was an idea to create another .sh file without rm and with cat, but I can't do it.
After leaving vim I used
Code:
:w !sudo tee % > /dev/null
but there is error with

Code:
Username is not in the sudoers file. This incident will be reported
if I'm leaving with :w then "E212 Can't open file for writing"

Last edited by vidar567; 05-17-2022 at 01:04 AM.
 
Old 05-16-2022, 02:30 PM   #2
slac-in-the-box
Member
 
Registered: Mar 2010
Location: oregon
Distribution: slackware64-15.0 / slarm64-current
Posts: 780
Blog Entries: 1

Rep: Reputation: 432Reputation: 432Reputation: 432Reputation: 432Reputation: 432
Quote:
Originally Posted by vidar567 View Post
Code:
/opt/create_and_delete_file_from_args.sh
Code:
cat: /root/archive/secret.txt: Permission denied
Consider the two snippets from you code above: the first is an additonal shell script that gets excuted, but even though you did not post the content from that script in the /opt/ folder, it clearly is trying to catalogue a file in the /root folder.

But you user seems to be named alice, not root, so why should your linux system let alice look at root's files? The /root folder is the home folder of the root user, and you need to be the root user to access it.

Therefore, you should try your script (assuming you trust it) with root privledges by using "sudo" first, or use "su -" and become root. Once you are rooted, you have permission to see contents of /root. Always be careful as root and only run scripts that you trust. What if /opt/create_and_delete_file_from_args.sh had a command like "rm -rf /" -- if you executed that as root user, you would delete everything on your system, until you deleted the ability to delete.

Be careful with your root user, keep it secret and safe.
 
Old 05-16-2022, 02:56 PM   #3
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,137
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
Code:
secret_script_path="/opt/create_and_delete_file_from_args.sh"
secret_directory="/tmp/tmp.6vOX6HxEg1"
secret_file="TUApZ7WH3C7n"

echo "${secret_script_path}" "${secret_directory}" $(echo "${secret_file}" | md5sum | awk '{ print $1 }')
/opt/create_and_delete_file_from_args.sh /tmp/tmp.6vOX6HxEg1 151504505d2d29ecad318671b5476560
So I assume that create_and_delete_file_from_args.sh takes 2 args.

If you want help then post create_and_delete_file_from_args.sh

There is no way anyone could tell what you have, what is wrong, from what you have posted.

Who owns /tmp and the contents?
Code:
__ls -l /tmp
__ls -l /tmp/tmp.6vOX6HxEg1/alice.sh
Quote:
cat: /root/archive/secret.txt: Permission denied
You can't write to that unless you are root. You probably should not be writing anything there.

Etc.

Edit:
Sorry, cloudflare made me change ls to __ls, change it back.
 
Old 05-17-2022, 01:03 AM   #4
vidar567
LQ Newbie
 
Registered: May 2022
Posts: 6

Original Poster
Rep: Reputation: 0
This is my /opt/create_and_delete_file_from_args.sh file
Code:
Code:
#!/usr/bin/env bash

"secret_directory=$1
secret_file=$2
exercise_flag=$(cat "/root/archive/secret.txt")

echo "${exercise_flag}" > "${secret_directory}/${secret_file}"

sleep 0.005"

rm "${secret_directory}/${secret_file}"
I trust in 100% the code and exercises. This is university exercises, but I can't deal with this last step.

I basically need to change/write new script to have access, download the file which is created before it is deleted. Do you think you could help me with this?
 
Old 05-17-2022, 01:53 AM   #5
descendant_command
Senior Member
 
Registered: Mar 2012
Posts: 1,876

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
Just comment out the last line of /opt/create_and_delete_file_from_args.sh and then read it at your leisure.
 
Old 05-17-2022, 02:17 AM   #6
vidar567
LQ Newbie
 
Registered: May 2022
Posts: 6

Original Poster
Rep: Reputation: 0
I can't edit it.
 
Old 05-17-2022, 02:38 AM   #7
descendant_command
Senior Member
 
Registered: Mar 2012
Posts: 1,876

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
Why not?
 
Old 05-17-2022, 03:54 AM   #8
vidar567
LQ Newbie
 
Registered: May 2022
Posts: 6

Original Poster
Rep: Reputation: 0
I have errors to save such file, because i.e this is read only. I can't change permission of this file and after creating new one without it and just cat it immediately it show
Code:
Username is not in the sudoers file. This incident will be reported
 
Old 05-17-2022, 10:50 AM   #9
yancek
LQ Guru
 
Registered: Apr 2008
Distribution: Slackware, Ubuntu, PCLinux,
Posts: 10,502

Rep: Reputation: 2489Reputation: 2489Reputation: 2489Reputation: 2489Reputation: 2489Reputation: 2489Reputation: 2489Reputation: 2489Reputation: 2489Reputation: 2489Reputation: 2489
The message in post 8 tells you exactly what the problem is. You are trying to edit a file that needs root permissions. If you can't log in as root, you can't do it. You can put an entry in the /etc/sudoers file to allow a user to execute a specific command but again, you need root permissions to do that.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Can Lawyers 'Overcome' The Bogus Copyright On 'We Shall Overcome' And Free It To The Public Domain? LXer Syndicated Linux News 0 04-17-2016 08:45 AM
csh Shell Script: set: Too many arguments. Anyway to overcome this? vxc69 Programming 2 05-04-2009 08:33 PM
overcome chmod ohovus Linux - Security 4 11-29-2005 12:41 PM
How to overcome the 2GB Ftp limit in redhat 7.2 running kernel 2.4.7-10 Ryleco Linux - Software 0 09-07-2004 09:23 AM
imp...!grub error 23 how to overcome farhan Linux - General 0 03-11-2003 04:30 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration