How do I make this wiring inside cabinet safer?
There is nothing you can do with old wiring that doesn't meet code that will make it meet code. Old NM cable with no ground may have been "grandfathered" into an existing building, so long as you don't alter it. But that would not apply here because the WAY it was done was never per code.

Do you really need a circuit there? I would just find out where it comes from and disconnect it there, then pull it out. If you DO want another circuit there, you can attach new NM cable with a ground to the end of this before pulling it out, then get an "old work box" that can be mounted into the wall properly. An old work box is designed to go into a cut-out in the wall, then has "molly bolt" type fasteners that turn behind the wall and snug up against the back of it to hold the box in place (make sure you put the wire through the KO first!). Once the wire is in there, you can put in a receptacle for a light, fan etc.