The premise seems flawed. Consider the term’s usage in Shmuel 2:3:22-23:
וְהִנֵּה֩ עַבְדֵ֨י דָוִ֤ד וְיוֹאָב֙ בָּ֣א מֵֽהַגְּד֔וּד וְשָׁלָ֥ל רָ֖ב עִמָּ֣ם הֵבִ֑יאוּ וְאַבְנֵ֗ר אֵינֶ֤נּוּ עִם־דָּוִד֙ בְּחֶבְר֔וֹן כִּ֥י שִׁלְּח֖וֹ וַיֵּ֥לֶךְ בְּשָׁלֽוֹם׃ וְיוֹאָ֛ב וְכָל־הַצָּבָ֥א אֲשֶׁר־אִתּ֖וֹ בָּ֑אוּ וַיַּגִּ֤דוּ לְיוֹאָב֙ לֵאמֹ֔ר בָּֽא־אַבְנֵ֤ר בֶּן־נֵר֙ אֶל־הַמֶּ֔לֶךְ וַֽיְשַׁלְּחֵ֖הוּ וַיֵּ֥לֶךְ בְּשָׁלֽוֹם׃
And behold, the David’s servants and Yoav came from the gedud, and they brought much plunder with them. Avner was not with David in Chevron, for he sent him, and he went away in peace. And Yoav and the entire army that was with him came, and they told Yoav, saying, “Avner Ben Ner came to the king, and he sent him away, and he left in peace.”
Or why go all the way to Nach? Consider Bereishis 49:19:
גָּ֖ד גְּד֣וּד יְגוּדֶ֑נּוּ וְה֖וּא יָגֻ֥ד עָקֵֽב׃
Gad - gedud will gedud him, and he will gedud at their heels.
which Rashi explains as:
גד גדוד יגודנו, גְּדוּדִים יָגוֹדּוּ הֵימֶנּוּ שֶׁיַּעַבְרוּ הַיַּרְדֵּן עִם אֲחֵיהֶם לַמִּלְחָמָה כָּל חָלוּץ עַד שֶׁנִּכְבְּשָׁה הָאָרֶץ: והוא יגוד עקב. כָּל גְּדוּדָיו יָשׁוּבוּ עַל עֲקֵבָם לְנַחֲלָתָם שֶׁלָּקְחוּ בְּעֵבֶר הַיַּרְדֵּן, וְלֹא יִפָּקֵד מֵהֶם אִישׁ:
”Gad - gedud will gedud him”: Gedudim will gedud from him - that they will cross the Yardein with their brothers in war, all armed, until the Land is conquered. “And he will gedud at their heels”: all his gedudim will return on their heels to their territory which they took from Transjordan, and no man would be missing.
Clearly the term גדוד has something to do with warfare. As such, consider Steinsaltz’s interpretation of the Gemara in Berachos:
אמר להם המלך: לכו ופשטו ידיכם בגדוד, במלחמה על העמים מסביב, להרחיב את גבולות הארץ, ולתת אפשרויות של פרנסה ליושביה.
leading to Sefaria’s translation:
King David told them: Go and take up arms with the troops in battle in order to expand our borders and provide our people with the opportunity to earn a livelihood.
To address your question point-by-point, with this background:
What is the legal justification for this measure?
It’s called a Milchemes Reshus - a king is allowed to declare war on another nation, provides certain prerequisites are met, as discussed in Rambam Hil. Melachim 5:1 and ch. 6.
Who were these bandits/troops?
Syria is commonly cited throughout Shas as a land which has certain stringencies over outside Eretz Yisrael yet is more lenient in many respects than Eretz Yisrael proper (ex. Demai 6:11, RH 1:4, BK 7:7, etc.). Rashi in many of those places quotes a tradition that this refers to Aram Tzova, a land which David conquered (ex. Berachos 36a, DH “u’ve’Suria” - presumably this is known from Tehillim 60:2, “when he fought with Aram Naharaim and Aram Tzovah, and Yoav returned and defeated Edom”). While the Gemara in question doesn’t cite the enemy specifically, I think it’s a fair assumption that it’s the same war.
If they were dangerous and the king always had a right to do this, then why were גדוד hanging around Israel, and why did King David only confront them when the people needed money? If the גדוד was not a threat, then why would the King decide to hurt and rob other people for the sake of financial gain?
Because they weren’t in Eretz Yisrael; this was a war on a foreign nation for economical gain. The king can’t just attack for no reason, but if the people are so poor that nobody can give Tzedakah, then he has no other choice but to find resources elsewhere.