First of all, take a look at this post -
http://europetrainsguide.com/forum/index.php/topic,39.msg130.html#msg130As you can see, there are two daily trains from Thessaloniki to Belgrade - at 5:57 (arrives to Belgrade at 18:06) and 17:05 (at Belgrade - 5:59). During summer with the train departing 17:05 runs direct carriages to Budapest, so you can actually travel with no changes all the way to Budapest, but you can as well change in Belgrade. At 7:20 from Belgrade departs train to Prague (IC 345) via Budapest (there at 14:55), other train (#342) departs Belgrade at 10:00, arrives to Budapest at 18:04 and overnight train Belgrade-Budapest 21:25-5:04. Budapest-Gyor is easy, trains runs really often
In terms of tickets, it is better to break your tickets via Belgrade. One way Thessaloniki-Belgrade ticket costs around 30 euro, one way Belgrade-Budapest purchased in Belgrade cost 15 euro. Both tickets are regular fare, so you can buy those any time, no limits on the amount of tickets sold. Domestic Hungarian ticket Budapest-Gyor cost from 2480 HUF (around 9 euro)
In terms of safety usually you will not need to worry, but it is better to use couchette or sleeper on the route from Thessaloniki to Belgrade, just in case