In regards to handguns, in every practical case of most likely every country, there’s no legal avenue of buying, owning or possessing them as a foreigner.
Some few exceptions are if you have diplomatic status, are an invited government personnel (law enforcement, military, security, intelligence) or a government contractor.
But essentially it’s impossible to procure a firearm legally as a foreigner in a foreign country, so that means resorting to the black market.
So it’s unadvisble to even attempt to buy a gun overseas, unless absolutely necessary.
Every major and most small cities have a robust underworld, this is where or how to get a gun. Making initial contact with the appropriate personnel will often be the first and most difficult obstacle.
As for myself, when I needed tools for work, they would usually be provided for by my employer or other entity – on-site, drop location or by courier.
There were many occasions where I had to use my own means through the underworld systems to procure firearms and other non-permissive tools.
But I had wide ranging resources at my disposal and significant prior intel of each locale to guide me to the right personnel, safely and efficiently.
In other words, I don’t recommend getting a gun in a foreign country.