Acquire Android image using flash drive OTG In Linux, the dd command is used to pull the image from the smartphone to computer. And of course, the smartphone need to be rooted before the dd command launched.


Is there any specific command or script to be put on the flash drive, to acquire image of Android smartphone and store the acquired image to the flash drive On-The-Go?
How to make it the operation automatically run after the flash drive is inserted? For example: Once I insert the flash drive into Android smartphone, the dd command run automatically and the acquired image stored to flash drive.


Edit : For Forensic purpose, i would like to minimize footprint process such as installing any apps before the flash drive inserted.
Is there any specific command or script to be put on the flash drive, to acquire image of Android smartphone and store the acquired image to the flash drive On-The-Go?


Nope. No special command or script is needed other than cat or dd, the binaries thankfully Android comes with.

Often, the auto mount point of a flash drive is /mnt/usbotg (may differ for your device), so the command would be

dd if=/dev/block/BLOCK_DEVICE of=/mnt/usbotg/FILENAME.img


Replace BLOCK_DEVICE with the block file's name (the partition you want to copy). It may start with mmcblk. 

If required, see How to list all major partitions with their labels? 


  How to make it the operation automatically run after the flash drive is inserted? For example: Once i insert the flash drive into Android Smartphone, the dd command run automatically and the acquired image stored to flash drive. (sic)


Unless you're planning to inject a BadUSB into the Android device, you would need an automation app. I found Tasker and Automagic suitable for the job. Both are paid apps with free trial version available on their site.

Tasker

Here is a simple complete profile:


Profile: State &rarr; Hardware USB Connected &rarr;  Mass Storage
Task: (Actions): 


Code &rarr; Run Shell:


Command: dd if=/dev/block/BLOCK_DEVICE of=/mnt/usbotg/FILENAME.img
check Use Root



This should be enough but I prefer to add some glitters here and there. I would wait for 10 seconds after flash drive is mounted before executing the command. I would also give visual indication to myself that Tasker is running the countdown.

All of this can be done by following this simple procedure:


Set a variable to maximum length of countdown (Variables &rarr; Variable Set)
Wait for 1 second (Task &rarr; Wait)
Subtract 1 from the remaining of variable (Variables &rarr; Variable Subtract)
Cancel any Tasker notification (Alert &rarr; Notify Cancel)
Give visual indication of countdown (Alert &rarr; Notify &rarr; include variable in Title or Text)
Repeat the said steps (exclude variable set) for countdown (Task &rarr; Goto &rarr; Action Number: 2)
Run the command (Code &rarr; Run Shell)



This would work. However, the moment you plug your flash drive into your device, Tasker would seek confirmation whether to continue with this flash drive or not. It can remember the device for future correspondence.

Automagic

Automagic would introduce you with a mandatory know-how, so details are not  important here. The basic flow would need:


Trigger: Storage Media Event; Event: Mounted
Action: Execute Root Command &rarr; fill the details


Activate the flow and Automagic's service, and test the flow.

There is one major caveat here. There is no differentiation between the type of storage connected, hence, your flow may trigger if you inject SD card too. 

Note: Rather than keeping the command inside the apps, you can keep it under a script and place it in your flash drive. Configure your automation app to confirm whether the script exists in the mounted flash drive and if yes, execute it. For confirmation, Tasker already has Read File action. Else, ls "YOUR_SCRIPT" should help.