In terms of the title- for longer-term health in terms of working and studying, why not work on time management with efficient use of stimulants and healthy sleep/habits rather than cramming or extended sprees?
Low dose methylphenidate combined with time management like pomodoro review techniques like spaced repetition (anki, etc...) can be beneficial. 20 minute review periods in morning/ at shifts. Setting up systems to more efficiently approach balancing studying and work. Restoration periods / schedules / mindfulness / meditation. What is your end goal? Do you want to retain knowledge or just prove that you can stay up?
Consider u-shaped effects of stimulants on cognition.
Psychostimulants such as cocaine have been used as performance enhancers throughout recorded history. Although psychostimulants are commonly prescribed to improve attention and cognition, a great deal of literature has described their ability to ...
www.ncbi.nlm.nih.gov
The importance of sleep for long term memory consolidation is something to consider. It can be easier to recall things with sleep, making some projects or finals less labor-intensive. Going to excess without sleeping seems more an element of machismo than an effective long-term technique for learning. Not that it isn't uncommon culturally, but that doesn't mean it is effective. If that is a motivator, then do better than them by not spending large bursts of time inefficiently, learning to do more in half hour than many spend over four.
I mean if you are going to use stimulants, using them to establish healthy habits and routines/systems, speeding that period of adjustment, might be an option to consider. It is possible to save more time learning to learn/balance over pursuing inefficient strategies made worse (or deceptively effective) with stimulants. Stimulants can reinforce whatever you do.
As for just staying up, modafinil, caffeine, meth will do it.