Comment by Anonyneko
Depends on the country. Many countries don't have fair use laws; thus, fan fiction is technically illegal there. In practice though you're not likely to be sued unless you're making a significant amount of money or damaging the brand value somehow.