I download all kinds of stuff from shady places. But I want to be sure there are no nasty viruses in these files. Is it enough if I just check the hash?
For example, when I go https://www.virustotal.com here and upload my file, I think it computes the hash and looks for it in the database. If the hash is clear - the file is clear. Is that true?
For starters the inverse is also not true. Just because some AV engine claims the file is malicious (or even just suspicious) that doesn't have to be the case. Most laypeople won't even know - let alone care about - the difference between outright malicious (malware) and PUA/PUS (potentially unwanted application/software). And if they did, they would have to decipher a very heterogeneous bunch of detection names across any number of AV engines.
And if they bothered to do that they'd potentially figure out that some of the products listed on VirusTotal share the same AV engine under the hood. And if a single engine shows a false positive but it appears, say, in three products out of four overall, that still makes "only" two false positives.
Last but not least an AV can only find or not find something. VirusTotal calls the latter "Undetected". Undetected is not the same as clean (or good or not malicious).
Can I add a virus and modify the file so the hash would be identical to the clean file?
Theoretically yes, but it's an involved process and feasibility depends on the hash algorithm in question (e.g. if you rely on MD5 then you have relied on the wrong digest algorithm in the last ~17 years).
For all practical purposes this can currently be considered impossible for SHA-256 (your example) in particular, though.
EDIT: The main goal of my question was this:
Suppose we have a perfect AV(find every virus) that has sha256 database, and everyone can upload any file to it.
There is no such thing (perfect AV). I worked almost fifteen years in the industry and can tell you it simply doesn't exist.
And to stress a point I made elsewhere: an AV that claims your system is clean must be using extraterrestrial technology way ahead of anything we know on this planet or it's using a whitelisting approach (which you can get cheaper with AppLocker/SRPs) that would prevent anything unknown by default or it's telling a lie (the most likely scenario).
What a conventional AV can claim is that it hasn't found anything. And there is a big difference between a clean system and one where no (known malicious) entities were found.
This AV scans the uploaded file and stores the hash.
If it scans the file what is it doing? A hash-based (whitelisting) AV would only have to have the capability to prevent files not matching hashes from its own database to run. There wouldn't have to be any heuristics, behavioral analysis (which on the endpoint is usually very limited for performance reasons), any signatures/fingerprints.
Suppose, the client downloaded some file somewhere and calculates its sha256 and searches the hash database.
The database says that there is such a hash in it, and file with that hash has no virus.
What are the chances that client's file has a virus in it?
The chances are mathematically very slim. But we've seen hash collisions during my tenure in the AV industry which is why we never relied on a single hash algorithm for identification (which is what you want).
So this is more like a hashing problem
Yes it is, but I think there is also a misunderstanding of the capabilities anti-malware can offer.
Similarly signing (or specifically code-signing) provide no safety from running malicious code. They attest that the signer signed a file -- and usually when it was signed. You'll have to trust the signer and the certification authorities (CAs) in the certificate chain. And if that trust gets abused there is potentially legal recourse if the CA didn't properly verify the signer prior to issuing the certificate. Signing "just" raises the bar quite a bit. But it's not a 100% bulletproof guarantee that an executable whose signature checks out doesn't turn out to be malicious.
Long story short: if you had different (unrelated) hash algorithms and computed the hash over a the same file using different of these algorithms, each added (unrelated) algorithm should significantly decrease the chances that you ever run into a hash collision. And consequently the chances of it being an entirely unrelated file would shrink to close to zero.
But even if someone stored multiple such hashes of every known file, someone would have to pass judgment on that file first. And that is the weak point in your whole scheme here. There are no perfect AVs. And the load of samples flowing in daily is incredible. Manual analysis is left only to high-profile or otherwise interesting malware and the rest gets usually the sandbox treatment (behavioral analysis) and a number of heuristics then decide into which category a file supposedly falls.
False positives and false negatives exist. None of this is as perfect as in your thought experiment 😉
NB: I used AV (short for anti-virus) because laypeople use the term "virus" for malware in general. However, the term virus only refers only to a subset of the existing malware types.