Searching for specific magic byte in an ocean of files

I want to know how I can search for a specific file type: I have entered a SSH server and I am trying to search for a .jpg file, but the owner of the server (my teacher) has removed all extensions.

I have tried grep -lr "JFIF" and I have found many directories with the same file name, so I jumped into a random directory which was found with the grep command and I tried to use the cat command on it, but the terminal couldn't show the entire binary.

Also the strings command is blocked on the ssh server, my teacher said that you have to find a jpg file and that this file contains a serial number (SN), but I have no idea where to find it.

If you guys want the SSH cardinals I will be happy to give them out to you, anyway my teacher said that you can use the file command to do that but I don't know how to do it.

BTW : he said (SN)

11

4 Answers

I'll try to give you some hints so you can solve your HW yourself.

Follow this steps:

  1. read the manual of file by executing man file.
  2. Then try it out by file somefile and see what happens
  3. Try to run file on different file types
  4. By now you should be able to understand how to find out if some file is a jpeg image or not.
  5. now read the manual for find (or use google to find out how to use it to find all files in some directory and all subdirectories)
  6. now find out how to use -exec option of find in order to connect it with previously used file command
  7. Now you should be able to find out the filetypes of all files in needed directory and list them
  8. Now read about pipes | and grep command to find out how to filter only for JPEG files.
10

First of all i want to thank @incBrain and @Serg for not answering my question directly which helped me understand a lot of new things :).

My task was finding a specific .jpg file that has it's extension removed, the first thing I though of is .jpg magic byte which was JFIF, and with @incBrain Instructions I was able to build this command

find / -exec file {} \; | grep "JFIF"

that line links the find command to file command using -exec then gets the output of both find & file commands and transfer it as an input through the pipe symbol | for the grep command which shows only the files that contain JFIF in their binary a.k.a .jpg files .

My problem actually was finding a specific serial number, at first I though that the serial number is included in the file's binary but that was wrong, then a crazy though came to my mind

Oh! why don't I see what the picture looks like, I mean if I can't find the serial number, getting the picture would be a good thing

and I have searched the Internet for a good way to transfer files from SSH into my machine and I've found this block of code which worked out!

scp :/home/example/file.txt /Local_Directory

I hope that this answer helps someone in the future :).

One possible solution could be this: use find which recursively lists regular files (-type f), and performs file command upon each one of them. Redirect output to grep to filter out filetypes.

However here, I would like to do something more fun than that; more awkward , but more fun.

$ find . -maxdepth 1 -type f -printf "%f\t" -exec hexdump -n8 {} \; | awk '/d8ff e0ff 1000 464a/{print $1}'

As you may or may not know every file has first 8 bytes of any file designate the filetype. Thus using find we search for all regular files, print it's name, but then execute hexdump to extract first 8 bytes, and let awk filter out only those filenames that have those first 8 bytes.

Here's a small proof:

$ hexdump -n 10 1450763029649.jpg
0000000 d8ff e0ff 1000 464a 4649
000000a
$ hexdump -C -n 10 1450763029649.jpg
00000000 ff d8 ff e0 00 10 4a 46 49 46 |......JFIF|
0000000a
9
file * | grep -i "jpeg"

This will search through every file in the directory and return it's file type. Via the Pipe | these results then get searched through by grep to find the file with the file type of "jpeg" or basically a .jpg file.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like