This is very common abroad in developing countries. Since I essentially get all of my spending money via ATM machines abroad in developing countries, I have tried to become savvy at avoiding this.
I always carefully look at the insertion slot of any ATM machine to make sure it looks like the real thing. I typically only use ATM machines inside a business, mall, or bank, particularly in malls.
Cover the hand when you key in your code, so that it can't be filmed.
I typically don't count the money I get from an ATM if it looks about right. If they are short, you are not going to be able to get the money reimbursed anyway. And someone scoping out the machine won't be able to see how much money you got.
I lowered the daily limit on my main ATM card to $500 from $1000 and removed overdraft protection. Finally, I almost never use a credit card abroad (except to purchase flights) or use a debit card for a purchase, and especially if it will be going out of my sight for whatever reason. I carry about 3 or 4 ATM cards for long trips, and I stopped calling all 4 banks before leaving, just the 2 banks for my main cards. That way, if the other cards get stolen, they are less likely to be usable by a thief (and I could easily call to activate them if I need them).