> Why is US good for attacking various middle eastern, african and south american countries, and russia is bad for attacking ukraine?

Intent matters. Iraq was not a sovereign nation when the US invaded, it was a slave encampment (its people had no human rights), its majority Shia population was held hostage - genocided and constantly tortured - by the minority Sunni Hussein regime. There's no such thing as a sovereign dictatorship; there can be no claim of sovereignty where there are no human rights. Any free nation had a moral right to invade Iraq, if it so chose, to attempt to remove Hussein's regime and attempt to free the Iraqi people from the grip of his regime. The US didn't go into Iraq to conquer it, or annex its territory, or steal its oil (which is why today Iraq is free-standing and has such enormous oil revenue pouring into its government coffers).

Did the US have a moral right to invade Nazi Germany? How about Fascist Italy or the Empire of Japan? Did the US have a moral right to invade France and help free it from the Nazis and Vichy France? Yes it did, of course, and the same moral principle in action there was just as valid in regards to Iraq and the Hussein regime that was holding the majority in Iraq hostage. Which simultaneously doesn't mean it was rational for the US to invade Iraq (as it wasn't willing to dedicate the extraordinary resources necessary to provide the security to prevent the civil war between the Sunni and Shia, which would have required far more troops and financial investment).

So even though the US rationally should not have invaded Iraq, the democratic world understands the US didn't go into there in the name of conquest. The democratic world understands the US didn't invade France, Italy or Germany to conquer them. That's why the US is still viewed as good, because intentions matter. It went into Iraq with a very naive belief that - with its superpower might - it could nation-build a new democracy relatively easily in the Middle East. And when the civil war broke out between sectarian groups, the US stood between them and tried to stop it, at great loss to the US in blood and treasure. The US didn't try to take Iraq's oil (India and China are the biggest recipients of Iraqi oil today), it lost over a trillion dollars from the invasion. It's entirely fair to call the US invasion of Iraq a gigantic blunder, a foolish mistake, an act of arrogance by a superpower that thought it could materialize a democracy easily out of thin air. The good nations of the world understand the US didn't try to conquer Iraq for its own empire, that annexation of Iraq wasn't its goal, which is why NATO is still standing and why the US allies in Europe didn't abandon the US.

The US spent hundreds of billions of dollars trying to nation-build Afghanistan to progress the nation forward, including shepherding the first democratic elections in its history. The US effort failed, it was naive in regards to what it would take to accomplish a positive, sustainable outcome in a nation as backwards and poorly developed as Afghanistan. The US is regarded as the good guys in regards to Afghanistan, because of what its intentions for the nation were (compare it to the Taliban and who the friends of the Taliban are - exclusively tyrants and theocrats). Russia went into Afghanistan in the name of conquest, to make it a de facto part of the Soviet Empire, it didn't aim to build a free, democrat nation there.

Russia has gone into Ukraine solely to annex its territory and conquer it for the goals of the Russian Empire, as per Putin's own oft stated world view (of how things should be). Ukraine is a burgeoning democracy pursuing liberal values, Russia is a brutally repressive dictatorship with no human rights that has largely been ruled by one tyrant after another for centuries. Russia's intentions are plainly evil, they aim to enslave the Ukrainian people and destroy their pursuit of liberal values, to force them to be part of Putin's imagined new Russian Empire.