Computers are dumb. They do what they are told to do, and that’s it. Powerful as they are, computers cannot (yet) make decisions on anything like a human level. If a nasty piece of code gets injected into a computer, it will execute it - unless other software tells it not to. The computer doesn’t ‘know’ that the code is undesirable.