XP-rience

Something that I'd like to share with you!

Saturday, May 09, 2020

Linux dd multiple partitions into single image file

No comments :

For example, USB 1 drive with 3 partitions to be written into a single image file.
List block devices

[root@localhost ddtest]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sdb      8:16   1  244M  0 disk
├─sdb1   8:17   1   30M  0 part /run/media/root/datapartition2
├─sdb2   8:18   1   40M  0 part /run/media/root/datapartition
└─sdb3   8:19   1   50M  0 part /run/media/root/datapartition1

/dev/sdb1,2,3 = 30M,40M,50M in size
Umount them

[root@localhost ddtest]# umount /dev/sdb*

Confirm again

[root@localhost ddtest]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sdb      8:16   1  244M  0 disk
├─sdb1   8:17   1   30M  0 part
├─sdb2   8:18   1   40M  0 part
└─sdb3   8:19   1   50M  0 part

Fdisk the drive to find its last partition end sector

root@localhost ddtest]# fdisk -l /dev/sdb

Disk /dev/sdb: 255 MB, 255852544 bytes, 499712 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xf4f4f4f4

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048       63487       30720   83  Linux
/dev/sdb2           63488      145407       40960   83  Linux
/dev/sdb3          145408      247807       51200   83  Linux

Calculate dd count over 1MB with formula below
(<last partition end sector> + 1) / 2048
For this USB we have (247807 + 1) / 2048 = 121

Now dd the whole /dev/sdb to test.iso with count of 121 times of 1MBytes

[root@localhost ddtest]# dd if=/dev/sdb of=test.iso bs=1M count=121 status="progress" conv=sync,noerror
121+0 records in
121+0 records out
126877696 bytes (127 MB) copied, 37.6761 s, 3.4 MB/s

Check the test.iso file size

[root@localhost ddtest]# ls -alh
-rw-r--r--. 1 root root 121M May  8 23:37 test.iso

Test the ISO file by restoring the ISO to another USB drive.
Plug out USB 1 and plug in USB 2 and check lsblk

[root@localhost ddtest]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sdb      8:16   1  3.7G  0 disk

Dd the test.iso file to /dev/sdb

[root@localhost ddtest]# dd if=test.iso of=/dev/sdb bs=1M conv=sync,noerror
121+0 records in
121+0 records out
126877696 bytes (127 MB) copied, 25.2433 s, 5.0 MB/s

Check lsblk again

[root@localhost ddtest]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sdb      8:16   1  3.7G  0 disk
├─sdb1   8:17   1   30M  0 part
├─sdb2   8:18   1   40M  0 part
└─sdb3   8:19   1   50M  0 part

Now we have 3 partitions restored identical to USB 1
Try to mount and check partition content

[root@localhost ddtest]# mount /dev/sdb1 1
[root@localhost ddtest]# mount /dev/sdb2 2
[root@localhost ddtest]# mount /dev/sdb3 3
[root@localhost ddtest]# ls 1
WinSCP-5.17.5-Setup.exe
[root@localhost ddtest]# ls 2
WinSCP-5.17.5-Setup.exe
[root@localhost ddtest]# ls 3
WinSCP-5.17.5-Setup.exe

Check  lsblk

[root@localhost ddtest]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sdb      8:16   1  3.7G  0 disk
├─sdb1   8:17   1   30M  0 part /root/Desktop/temp/ddtest/1
├─sdb2   8:18   1   40M  0 part /root/Desktop/temp/ddtest/2
└─sdb3   8:19   1   50M  0 part /root/Desktop/temp/ddtest/3

It also works with Windows tools such as Rufus...


Or Win32 Disk Imager, but only if the device exists under the drop button circled below


How the partitions look like under Windows Disk Management


Just to add, if you are having missing partition from Gparted you can try to zeroing the 1st sector of 512 bytes (MBR)

[root@localhost ddtest]# dd if=/dev/zero of=/dev/sdc bs=512 count=1
1+0 records in
1+0 records out
512 bytes (512 B) copied, 0.00308483 s, 166 kB/s




Thursday, May 07, 2020

Nginx PHP-FPM setup under Kali Linux

No comments :
Another Nginx PHP-FPM setup post similar to previous post [ here ]

To start with, install Nginx...

root@kali:~# apt-get -y install nginx

Start Nginx

root@kali:~# service nginx start

Install PHP-FPM, note the version

root@kali:~# apt-get -y install php-fpm

Edit default configuration

root@kali:~# vi /etc/nginx/sites-available/default

Add index.php into server block

index index.html index.htm index.nginx-debian.html index.php;

Add location block

location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
}

Restart Nginx

root@kali:~# service nginx restart

Edit php.ini, note the PHP-FPM version, add cgi.fix_pathinfo=0

root@kali:~# vi /etc/php/7.3/fpm/php.ini

; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI.  PHP's
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is.  For more information on PATH_INFO, see the cgi specs.  Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec.  A setting
; of zero causes PHP to behave as before.  Default is 1.  You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://php.net/cgi.fix-pathinfo
cgi.fix_pathinfo=0

Restart PHP-FPM

root@kali:~# service php7.3-fpm restart

Create a PHP test page "test.php" as content below and save it to /var/www/html/

<?php
phpinfo();
?>

run the PHP file = http://<your host>/test.php