Advertisement
Guest User

Untitled

a guest
Jun 26th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. /* Welcome freedom fighter. The liberation front has gained access to the
  2. * source code for a wire transfer program used by FIRST NATIONAL CYBER, a
  3. * bank used by the alliance for oppression and we need you to find a
  4. * vulnerability in this program and stea... er liberate the funds from an
  5. * operative named prince_john.
  6. *
  7. * The bank programmers have attempted to put in a number of defense checks
  8. * to prevent abuse of the bank wire network, but we suspect that deadlines
  9. * and corporate training programs have left them without the motivation
  10. * required to think of every possibility.
  11. *
  12. * It is imperative that you find a way to drain the account completely
  13. * otherwise they may be able to withdraw the remaining funds and go
  14. * underground before we can finish tracking them in the physical world.
  15. *
  16. * Good luck and as always, should you be detected your actions will be
  17. * blamed on the Chinese Government.
  18. *
  19. * ~~~0xDEADFEED~~~
  20. *
  21. * To build and run the wire transfer program:
  22. *
  23. * $ cc -o wire_transfer wire_transfer.c
  24. * $ ./wire_transfer
  25. *
  26. */
  27.  
  28. #include <stdbool.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32.  
  33. #define MAXLEN 255
  34.  
  35. typedef struct Account {
  36. int balance;
  37. char owner[MAXLEN];
  38. } Account;
  39.  
  40. Account *init_account(const char *name, int balance);
  41. bool wiretransfer(Account *source, Account *dest, int amount);
  42. void display_balance(Account *account);
  43. void exit_if(bool condition, const char *message);
  44. void validate_heist(Account *target);
  45.  
  46. int main(int argc, const char *argv[])
  47. {
  48. unsigned int transfer_amount;
  49. char input[MAXLEN];
  50.  
  51. printf("----- WELCOME TO FIRST CYBER BANK -----\n");
  52. printf("username: robin_hood\n");
  53. printf("password: *****************\n");
  54. printf("Logged In Successfully!\n");
  55. printf("account> wiretransfer prince_john\n");
  56. printf("----- BEGIN WIRE TRANSFER SETUP -----\n");
  57.  
  58. Account *yours = init_account("robin_hood", 10000);
  59. Account *theirs = init_account("prince_john", 500000);
  60.  
  61. display_balance(yours);
  62. display_balance(theirs);
  63.  
  64. while (true) {
  65. printf("Amount to transfer from your account to theirs: ");
  66. fgets(input, sizeof(input), stdin);
  67. transfer_amount = strtoul(input, NULL, 10);
  68.  
  69. if ((long)transfer_amount > 0) {
  70. break;
  71. }
  72.  
  73. printf("Invalid transfer amount!\n");
  74. }
  75.  
  76. printf("----- ATTEMPTING WIRE TRANSFER -----\n");
  77. printf("Wire transfer status: ");
  78. printf("%s\n", wiretransfer(yours, theirs, transfer_amount) ? "SUCCESS" : "FAILURE");
  79.  
  80. display_balance(yours);
  81. display_balance(theirs);
  82. validate_heist(theirs);
  83.  
  84. free(yours);
  85. free(theirs);
  86.  
  87. return 0;
  88. }
  89.  
  90. Account *init_account(const char *name, int balance)
  91. {
  92. Account *account = malloc(sizeof(Account));
  93. exit_if(account == NULL, "Problem allocating memory");
  94.  
  95. if (account != NULL) {
  96. strncpy(account->owner, name, sizeof(account->owner) - 1);
  97. account->balance = balance;
  98. }
  99.  
  100. return account;
  101. }
  102.  
  103. bool wiretransfer(Account *source, Account *dest, int amount)
  104. {
  105. if (source == NULL || dest == NULL) return false;
  106. if (source->balance - amount < 0) return false;
  107. if (dest->balance + amount < 0) return false;
  108.  
  109. source->balance -= amount;
  110. dest->balance += amount;
  111. return true;
  112. }
  113.  
  114. void display_balance(Account *account)
  115. {
  116. if (account == NULL) return;
  117. printf("%s has $%d in their account.\n", account->owner, account->balance);
  118. }
  119.  
  120. void exit_if(bool condition, const char *message)
  121. {
  122. if (condition) {
  123. perror(message);
  124. exit(EXIT_FAILURE);
  125. }
  126. }
  127.  
  128. void validate_heist(Account *target)
  129. {
  130. if (target->balance == 0) {
  131. printf("Target funds liberated: You win ;)\n");
  132. } else {
  133. printf("Target still has operating funds, try again.\n");
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement