Hannah R. answered 1d
Stanford biochemistry PhD candidate for Math and Science Tutoring
In a way, through horizontal gene transfer.
Viruses are meant to just take their DNA to their hosts. But sometimes a virus will inadvertently incorporate some host DNA into its own DNA. When the virus infects a new host, they send this old host DNA along with the viral DNA, and recombination can occur allowing the new host to get new DNA from the old host. This is a type of horizontal gene transfer. In eukaryotes, double-stranded DNA viruses are important vectors for horizontal gene transfer, and in prokaryotes bacteriophages are important vectors.
If the new gene gives the new host something useful, like antibiotic resistance or the ability to metabolize a new nutrient, then the virus has inadvertently benefited the host.
Another human example (which is not horizontal gene transfer) involves the cowpox virus. The cowpox virus was a virus dairy workers got from touching the udders of infected cows. It caused illness, so by itself it wasn't great. But it trained the immune system to deal with the deadlier smallpox virus. People who had cowpox before were immune to smallpox and survived the outbreaks, and cases like these inspired smallpox vaccines. So this is another situation where a virus can benefit its host, by training their immune systems to tackle bigger threats.