Unix forever!

DSR enhanced security and "formal verification" course

The "one-click" Linux C programming course

This course is completely done from my Android mobile, which, of course, is a Linux too.

It's so simple, you'll love it!

Linux Server in your browser (one-click!)

https://bellard.org/jslinux/vm.html?url=alpine-x86.cfg&mem=192

What you then will see here is a typical Unix terminal. Same as you would see after a Linux install on your computer - without the standard GNOME desktop - or after opering a Bash shell after a standard Linux installation. The only difference is, that this Linux completely runs in your browser.

Loading...

Welcome to JS/Linux (i586)

Use 'vflogin username' to connect to your account.
You can create a new account at https://vfsync.org/signup .
Use 'export_file filename' to export a file to your computer.
Imported files are written to the home directory.

localhost:~#  

localhost:~# is the alias name for the local IP address 127.0.0.1

The # in localhost:~# tells you that you are administrator, which in Unix always has the username root.

Using with / on Android phone/tablet environment

It's about saving and restoring your work and downloading examples from the internet.

Clearing your screen

Simple: localhost:~# clear ⏎

Up-/downloading files from/to Linux Server

For the course, you might want to upload some locally edited or downloaded .c files into the Linux Servern running in your browser. Or get some files out of the Linux Server to your local /download directory on your Android phone or tablet. Here's how you do it:

1
2
3
4
localhost:~# ls ⏎
bench.py    hello.c     hello.js    readme.txt
localhost:~# export_file hello.c ⏎
localhost:~#

The hello.c file is now saved to your Android mobile or tablet. You find it in the /download directory.

From there you can upload "hello.c" again into the file system of your Linux Server:

Click on the up-arrow ⤒ in the down left corner of your Linux terminal window to upload files from local /download directory on Android mobile or tablet to continue your work.

Uploading .c file into your Linux Server

From e.g. Chuck Severance's excellent C course, download

https://github.com/csev/cc4e/blob/master/book/code/c_008_01.c

Upload c_008_01.c from your Android's /download directoy into the Linux Server by pressing the ⤒ botton left below the Linux Server terminal window.

Verify that the file c_008_01.c was uploaded:

1
2
3
4
5
6
7
8
localhost:~# ls -l ⏎
total 20
-rw-r--r--    1 root     root           114 Jul  5  2020 bench.py
-rw-------    1 1000     root           439 Jul 13 13:48 c_008_01.c
-rw-r--r--    1 root     root            76 Jul  3  2020 hello.c
-rw-r--r--    1 root     root            22 Jun 26  2020 hello.js
-rw-r--r--    1 root     root           151 Jul  5  2020 readme.txt
localhost:~#  

First compile and run of a C program

It's always safe to make a copy. With the cp command you can also rename it:

1
2
3
4
5
6
localhost:~# ls ⏎
bench.py    c_008_01.c  hello.c     hello.js    readme.txt
localhost:~# cp c_008_01.c fahrenheit.c ⏎
localhost:~# ls ⏎
bench.py      fahrenheit.c  hello.js
c_008_01.c    hello.c       readme.txt

Now we compile with Fabrice Bellard's own tcc C compiler:

localhost:~# tcc fahrenheit.c -o fahrenheit ⏎
localhost:~# ls ⏎
bench.py      fahrenheit.c  hello.c       readme.txt
fahrenheit    hello         hello.js
localhost:~# ./fahrenheit ⏎
   0  -17.8
  20   -6.7
  40    4.4
  60   15.6
  80   26.7
 100   37.8
 120   48.9
 140   60.0
 160   71.1
 180   82.2
 200   93.3
 220  104.4
 240  115.6
 260  126.7
 280  137.8
 300  148.9
localhost:~#

Note: To execute the binary fahrenheit you have to put a ./ before. For security reasons your directory, where you're working in, is not included in the search path for binaries. But we can change that by adding /root to the $PATH environment variable:

localhost:~# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
localhost:~# export PATH="/root:$PATH"
localhost:~# echo $PATH
/root:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
localhost:~# ls
bench.py    hello.c     hello.js    readme.txt
localhost:~# tcc hello.c -o hello
localhost:~# hello
hello world
localhost:~#

Installing offline code editor

You might want to use a local editor on your Android phone / tablet to work offline:

https://play.google.com/store/apps/details?id=com.maxistar.textpad

https://f-droid.org/de/packages/com.maxistar.textpad/

Who or where am I, what rights do I have?

Let's try following Unix commands:

1
2
3
4
5
6
7
8
9
localhost:~# clear ⏎
localhost:~# whoami ⏎
root
localhost:~# id ⏎
uid=0(root) gid=0(root)
localhost:~# pwd/root
localhost:~# echo $PATH/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

It sais, that you are user root (root always has admin rights!!!), user ID and group ID are zero (which always is for admin), your current directory is /root and it gives out the search path, where binaries are searched when you simply type in a command, e.g. ls

Unix standard commands

Exploring the environment

Here you see the different possibilities to show the contents of the default directory, Linux provides or has set for you directly after the login:

localhost:~# ls ⏎
bench.py    hello.c     hello.js    readme.txt
localhost:~# ls -l ⏎
total 16
-rw-r--r--    1 root     root           114 Jul  5  2020 bench.py
-rw-r--r--    1 root     root            76 Jul  3  2020 hello.c
-rw-r--r--    1 root     root            22 Jun 26  2020 hello.js
-rw-r--r--    1 root     root           151 Jul  5  2020 readme.txt
localhost:~# ls -a ⏎
.             .ash_history  .mozilla      bench.py      hello.js
..            .cache        .wine         hello.c       readme.txt
localhost:~#  

The directories beginning with a . are called 'hidden directories'. It's a convention in Unix. Unix is a multi user operating system. There you typically find local config files, a user can set to individually customise its binaries e.g. the browser, the mail program, editor, the graphical user interface ... It's what in Windows is called "Registry". Each user's home directory has one. You typically find them under /home.

Unix has a prototype environment setup, which gets set when a user is logging in. You find it in the file /etc/profile , which is the registry, where the setup and common environment variables for the whole Unix machine at bootup is set:

localhost:~# more /etc/profile ⏎
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PAGER=less
export PS1='\h:\w\$ '
umask 022

for script in /etc/profile.d/*.sh ; do
        if [ -r $script ] ; then
                . $script
        fi
done
localhost:~#

In variable PS1 the shell prompt is defined: In our case: PS1='\h:\w\$ ' which means first the hostname "localhost" comes, followed by a ":" then the working directory and then the $ prompt which indicates the normal user prompt.

Administrative commands under Unix

Adding a user in Unix

localhost:/home# adduser -h /home/myuser myuser ⏎
Changing password for myuser
New password: *************
Retype password: *************
passwd: password for myuser changed by root
localhost:/home# ls ⏎
myuser
localhost:/home# tail -1 /etc/passwd ⏎
myuser:x:1001:1001:Linux User,,,:/home/myuser:/bin/ash
localhost:/home# tail -1 /etc/shadow ⏎
myuser:$6$tvMlh0k5Y7sdKYwM$p/Ru9qKPoqiS6D8xMksDhrrPpLfAmhJO1Lgm.tq01OoF5ZB8OgwHa
tp5nRsCWssZUadEOLRT7e6Bim0S09RCg/:19917:0:99999:7:::

Testing the myuser account

From user root become user myuser, verify and then let's switch back:

localhost:~# cdlocalhost:~# pwd/root
localhost:~# id ⏎
uid=0(root) gid=0(root)
localhost:~# whoami ⏎
root
localhost:~# su myuser ⏎
localhost:/root$ cdlocalhost:~$ id ⏎
uid=1001(myuser) gid=1001(myuser) groups=1001(myuser)
localhost:~$ whoami ⏎
myuser
localhost:~$ pwd/home/myuser
localhost:~$ exitlocalhost:~# pwd/root

You may have noticed that the user prompt changes from # to $ when have limited rights as user myuser ...

Changing directory in Unix

1
2
3
4
5
6
7
8
localhost:/home# cd ⏎
localhost:~# cd /home
localhost:/home# ls ⏎
myuser
localhost:/home# cd ⏎
localhost:~# pwd ⏎
/root
localhost:~#  

The important /bin and /sbin directories

The directories /bin and /sbin always have to be there, same as /etc and /dev.

When you accidentally may have cut off yourself by e.g. setting the environment variable PATH you will have to work with absolute pathnames, e.g. /bin/ls

localhost:~# cd /bin ⏎
localhost:/bin# ls ⏎
arch            false           lzop            rm
ash             fatattr         makemime        rmdir
base64          fdflush         mkdir           run-parts
bash            fgrep           mknod           sed
bbconfig        findmnt         mktemp          setpriv
bbsuid          fsync           more            setserial
busybox         getopt          mount           sh
busybox-extras  grep            mountpoint      sleep
cat             gunzip          mpstat          stat
chgrp           gzip            mv              stty
chmod           hostname        netstat         su
chown           ionice          nice            sync
conspy          iostat          pidof           tar
cp              ipcalc          ping            touch
date            kbd_mode        ping6           true
dd              kill            pipe_progress   umount
df              link            printenv        uname
dmesg           linux32         ps              usleep
dnsdomainname   linux64         pwd             watch
dumpkmap        ln              rc-status       wdctl
echo            login           red             zcat
ed              ls              reformime       zsh
egrep           lsblk           rev             zsh-5.8
localhost:/bin# cd /sbin ⏎
localhost:/sbin# ls ⏎
acpid              hdparm             mkdosfs            reboot
adjtimex           hwclock            mkfs               rmmod
agetty             ifconfig           mkfs.bfs           route
apk                ifdown             mkfs.cramfs        runscript
arp                ifenslave          mkfs.minix         service
blkdiscard         ifup               mkfs.vfat          setconsole
blkid              init               mkmntdirs          sfdisk
blkzone            init.org           mkswap             slattach
blockdev           inotifyd           modinfo            start-stop-daemon
cfdisk             insmod             modprobe           supervise-daemon
chcpu              ip                 nameif             swaplabel
ctrlaltdel         ipaddr             nologin            swapoff
depmod             iplink             openrc             swapon
dhcpcd             ipneigh            openrc-init        switch_root
fbsplash           iproute            openrc-run         sysctl
fdisk              iprule             openrc-shutdown    syslogd
findfs             iptunnel           pivot_root         tunctl
fsck               klogd              poweroff           udhcpc
fsck.cramfs        ldconfig           raidautorun        vconfig
fsck.minix         loadkmap           raw                watchdog
fsfreeze           logread            rc                 wipefs
fstrim             losetup            rc-service         zramctl
getty              lsmod              rc-sstat
halt               mdev               rc-update

Back to last lesson - https://rentry.co/DSRsecuritycoursepart6 Next lesson: https://rentry.co/DSRsecuritycoursepart8

Edit
Pub: 27 Jun 2024 09:14 UTC
Edit: 14 Jul 2024 22:37 UTC
Views: 227