Advertisement
Guest User

Untitled

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